How to change the pixel value of rgb unit8 image to NaN or black

7 views (last 30 days)
I have a rgb image, I want to change the pixel values greater than (r:175,g:255,b:55) to Nan or black . How can I do that. Please let me know Image info: I , uint8 0 255

Accepted Answer

Image Analyst
Image Analyst on 22 May 2013
% See where all channels exceed (175, 255, 55) and
% set image to black for those pixels. (untested)
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold each at the particular color
binaryRed = redChannel > 175;
binaryGreen = greenChannel > 255;
binaryBlue = blueChannel > 55;
% Find where all exceed threshold.
mask = binaryRed & binaryGreen & binaryBlue;
% Mask image to be black there.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
imshow(maskedRgbImage);
  1 Comment
Ankit Gupta
Ankit Gupta on 22 May 2013
Thanks a lot image analyst , I do have to change the ">" sign to "<" , it works fine for me.

Sign in to comment.

More Answers (2)

Alex Taylor
Alex Taylor on 22 May 2013
I think we would need some clarifying information to answer this question.
What does it mean to have a pixel value "greater" than (175,255,55)? If any one of the three channels is greater than your cutoff, do you want to clip to black? If all three channels are greater than your cutoff, do you want to clip to black?
Also, integer types do not define NaN, so you are going to need to use an integral value to represent black (e.g. 0).
Have you considered converting your image to grayscale before thresholding the intensity values? Have you considered thresholding in a different colorspace (working on the L channel in LAB space) then converting back?
  2 Comments
Ankit Gupta
Ankit Gupta on 22 May 2013
HI Alex, thanks, I want to change the values to black if all three channels are greater than the cutoff value . my requirements are not to change it to the gray scale, but the same rgb image. Thanks,
ankit

Sign in to comment.


Image Analyst
Image Analyst on 22 May 2013
Alex is right. We need the big picture here. WHY do you think you need to do that? What do you really want to do? It's possible that setting pixels to nan or black is not really the best approach to what you want to accomplish. So please share what you want to accomplish instead of focusing on what you think may be the best approach. Do you want to measure something (e.g. area, shape, or mean color) of objects of a particular color, or something else? You can upload your picture to http://snag.gy and tell us what you want to do or measure in your picture.
  9 Comments
Image Analyst
Image Analyst on 22 May 2013
I'm 99% certain you don't need to convert anything to nan or black. What do you think you'd do if that were done?
Ankit Gupta
Ankit Gupta on 22 May 2013
Edited: Ankit Gupta on 22 May 2013
Hi Image analyst,
I am just trying to figure out , how close I can get to find the shift in the loop position in the various images. I tried to crop them , convert to gray scale and correlate them but couldn't get relevant answer.Here I am trying to convert everything to black or Nan around and inside the loop and try to correlate it to the other images and see if I can find any shift information.
Thanks,
Ankit

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!