如何检测仅具有红色物体的图像中的边缘

时间:2015-04-28 14:33:05

标签: image matlab image-processing

我有一张图像,在该图像中检测到所有红色物体。

以下是两张图片的示例:

http://img.weiku.com/waterpicture/2011/10/30/18/road_Traffic_signs_634577283637977297_4.jpg

但是当我继续使用该图像进行边缘检测时,我得到的输出只有黑色。但是,我想检测那个红色物体的边缘。

r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(r,rgb2gray(im));
bw=im2bw(diff,0.18);
area=bwareaopen(bw,300);
rm=immultiply(area,r);  gm=g.*0;  bm=b.*0;
image=cat(3,rm,gm,bm);
axes(handles.Image);
imshow(image);

I=image;
Thresholding=im2bw(I);

axes(handles.Image);
imshow(Thresholding)

fontSize=20;
edgeimage=Thresholding;
BW = edge(edgeimage,'canny');
axes(handles.Image); 
imshow(BW);

2 个答案:

答案 0 :(得分:8)

当您申请im2bw时,您只想使用I的红色频道(即第一频道)。因此使用此命令:

Thresholding =im2bw(I(:,:,1));

例如产生此输出:

enter image description here

答案 1 :(得分:0)

对于那些设法在这里绊倒的人来说,仅供参考。 HSV色彩空间更适合检测RGB色彩空间上的颜色。一个很好的例子是gnovice的answer。主要原因是有些颜色可以包含完整的255个红色值但实际上不是红色(黄色可以由(255,255,0)形成,白色来自(255,255,255),品红色来自(255,0,255)等等) )。

我在下面修改了他的代码:

cdata = imread('roadsign.jpg');

hsvImage = rgb2hsv(cdata);         %# Convert the image to HSV space
hPlane = 360.*hsvImage(:,:,1);     %# Get the hue plane scaled from 0 to 360
sPlane = hsvImage(:,:,2);          %# Get the saturation plane
bPlane = hsvImage(:,:,3);          %# Get the brightness plane

% Must get colors with high brightness and saturation of the red color
redIndex = ((hPlane <= 20) | (hPlane >= 340)) & sPlane >= 0.7 & bPlane >= 0.7;

% Show edges
imshow(edge(redIndex));

输出: enter image description here

相关问题