Black pixels to white

17 次查看(过去 30 天)
M M
M M 2011-6-16
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).

采纳的回答

Sean de Wolski
Sean de Wolski 2011-6-16
%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

更多回答(2 个)

Walter Roberson
Walter Roberson 2011-6-16
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 2011-6-16
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