Extract all red pixels from image to binary image

8 vues (au cours des 30 derniers jours)
Philip
Philip le 7 Déc 2011
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

Réponses (2)

Walter Roberson
Walter Roberson le 7 Déc 2011
HaloRegion_Map = img(:,:,1) == 255 & img(:,:,2) == 0 && img(:,:,3) == 0;

Sean de Wolski
Sean de Wolski le 7 Déc 2011
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by