Image Manipulation

N

Nicky

I am using Visual C# 2005 and I need help creating a filter that will
turn a picture black and white. This code I have so far is:

byte red, green, blue, avg, newColor;
int x;
int y;

ImageArray img = new ImageArray(pictureBoxMainImage.Image);

img.LockImage();

for (x = 0; x < img.Width; x++)
for (y = 0; y < img.Height; y++)
{
blue = img[x, y].B;
green = img[x, y].G;
red = img[x, y].R;

avg = (byte)((red + blue + green) / 3);

if (( red > 127) (blue > 127) (green > 127 ))
{
blue = img[255, 255].B;
green = img[255, 255].G;
red = img[255, 255].R;
//newColor = 0;
}
if ((red < 127) (blue < 127) (green < 127))
{
blue = img[0, 0].B;
green = img[0, 0].G;
red = img[0, 0].R;
//newColor = 255;
}

img[x, y] = System.Drawing.Color.FromArgb(avg, avg,
avg);
//img[x, y] = System.Drawing.Color.FromArgb(int red, int
blue, int green);
}

img.UnlockImage();

labelOutput.Text = (img.Width * img.Height).ToString();
pictureBoxMainImage.Image = img.ToBitmap();

}

Please help me with this code as I am a beginner in college and having
a terrible time.
 
B

Bruce Wood

First, it's not completely clear from your code what your algorithm is.
Do you want the colour to be black if the average of red, blue, and
green is < 127, otherwise white?

Anyway, some comments.

First, whenever you first mention the name of a variable, you have to
_declare_ it: say what type it is going to be. After that, the compiler
knows, so you don't have to do it again. So, where you first use blue,
red, and green, you have to say that they're ints:

int blue = img[x, y].B;
int green = img[x, y].G;
int red = img[x, y].R;

Then after that you don't, so where you have

//img[x, y] = System.Drawing.Color.FromArgb(int red, int
blue, int green);

commented out, that line would be wrong, because there's no need to say
that red is an "int" (again) because you've already said so, the first
time you mentioned "red".

Second, in your "if" statements, you have to join conditions with some
sort of operator. You can't just say "if red is less than 127 green is
less than 127...". Even in English that doesn't work: you have to say
whether you mean "if red is less than 127 OR green is less than 127
...." or whether you mean "if red is less than 127 AND green is less
than 127 ...". Therefore, your "if" would need to look like either

if ((red < 127) || (blue < 127) || (green < 127))

or

if ((red < 127) && (blue < 127) && (green < 127))

Either way, I don't think that's what you want. You calculated "avg"
just above (which needs to be declared as an "int", by the way). I
suspect that you want to be testing that, not red, blue, and green
individually.
 
N

Nicky

This did not help me. I just don't understand where I'm going wrong in
getting the picture to turn black and white with the parameters above.
Could I do this with a "foreach" loo/statement?
 
S

SharpKnight

I am not an expert but what I would do is the following:

I would in the code use Image instead of ImageArray

then use Color pixelColor = img.GetPixel(x, y);
to retrieve the color of position x, y

then instead of
if (( red > 127) (blue > 127) (green > 127 ))
(which can not work without operators between the conditions) simply use

pixelColor.GetBrightness()
(or the average) to see if larger or smaller than 127

In the end use
SetPixel(...)
to change the color of the pixel (in the image)

Concerning the control I am not sure if it will keep the changes (maybe
when it refreshes it reloads the original image again).

For debugging purpose I suggest that you save the image to disk
(with the img.Save(..) method) so that you can check your modified image
on the disk.
If it is black and white and your control is still not showing the image
than the problem is in the refreshing of the control.


good luck !


Nicky schreef:
 
N

Nicky

We are working on filters. I have to create a filter that turns the
picture black & white, blur and inverse.

The instructions for black & white read:

this filter works by taking the average of RGB for a pixel and seeing
if it is above 127 or not. If it is above 127, set the pixel to white.
If it is below 127 set the pixel to black.

I have tried several different things and my picture keeps turning grey.
 
J

James Park

Nicky said:
I am using Visual C# 2005 and I need help creating a filter that will
turn a picture black and white. This code I have so far is:

<code snipped>

Here's a few things that jump out:

Code:
if (( red > 127) (blue > 127) (green > 127 ))

This isn't legal, and not really what you want. You calculated avg for a
reason, use it for the test.

Code:
blue = img[255, 255].B;
green = img[255, 255].G;
red = img[255, 255].R;

This code gets the pixel at the coordinate (255, 255) (if it even exists),
and assigns the corresponding red, green, and blue parts. What you probably
meant to do here is simply set red, green, and blue to 255.

Code:
if ((red < 127) (blue < 127) (green < 127))

Even if this was somehow legal and did what you wanted, you miss the miss
the case of exactly 127.

Code:
img[x, y] = System.Drawing.Color.FromArgb(avg, avg, avg);

This code will assign some shade of gray to the pixel, not necessarily black
or white.

Here's what I meant in code if I was unclear:

if (avg > 127)
{
blue = 255;
green = 255;
red = 255;
}
if (avg <= 127)
{
blue = 0;
green = 0;
red = 0;
}

img[x, y] = System.Drawing.Color.FromArgb(red, green, blue);

Alternatively, you could do:

img[x, y] = avg > 127 ? System.Drawing.Color.White :
System.Drawing.Color.Black;
 
N

Nicky

Hi James

I tried this:
if (avg > 127)
{
blue = 255;
green = 255;
red = 255;


}


if (avg <= 127)
{
blue = 0;
green = 0;
red = 0;


}


img[x, y] = System.Drawing.Color.FromArgb(red, green, blue);

and I still got Gray. Thanks. I'm just not getting this,.
 
D

dotnetchic

I am wondering why you are still trying to use unsafe image processing.
Instead, try using GetPixel and SetPixel. Here's an example.

Bitmap oldImage = new Bitmap(pictureBoxMainImage.Image);
Bitmap newImage = new Bitmap(oldImage.Width, oldImage.Height);

for (int x=0; x<pictureBoxMainImage.Image.Width; x++)
{
for (int y=0; y<pictureBoxMainImage.Image.Height; y++)
{
// get the pixel color from the old image and find its average
rgb value
Color color = oldImage.GetPixel(x, y);
int avg = (color.R + color.G + color.B) / 3;

// select a new color for the new image
Color newColor = Color.White; // default to white
if (avg > 127) // set to black if the
average is above 127
{
newColor = Color.Black;
}

// set the new color in the new image
newImage.SetPixel(x, y, newColor);
}
}

// do whatever with your new image...I just set it back to the picture
box
pictureBoxMainImage.Image = newImage;
 
D

dotnetchic

Oops! Just reverse those two color statements above.

Color newColor = Color.Black;
if (avg > 127)
{
newColor = Color.White;
}

But, you get the gist. You should really check out the GDI+ library
for your other filters as well. I believe Chris posted a link to one
above. Anyway, hope this helps.

Cheers ;)
 
N

Nicky

This is the code that I used for the grey filter.

{
byte red, green, blue, avg;
int x;
int y;

ImageArray img = new ImageArray(pictureBoxMainImage.Image);

img.LockImage();

for (x = 0; x < img.Width; x++)
for (y = 0; y < img.Height; y++)
{
blue = img[x, y].B;
green = img[x, y].G;
red = img[x, y].R;

avg = (byte)((red + blue + green) / 3);

if (avg == 127)
{
blue = green = red = 0;
}
else
{
blue = green = red = 255;
}

img[x, y] = System.Drawing.Color.FromArgb(avg, avg,
avg);
}

img.UnlockImage();

labelOutput.Text = (img.Width * img.Height).ToString();
pictureBoxMainImage.Image = img.ToBitmap();

}


We have used GetPixel or SetPixel. I am trying to use what we have
learned in class so far.
 
D

dotnetchic

Ok. Then try this...since you're using FromArgb, you'll need to pass
the alpha value, which in both cases should be 255. So then you've got

Color newColor = Color.FromArgb(255, 0, 0, 0); // black
if (avg > 127)
{
newColor = Color.FromArgb(255, 255, 255, 255); // white
}

img[x, y] = newColor;
 
B

Bruce Wood

Well, one problem is that you're testing

if (avg == 127)

so you'll get black only if the average of red, green, and blue is
exactly 127. I think that you want

if (avg < 127)

(Your problem doesn't specification doesn't say what to do if the
average is exactly 127. I picked white for that case.)

As well, you're saying:

img[x, y] = System.Drawing.Color.FromArgb(avg, avg, avg);

so you're setting all three: red, green, and blue, to the same number:
the average. Setting all three colours to the same value gets you gray.
What you really want to say is:

img[x, y] = System.Drawing.Color.FromArgb(red, green, blue);

to use the new red, green, and blue values that you set in the "if"
statement above.
 
J

James Park

Bruce Wood said:
I think that you want

if (avg < 127)

(Your problem doesn't specification doesn't say what to do if the
average is exactly 127. I picked white for that case.)

There are exactly 256 numbers, so an even 128 split on both sides is
possible (the above suggestion will end up 127/129). Then again, things are
kind of skewed anyway (like red = 1, blue = 1, green = 0 will mean avg = 0
even though it's closer to 1), so it's probably not a big deal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top