Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI I am trying to convert an image into normalized rgb now my code is. Folowing code is in the loop equal to image dimensions

C#
     Color color = grayScale.GetPixel(xCoordinate, yCoordinate);
                    sum = (color.R + color.G + color.B);
                    red = color.R;
                    green = color.G;
                    blue = color.B;

                   
                        redColor =  (double)red/ (double)(sum);
                        greenColor = (double)green / (double)(sum);
                        blueColor = (double)blue /(double)(sum);

//Color.FramArgb only accepts int and my val;ues are in double since they are normalized b/w 0-1 
                        grayScale.SetPixel(xCoordinate, yCoordinate,          Color.FromArgb(redColor, greenColor,blueColor));
                     
                    }

now the problem is how can i pass this argument since i cannot convert it to int because i would loose my value.?
Thanks anyways
Posted
Updated 9-Jan-14 10:48am
v2
Comments
Ron Beyer 9-Jan-14 16:49pm    
Pass what argument? The color? The components? The image?

multiply by 255:
C#
grayScale.SetPixel(xCoordinate, yCoordinate, Color.FromArgb((int)(255*redColor), (int)(255*greenColor),(int)(255*blueColor)));
 
Share this answer
 
Comments
loraloper_22 9-Jan-14 16:53pm    
Thank you so much... should have done it myself :)...
Matt T Heffron 9-Jan-14 17:03pm    
You're going to discover that your calculations do not create gray scale!
In gray scale all three of R,G,B would be equal.
The simplest calculation would be just the average of the RGB => gray = (R + G + B)/3
This is a kind of gray scale but it doesn't take into account the way that the human eye responds to color, so it will appear wrong.
See: http://en.wikipedia.org/wiki/Gray_scale#Converting_color_to_grayscale
loraloper_22 9-Jan-14 17:08pm    
i know this is not going to create a greysclae i just unconsciously named it grey scale. Thanks for pointing this out.
One last question what should i do when any of the RGB component is zero i mean i cannot divide there. Any suggestions?
Matt T Heffron 9-Jan-14 17:21pm    
You're only in trouble if sum == 0
Just check for that special case.
0/(not-zero) is OK
In fact, your calculations look to me as if you'll end up with pretty much the same colors as you started with.
loraloper_22 9-Jan-14 17:37pm    
Yeah i have added a special case.
"In fact, your calculations look to me as if you'll end up with pretty much the same colors as you started with."
Well i am not having same colors as is started with. Do you think i am normalizing the image in a wrong way?
anyways thanks a lot for you help
GetPixel is WAY to slow. Search the articles here for "Image processing for dummies". Yes, you read that right. There's an example in those articles that demonstrates a far faster method of converting an image to grayscale.
 
Share this answer
 
Comments
loraloper_22 9-Jan-14 17:01pm    
Yes i know about Lockbit method i was just wondering how to solve that particular problem. Thanks anyways

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900