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

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?

Thanks

2 Comments

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?

Jon on 5 Feb 2013

Is a RGB image.

Jon

Tags

Products

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?

2 Comments

Jon on 5 Feb 2013

Thanks

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

0 Comments

Image Analyst

Contact us