2

I have an image, in that image all red objects are detected.

Here's an example with two images:

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

But when i proceed that image for edge detection method i got the output as only black color. However, I want to detect the edges in that red object.

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);
rayryeng
  • 96,704
  • 21
  • 166
  • 177
bk sumedha
  • 63
  • 1
  • 8

2 Answers2

8

When you apply im2bw you want to use only the red channel of I(i.e the 1st channel). Therefore using this command:

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

for example yields this output:

enter image description here

Benoit_11
  • 13,785
  • 2
  • 22
  • 35
0

Just FYI for anyone else that manages to stumble here. The HSV colorspace is better suited for detecting colors over the RGB colorspace. A good example is in gnovice's answer. The main reason for this is that there are colors which can contain full 255 red values but aren't actually red (yellow can be formed from (255,255,0), white from (255,255,255), magenta from (255,0,255), etc).

I modified his code for your purpose below:

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

Output: enter image description here

Community
  • 1
  • 1
Justin
  • 4,529
  • 2
  • 33
  • 53