471,315 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Image Manipulation

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.

Feb 20 '06 #1
16 1867
See if this helps:

http://www.bobpowell.net

Feb 20 '06 #2
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.

Feb 20 '06 #3
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?

Feb 20 '06 #4
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:
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.

Feb 20 '06 #5
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.

Feb 20 '06 #6
"Nicky" <ig*******@bellsouth.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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;
Feb 20 '06 #7
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,.

Feb 20 '06 #8
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;

Feb 20 '06 #9
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 ;)

Feb 20 '06 #10
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.

Feb 20 '06 #11
One more thing...

If for some reason, you need to process the image at the byte level
(for speed or because your professor is telling you to, etc.), then you
might want to check out this page on msdn that covers unsafe image
processing. It _is_ quicker, but it's a little trickier to accomplish.
http://msdn.microsoft.com/library/de...rp11152001.asp

Feb 20 '06 #12
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;

Feb 20 '06 #13
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.

Feb 21 '06 #14
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.
Feb 21 '06 #15
if it's greater than 127, the color is white; less than 127 the color
is black.

Feb 21 '06 #16
See this:

http://www.bobpowell.net/imagesaturation.htm

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
To a tea you esteem
a hurting back as a wallet.
"Nicky" <ig*******@bellsouth.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
if it's greater than 127, the color is white; less than 127 the color
is black.

Feb 21 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rune Johansen | last post: by
6 posts views Thread by Giggle Girl | last post: by
9 posts views Thread by Job | last post: by
9 posts views Thread by zacariaz | last post: by
10 posts views Thread by Pulzar | last post: by
3 posts views Thread by jon | last post: by
8 posts views Thread by shotokan99 | last post: by
12 posts views Thread by laredotornado | last post: by
5 posts views Thread by Jumping Arne | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.