How can i convert the images to gray and then to get each image values between 0 - 255 ? Instead of

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the first function im using today:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; unsafe <span style="color:Blue; double GetAveragePixelValue(Bitmap bmp)
{
BitmapData bmData = <span style="color:Blue; null;

<span style="color:Blue; try
{
bmData = bmp.LockBits(<span style="color:Blue; new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

<span style="color:Blue; int stride = bmData.Stride;
IntPtr scan0 = bmData.Scan0;
<span style="color:Blue; int w = bmData.Width;
<span style="color:Blue; int h = bmData.Height;

<span style="color:Blue; double sum = 0;
<span style="color:Blue; long pixels = bmp.Width * bmp.Height;

<span style="color:Blue; byte* p = (<span style="color:Blue; byte*)scan0.ToPointer();

<span style="color:Blue; for (<span style="color:Blue; int y = 0; y < h; y++)
{
p = (<span style="color:Blue; byte*)scan0.ToPointer();
p += y * stride;

<span style="color:Blue; for (<span style="color:Blue; int x = 0; x < w; x++)
{
<span style="color:Blue; double i = ((<span style="color:Blue; double)p[0] + p[1] + p[2]) / 3.0;
sum += i;

p += 3;
}

<span style="color:Green; //no offset incrementation needed when getting
<span style="color:Green; //the pointer at the start of each row
}

bmp.UnlockBits(bmData);

<span style="color:Blue; double result = sum / (<span style="color:Blue; double)pixels;

<span style="color:Blue; return result;
}
<span style="color:Blue; catch
{
<span style="color:Blue; try
{
bmp.UnlockBits(bmData);
}
<span style="color:Blue; catch
{

}
}

<span style="color:Blue; return -1;
}
[/code]
<br/>
And then im adding the averages to a list like that:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; int ISampleGrabberCB.BufferCB(<span style="color:Blue; double sampleTime, IntPtr pBuffer, <span style="color:Blue; int bufferLen)
{
<span style="color:Blue; using (<span style="color:Blue; var bitmap = <span style="color:Blue; new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
<span style="color:Blue; if (!<span style="color:Blue; this.Secondpass)
{
<span style="color:Green; //get avg
<span style="color:Blue; double average = GetAveragePixelValue(bitmap);

<span style="color:Blue; if (AveragesList == <span style="color:Blue; null)
AveragesList = <span style="color:Blue; new List<<span style="color:Blue; double>();
<span style="color:Green; //save avg
AveragesList.Add(average);
[/code]


Instead of that i want first to convert each image pixels to gray:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; static Bitmap MakeGrayscale(Bitmap original)
{
<span style="color:Green; //create a blank bitmap the same size as original
Bitmap newBitmap = <span style="color:Blue; new Bitmap(original.Width, original.Height);

<span style="color:Green; //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);

<span style="color:Green; //create the grayscale ColorMatrix
ColorMatrix colorMatrix = <span style="color:Blue; new ColorMatrix(
<span style="color:Blue; new <span style="color:Blue; float[][]
{
<span style="color:Blue; new <span style="color:Blue; float[] {.3f, .3f, .3f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.59f, .59f, .59f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.11f, .11f, .11f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 1, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 0, 1}
});

<span style="color:Green; //create some image attributes
ImageAttributes attributes = <span style="color:Blue; new ImageAttributes();

<span style="color:Green; //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);

<span style="color:Green; //draw the original image on the new image
<span style="color:Green; //using the grayscale color matrix
g.DrawImage(original, <span style="color:Blue; new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

<span style="color:Green; //dispose the Graphics object
g.Dispose();
<span style="color:Blue; return newBitmap;
}
[/code]

Then when i have a gray image i want to make a List<> of int ill call it myCounters
And i want to calculate im not sure how the values between 0-255 of each image to scan all pixels I think its called: histogram

So on each image i need to scan all the pixels after i converted them to gray and if there is any changes in between 0 to 255 to change it in the List<>
In the List<> i should have 0 to 255 counters of INT type and somehow i need to forward each counter by one according to the pixels scan.
If in the image in some place it changed so ti change in the List<> counter number 5 by one.

I didnt understand it good.



View the full article
 
Back
Top