one color retention in matlab......

1 view (last 30 days)
mishaal fatima
mishaal fatima on 7 Dec 2014
Commented: Image Analyst on 9 Dec 2014
i got this got from internet but i cant understand it very well therefore i cant change it. this code convert the given image into grayscale except red colored pixels...... now i want to change this code for blue,green,yellow and other colors if possible but i dont know how to change it..... plz can anyone help me.....
figure
pic = imread('147121,xcitefun-barbie-dolls-11.jpg');
imshow(pic);
for mm = 1:size(pic,1)
for nn = 1:size(pic,2)
if pic(mm,nn,1) < 80 || pic(mm,nn,2) > 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)
  2 Comments
Geoff Hayes
Geoff Hayes on 7 Dec 2014
Mishaal - the line of code that is "important" here is
if pic(mm,nn,1) < 80 || pic(mm,nn,2) > 80 || pic(mm,nn,3) > 100
which is just saying that if the RGB pixel has a blue component that is less than 80, OR a green component greater than 80, or a red component greater than 100, then this pixel is NOT considered to be red and so should be converted to grayscale using the equation
gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3);
Why the author of the code chose 80, 80, and 100 is curious, though Image Analyst would know more about these choices.
If you got this code from http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color, then you may want to take a look at the answer that uses HSV, as the result seems to be better and you may be able to research more on why the author chose that method.
mishaal fatima
mishaal fatima on 8 Dec 2014
i tried HSV color space, its results are a lot better but i dont know how to change the rest of the image into gray scale..... can u help?
figure pic = imread('color_wheel2.jpg'); imshow(pic); hsvImage = rgb2hsv(pic); hPlane = 360*hsvImage(:,:,1); sPlane = hsvImage(:,:,2);
%RED noncolorIndex = (hPlane > 20) & (hPlane < 340) ; %sPlane(noncolorIndex) = 0;
hsvImage(:,:,2) = sPlane; rgbImage = hsv2rgb(hsvImage); imshow(rgbImage)

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 7 Dec 2014
Color segmentation in RGB color space is not recommended unless you have strict control over your lighting and exposure. You're must better doing it in HSV color space. Or you could do it with CIE LAB or using delta E. I have color segmentation demos in my File Exchange and I suggest you use the one that uses HSV. To select different colors you just need to threshold on the hue channel. The demos give you a mask of the desired color. If you want to set everything outside of there to gray version of the original or some other constant, uniform color (black, white, purple, whatever), then it's not too hard, but let me know.
  5 Comments
Image Analyst
Image Analyst on 9 Dec 2014
Edited: Image Analyst on 9 Dec 2014
Well you did something wrong in your second, desired image. You've changed the value channel also - it's no longer a constant. Here is my code:
Image Analyst
Image Analyst on 9 Dec 2014
If you really want to change the v channel also (which I don't recommend) see the lines of code I added to change the V channel to the rgb2gray() version of the RGB image.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!