473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1927
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rune Johansen | last post by:
Hi. I'm doing some image manipulation in an applet using the example code on this page: http://www.akop.org/art/pixels3.htm However, I really want an application rather than an applet, I just...
6
by: Giggle Girl | last post by:
Overall Background: I am in charge of migrating an already-made Content Mangement System from ASP to PHP. I do not know PHP -- yet! I am trying to foresee potential issues, and here is one. ...
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
9
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.?...
9
by: zacariaz | last post by:
I dont know, and i dont much like javascript, however, i am told that the only way to do want i want to do, is with javascript, so here goes. zoom and cut is the only features i need. 1. the...
10
by: Pulzar | last post by:
Hi there, I want to show a simple image on a web page, and allow the viewer to select and change one of the colours used in the image, and immediately preview the result. I'd like to keep the...
3
by: jon | last post by:
Hello, I've had long standing code that runs in IE, that I'm testing with firefox unsuccessfully now. The problem seems to be that images that I dynamically create don't fire their onload event...
8
by: shotokan99 | last post by:
i have this situation. i have a query string: http://www.myquerystring.com?x=xxxxx what this url does is it will return or start downloading a .png file. what i wanted to do is trap this png...
12
by: laredotornado | last post by:
Hi, I'm using PHP 5. I was wondering given an image, a.jpg, how can I make an image that would look like you slid a white index card (which I have a file, white.jpg with the same dimensions as...
5
by: Jumping Arne | last post by:
I'm going to try to write some imange manipulation code (scaling, reading EXIF and IPTC info) and just want to ask if PIL is *THE* library to use? I looked at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.