Azzera filtri
Azzera filtri

Black pixels to white

13 visualizzazioni (ultimi 30 giorni)
M M
M M il 16 Giu 2011
Does anyone know a simple way to convert black pixels in a background to white in a jpg? The image is extremely simple with a solid black background and a solid red object. I just want to make the background white (other than doing it by hand in Photoshop since there will be many similar images).

Risposta accettata

Sean de Wolski
Sean de Wolski il 16 Giu 2011
%I is your image
M = repmat(all(~I,3),[1 1 3]); %mask black parts
I(M) = 255; %turn them white
This is only setting parts that are pure black to white, it could easily be modified to dark parts etc. with:
M = repmat(all(I<20,3),[1 1 3]); %mask values less than 20 in RGB
  1 Commento
M M
M M il 16 Giu 2011
Thank you so much! it worked perfectly

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 16 Giu 2011
What is the representation of the values? The technique would differ for RGB vs indexed
If you have an indexed image,
YourImage(YourImage==IndexOfBlack) = IndexOfWhite
This presumes that you only have a single index of black, e.g., the colors do not include "pure black", "so black you'd never see a thing with the human eye", "mega dark blue", "purple-ish quantum noise in a known-black area" and so on. If you can identify all the different black indices, then
YourImage(ismember(YourImage,BlackIndexList)) = IndexOfWhite;
Or, supposing that you have uint8 RGB and "red" to you is anything more than 1/4 strength red,
YourImage(YourImage(:,:,3)<(256/4),:) = [255,255,255]; %white

Matt Tearle
Matt Tearle il 16 Giu 2011
x = imread('street1.jpg');
figure
image(x)
idx = all(x==0,3);
x(repmat(idx,[1,1,3]))=255;
figure
image(x)
Assuming, here, that the image is m-by-n-by-3 (ie true color) and uint8. Change as necessary. Also, you might want to use < small_value, rather than == 0. Your call.

Community Treasure Hunt

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

Start Hunting!

Translated by