Extract all red pixels from image to binary image

조회 수: 8 (최근 30일)
Philip
Philip 2011년 12월 7일
Does anybody know of a way to extract all 'red' values [255 0 0] from an image to make a binary image where 1's indicate the locations of red pixels?
I have done this with a loop, but I'm sure there is a much quicker way to do it...
Here's my loop, in case it clears up the objective:
for m = 1:size(img,1)
for n = 1:size(img,2)
if img(m,n,1) == 255 && img(m,n,2) == 0 && img(m,n,3) == 0
HaloRegion_Map(m,n) = 1;
end
end
end
Thanks for your help

답변 (2개)

Walter Roberson
Walter Roberson 2011년 12월 7일
HaloRegion_Map = img(:,:,1) == 255 & img(:,:,2) == 0 && img(:,:,3) == 0;

Sean de Wolski
Sean de Wolski 2011년 12월 7일
X = uint8(rand(100,100,3)>.5)*255; %sample image with some red
red = all(bsxfun(@eq,X,reshape([255 0 0],[1 1 3])),3); %logical image of red parts
And, for fun, a speed comparison:
X = uint8(rand(500,500,3)>.5)*255;
img = X;
t1 = 0;
t2 = t1;
for ii = 1:100
tic
red = all(bsxfun(@eq,X,reshape([255 0 0],[1 1 3])),3);
t1 = t1+toc;
tic
HaloRegion_Map = (img(:,:,1) == 255) & ~img(:,:,2) & ~img(:,:,3);
t2 = t2+toc;
end
[t1 t2]
ans = 0.3921 0.6233

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by