Black pixels to white
17 views (last 30 days)
Show older comments
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).
0 Comments
Accepted Answer
Sean de Wolski
on 16 Jun 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
More Answers (2)
Walter Roberson
on 16 Jun 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
0 Comments
Matt Tearle
on 16 Jun 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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!