Extract all red pixels from image to binary image

6 ビュー (過去 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 Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by