Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
     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 9-Jan-14 10:45am
Edited 9-Jan-14 10:48am
v2
Comments
Ron Beyer at 9-Jan-14 16:49pm
   
Pass what argument? The color? The components? The image?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

multiply by 255:
grayScale.SetPixel(xCoordinate, yCoordinate, Color.FromArgb((int)(255*redColor), (int)(255*greenColor),(int)(255*blueColor)));
  Permalink  
Comments
loraloper_22 at 9-Jan-14 16:53pm
   
Thank you so much... should have done it myself :)...
Matt T Heffron at 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 at 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 at 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 at 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
Matt T Heffron at 9-Jan-14 17:52pm
   
What you are doing is expanding the color of each pixel independently. E.g., a nearly black pixel of (1,0,0) would end up as full intensity red. If you are trying to expand the range of the image as a whole, you probably want to do something like normalize by the maximum sum in the whole image. Again, see the "Image processing for dummies" article(s) for other operations.
loraloper_22 at 9-Jan-14 18:50pm
   
"What you are doing is expanding the color of each pixel independently."| This is what exactly i am trying to achieve.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
loraloper_22 at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Shai Vashdi 339
2 Sergey Alexandrovich Kryukov 291
3 Peter Leow 135
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,335
2 Peter Leow 4,040
3 Maciej Los 3,540
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 9 Jan 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid