How I can change white color to red?

3 views (last 30 days)
Jon
Jon on 5 Feb 2013
I have an image and I would like to turn all white pixels to red. How can I do this?
Thanks
  2 Comments
Jan
Jan on 5 Feb 2013
"I have an image" is not clear. Is it an oilpainting, an indexed grayscale image as PNG, or an imported RGB array?

Sign in to comment.

Accepted Answer

Jan
Jan on 5 Feb 2013
Let me guess what your image is:
img = uint8(rand(640, 480, 3) * 255);
R = img(:, :, 1);
G = img(:, :, 2);
B = img(:, :, 3);
isWhite = R == 255 & B == 255 & G == 255;
G(isWhite) = 0;
B(isWhite) = 0;
newImg = cat(3, R, G, B);
I do not like this solution. Is there an "inplace" method which does not duplicate the data?
  2 Comments
Image Analyst
Image Analyst on 5 Feb 2013
Edited: Image Analyst on 5 Feb 2013
Is (253,254,251) still white? (BTW, that's what my response was asking.) If so, you can alter the formula to use different thresholds. In that example, you'd use >= instead of == and the new threshold values:
isWhite = R >= 253 & G >= 254 & B >= 251;

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Feb 2013
Define the range of values that encompass white? Is it only (255,255,255) or could it be more of the possible 16.7 million colors?

Tags

Community Treasure Hunt

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

Start Hunting!