Extract all red pixels from image to binary image
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 7 Dec 2011
HaloRegion_Map = img(:,:,1) == 255 & img(:,:,2) == 0 && img(:,:,3) == 0;
0 Comments
Sean de Wolski
on 7 Dec 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
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!