PDA

View Full Version : شمارش پیکسل یک رنگ دلخواه



man4toman
پنج شنبه 25 مرداد 1386, 21:12 عصر
سلام دوستان .
میخواستم بپرسم چجوری میشه تو یه عکس تعداد پیکسل یک رنگ رو بشمریم.
مثلا تعداد پیکسلهای منطقه سیاه تو یه عکس رنگی.
آیا امکان همچین کاری هست.اگه هست به چه صورتیه .
با یه تیکه کد قضیه رو توضیح بدین.
با تشکر

PC2st
جمعه 26 مرداد 1386, 01:21 صبح
با تغییراتی در متد ShowChanges



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.ShowChanges();
}
private unsafe void ShowChanges()
{
DateTime dt = DateTime.Now;
Bitmap b1 = new Bitmap("1.jpg");
int iLength = b1.Size.Width;
int jLength = b1.Size.Height;
int repeats = 0;
float percent;
UnsafeBitmap ub1 = new UnsafeBitmap(b1);
// Black Color Elements
int blue = 0, red = 0, green = 0;
ub1.LockBitmap();
for (int i = 0; i < iLength; i++)
for (int j = 0; j < jLength; j++)
if (ub1.PixelAt(i, j)->blue == blue && ub1.PixelAt(i, j)->green == green && ub1.PixelAt(i, j)->red == red)
repeats++;
ub1.UnlockBitmap();
percent = (float)repeats / b1.Size.Width / b1.Size.Height * 100;
TimeSpan ts = DateTime.Now - dt;
MessageBox.Show(ts.TotalSeconds.ToString("00.00") + " Second\n\n" + percent.ToString("00.00") + "%");
}
}
public unsafe class UnsafeBitmap
{
Bitmap bitmap;
// three elements used for MakeGreyUnsafe
int width;
BitmapData bitmapData = null;
Byte* pBase = null;
public UnsafeBitmap(Bitmap bitmap)
{
this.bitmap = new Bitmap(bitmap);
}
public UnsafeBitmap(int width, int height)
{
this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}
public void Dispose()
{
bitmap.Dispose();
}
public Bitmap Bitmap
{
get
{
return (bitmap);
}
}
private Point PixelSize
{
get
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref unit);
return new Point((int)bounds.Width, (int)bounds.Height);
}
}
public void LockBitmap()
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap.GetBounds(ref unit);
Rectangle bounds = new Rectangle((int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height);
// Figure out the number of bytes in a row
// This is rounded up to be a multiple of 4
// bytes, since a scan line in an image must always be a multiple of 4 bytes
// in length.
width = (int)boundsF.Width * sizeof(PixelData);
if (width % 4 != 0)
{
width = 4 * (width / 4 + 1);
}
bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
pBase = (Byte*)bitmapData.Scan0.ToPointer();
}
public void UnlockBitmap()
{
bitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}
public PixelData* PixelAt(int x, int y)
{
return (PixelData*)(pBase + y * width + x * sizeof(PixelData));
}
}
public struct PixelData
{
public byte blue;
public byte green;
public byte red;
}
}

برای حفظ قانون کپی رایت :) اینم لینکی که کلاس UnsafeBitmap در اون تعریف شده:
http://www.dreamincode.net/forums/index.php?showtopic=14788