Black pixels to white

조회 수: 16 (최근 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