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

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

 채택된 답변

Image Analyst
Image Analyst 2013년 5월 22일
% 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

Thanks a lot image analyst , I do have to change the ">" sign to "<" , it works fine for me.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Alex Taylor
Alex Taylor 2013년 5월 22일

0 개 추천

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

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
See my code in my second answer.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2013년 5월 22일

0 개 추천

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

Ankit Gupta
Ankit Gupta 2013년 5월 22일
편집: Image Analyst 2013년 5월 22일
Hi Alex and Image analyst,
I am trying to find the changes in the image as I asked earlier especially in loop position. I used gray images but was not successful in finding the change. here are the images
I want to find the changes or shift in the loop position , I tried many different ways, but its seems the image quality is not good as of now I cant change the camera and its position. I tried cropping the image and using normxcorr2 but dint get any relevant result.
Well certainly you can at least focus the lens.
Ankit Gupta
Ankit Gupta 2013년 5월 22일
편집: Ankit Gupta 2013년 5월 22일
Hi Image analyst, Thats the maximum what I can focus , these are just usb web camera with fixed focus. I am trying to only visualize the loop and see if the normxcorr2 works or not, hence want to fill everything except the loop with either black or Nan and see if the correlation works with other image to find the shift in the loop position
Then buy a better camera. You'll spend more in time trying to fix the focus problem that if you just bought a camera with a lens you can focus.
Ankit Gupta
Ankit Gupta 2013년 5월 22일
편집: Ankit Gupta 2013년 5월 22일
Hi Image analyst, Buying a new camera is not in my hand, we might get new cameras but not sure when, but for the time being is it possible to convert those pixel value greater than the cutoff limit to black or 0.I am able to put Nan in a gray scale image but dont know how to do in rgb image which is unit8. Please let me know.
You cannot put a NaN in any image whose datatype is uint8. The value NaN is not defined in integer datatypes, only floating point datatypes (e.g. double, single).
One way to think about this is the following piece of code in which I attempt to cast the value NaN to uint8 in MATLAB:
uint8(NaN)
In MATLAB, we cast NaN to 0 when casting to integer types by convention (because NaN isn't defined in uint8).
Hi Alex,
thanks as you said earlier, how should i convert it black or put 0 in it. I tried this code, but only got few black points rather then the whole area of black.
>> sel = bsxfun( @eq, I, permute( [175, 255, 55], [1 3 2] ) );
>> figure;I( sel(:,:,[1 1 1]) ) = NaN;
Using uint8(NaN) didn't display anything but the figure window with nothing in it. Is it possible to just put 0 for pixel value greater than the cutof value
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 2013년 5월 22일
편집: Ankit Gupta 2013년 5월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by