How can I convert an RGB image to grayscale but keep one color?

Figure pic = imread('EcyOd. Jpg'); for mm = 1:size(pic,1) for nn = 1:size(pic,2) if pic(mm,nn,1) 80 || pic(mm,nn,3) > 100 gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3); pic(mm,nn,:) = gsc gsc gsc; end end end imshow(pic).

Thanks, that's a lot easier way to do it. How did you get those coefficients (0.3, 0.59, 0.11) though? I don't understand that.

– Richard Knop Oct 31 '10 at 17:46 @Richard Knop: That's the formula used by RGB2GRAY, as listed in the documentation. – gnovice Oct 31 '10 at 17:48 @ Richard Knop: mathworks. Com/help/toolbox/images/ref/rgb2gray.

Html scroll a bit down until you reach paragraph 'Algorithm'. But there are others on the web as well. – zellus Oct 31 '10 at 17:49 Thanks very much.

– Richard Knop Oct 31 '10 at 17:57 Re: (0.3, 0.59, 0.11) The human visual system is much more sensitive to green than to red, more sensitive to red than blue. Those numbers are an approximation of the HVS's response across the visible spectrum. En.wikipedia.Org/wiki/Spectral_sensitivity – David Poole Oct 31 '107 at 14:05.

One option which greatly improves the quality of the resulting image is to convert to a different color space in order to more easily select your colors. In particular, the HSV color space defines pixel colors in terms of their hue (the color), saturation (the amount of color), and value (the brightness of the color). For example, you can convert your RGB image to HSV space using the function RGB2HSV, find pixels with hues that span what you want to define as "non-red" colors (like, say, 20 degrees to 340 degrees), set the saturation for those pixels to 0 (so they are grayscale), then convert the image back to RGB space using the function HSV2RGB: hsvImage = rgb2hsv(cdata); %# Convert the image to HSV space hPlane = 360.

*hsvImage(:,:,1); %# Get the hue plane scaled from 0 to 360 sPlane = hsvImage(:,:,2); %# Get the saturation plane nonRedIndex = (hPlane > 20) & ... %# Select "non-red" pixels (hPlane A note on selecting color ranges... One additional thing you can do which can help you select ranges of colors is to look at a histogram of the hues present in the pixels of your HSV image. Here's an example that uses the functions HISTC and BAR: binEdges = 0:360; %# Edges of histogram bins N = histc(hPlane(:),binEdges); %# Bin the pixel hues from above hBar = bar(binEdges(1:end-1),N(1:end-1),'histc'); %# Plot the histogram set(hBar,'CData',1:360,... %# Change the color of the bars using 'CDataMapping','direct',... %# indexed color mapping (360 colors) 'EdgeColor','none'); %# and remove edge coloring colormap(hsv(360)); %# Change to an HSV color map with 360 points axis(0 360 0 max(N)); %# Change the axes limits set(gca,'Color','k'); %# Change the axes background color set(gcf,'Pos',50 400 560 200); %# Change the figure size xlabel('HSV hue (in degrees)'); %# Add an x label ylabel('Bin counts'); %# Add a y label And here's the resulting pixel color histogram: Notice how the original image contains mostly red, green, and yellow colored pixels (with a few orange ones). There are almost no cyan, blue, indigo, or magenta colored pixels.

Notice also that the ranges I selected above (20 to 340 degrees) do a good job of excluding most everything that isn't a part of the two large red clusters at either end.

Thanks. I will try that. Meanwhile, I have updated my question.

Could you check it out? :) – Richard Knop Oct 31 '10 at 17:37 +1 for appreciating your solution. Superior in result and code.

– zellus Nov 1 '10 at 21:08.

I don't really know how matlab works so I can't really comment on the code, but maybe this will help explain a bit how RGB colors work. When using RGB colors a gray scale can be made by making sure the values for R,G and B are all the same. So basically what you want to do is detect if a pixel is red, when not just make R,G and B the same (you can use an average of the 3 for a rudimentary result).

Harder part is how to detect if a pixel is actually red, you can't just check if a pixel is high in the R value since it can still be another color, and a low R value can just mean a darker red. So you could do something like this: (I don't have matlab, so assuming syntax): red = cdata( y, x, 1 ); green = cdata( y, x, 2 ); blue = cdata(y, x, 3); if (red.

Thanks. I have changed my code a bit and I already get some output but the areas that should be red are white instead. Check my updated question.

– Richard Knop Oct 31 '10 at 17:33 Your colors are white because you removed the original values for green and blue for the pixels you want to keep. That's why in the example it only modifies the matrix for pixels you want to make gray and just leaves the rest alone. – Doggett Oct 31 '10 at 17:39 Kind of implemented your algorithm without noticing your post.

I hope you don't mind. – zellus Oct 31 '10 at 17:53 @Zellus Not at all, I see my total lack of knowledge of matlab made my example a bit longer than necessary anyhow ;) – Doggett Oct 31 '10 at 17:58.

You may wish to take a look at this. In your case, you can modify the code to keep certain colors instead. mathworks.com/matlabcentral/fileexchange....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions