Black pixels to white

19 ビュー (過去 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
  1 件のコメント
M M
M M 2011 年 6 月 16 日
Thank you so much! it worked perfectly

サインインしてコメントする。

その他の回答 (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