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

how to detect edges in an image having only red object

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

这是一个包含两张图片的示例:

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);

当您应用 im2bw 时,您只想使用 I 的红色通道(即第一个通道)。因此使用这个命令:

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

例如产生这个输出:

仅供参考,供任何设法在这里跌跌撞撞的人参考。 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));

输出: