MATLAB Answers


How I can change white color to red?

Asked by 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?



Jan Simon
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?

on 5 Feb 2013

Is a RGB image.



No products are associated with this question.

2 Answers

Answer by Jan Simon
on 5 Feb 2013
 Accepted answer

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?


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;

Answer by 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?


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!