Detecting Objects' Shape(III) : Triangle Object

This example illustrates a simple method in detecting triangle object. This method is slightly different from the methods used for round object and square object detection in which the area and the perimeter of the objects are used to calculate the "score" of similarity. Instead, this method uses the "extrema" points of an object to determine whether the object is triangle.

1. Original image and the negative image (value 0 represent background while 1 represents object).

S = imread('pic11.jpg');
S = im2bw(S);
S2 = ~S;
imshow(S2);



2. Labeling the objects in the image. The advance programming language will support this function and label the objects.

S3 = bwlabel(S2);
imshow(S3,[]);

3. In this example of triangle object detection, only one object property is extracted --> "Extrema".

S4 = regionprops(S3,'Extrema','Centroid');

The “Centriod” is just used for labeling purpose. Following figure shows what “Extrema” of an object is:
4. Ideally, a triangle will only have 3 extrema points. However, due to the imperfection of a binary object, a few of close-by points might exist instead of one.
For example eight extrema points of a triangle are: (135.5,11.5),(136.5,11.5),(157.5,45.5),(157.5,47.5),(157.5,47.5),(112.5,4 7.5),(112.5,47.5),(112.5,45.5) first 2 points are referring to the upper corner of the triangle but the values are different. This applied to point 3 to 5 and points 6-8 too.

In order to solve this problem, we need to cluster the points to their group based on the location of the points. Different ways could be used for clustering but since our topic is about image processing, image processing method will be used to group the similar points together.

In this method, a morphology technique --> dilation will be used. Firstly, the extrema points will be used to construct an image. The coordinates of the extrema points will be converted to points in an image. Let's look into one of the object for illustration:

a. Create morphological structuring element.

se = strel('disk',5);

b. Round the extrema points for 5th object (pixel value must be integer)

temp = round(S4(5).Extrema);

c. Remap the coordinates by referring to the minimum values of x and y

temp(:,1) = temp(:,1) - min(temp(:,1)) + 1;
temp(:,2) = temp(:,2) - min(temp(:,2)) + 1;

d. Create a zero matrix (black image)

mask = zeros(max(temp(:,1)),max(temp(:,2)));

e. Convert the coordinates to "objects" in the black image

for cnt2 = 1:8
mask(temp(cnt2,1),temp(cnt2,2))=1;
end
imagesc(mask);

f. Do note that some of the points are close together; use the image dilation technique to group close-by points.

mask2 = imdilate(mask,se);
imagesc(mask2);

g. Count number of real extrema points in the image

[labeled,numObjects] = bwlabel(mask2,8);
disp(numObjects);

3

5. The following codes perform calculation for all objects

se = strel('disk',5);
for cnt = 1:length(S4)
temp = round(S4(cnt).Extrema);
temp(:,1) = temp(:,1) - min(temp(:,1)) + 1;
temp(:,2) = temp(:,2) - min(temp(:,2)) + 1;
mask = zeros(max(temp(:,1)),max(temp(:,2)));
for cnt2 = 1:8
mask(temp(cnt2,1),temp(cnt2,2))=1;
end
mask2 = imdilate(mask,se);
[labeled,numObjects(cnt)] = bwlabel(mask2,8);
end

disp(numObjects);
5 3 4 6 3 4

score = (numObjects == 3);

6. Display the result visually

imshow(S2)
for cnt = 1:length(S4)
text(S4(cnt).Centroid(1),S4(cnt).Centroid(2),num2str(score(cnt)),'color','red');
end