# Counting the number of objects intersected by the image border in MATLAB

I have an RGB image with various signs. My main goal is to count the signs that are in contact with the image borders.

## Approach and problem

I started by loading the image [Fig. 1], then converted it to grayscale and applied a median filter to get rid of some noise [Fig. 2]. Then I binarized it with a threshold of 0.2, which resulted in Figure 3. At this time I got my binarized image, but the problem is that some parts that belong to the same sign are appearing in various regions, instead of only one. Now my goal is to merge the regions that belong to the same object , so then I could use bwlabel to count how many signs are in the image, and use imclearborder to get rid of the ones in the border, and use bwlabel again to get the difference between the two.

My approach was to use bwmorph, Dilate to dilate the objects and then try to fill them with imfill, holes. But the problem is that if I dilate them in a small amount[Fig. 4], the imfill doesn't seem to fill them, if I dilate them by a big amount[Fig 5] all the objects start to merge :(

# Code

img=im2double(imread('image.png')); figure, imshow(img)
img_gray=rgb2gray(img); imshow(img_gray);
img_mediana=medfilt2(img_gray, [3 3]); figure, imshow(img_mediana);
img_bin=im2bw(img_mediana, 0.2); imshow(img_bin)
img_dilate=bwmorph(img_bin, 'Dilate', 10); imshow(img_dilate)
img_fill=imfill(img_dilate, 'Holes'); figure, imshow(img_fill)


# Figures

Fig 1:

Fig 2:

fig 2 http://dl.dropbox.com/u/5272012/2.png

Fig 3:

Fig 4:

Fig 5:

fig 5 http://dl.dropbox.com/u/5272012/5.png

• My question is what exactly tells you that sign is broken? How do you want to really put the output? i mean - do you just want Colorize all signs which are cut? or do you really want to list each sign and classify cut/full? – Dipan Mehta May 16 '12 at 12:59

## 2 Answers

In my opinion, dilation erosion are very basic tools. You have a very strong information base and quite a decent input image to make such decisions.

Here is my view:

1. Given a reasonable success you have shown to go from Fig 1 to 3, you can identify and segment individual signs.

2. Assuming that you have known signs a prior, you can apply a decent fast algorithms to do pattern matching. In case if the exact patterns are not known- you can just identify the outer shape of the pattern.

3. Based on the classification, you can always define the centroid of each matched pattern and its respective width and height. If the centroid X,Y position is too close to the border - i.e. $centroid(x) < 0$ or $centroid(x) > imagewidth - shapewidth$ it is outside the edge, similarly you can apply for Y axis as well.

4. Given that you are only concerned about what falls on the edge - you should start with each edge only and start pattern matching there. Start matching the partial pattern/shape and if the partial pattern/shape does match that object IS being cut on the edge.

Here are some references that might help you formulate the problem well.

This paper is a very good to understand lot of basics about Signs/tokens you are dealing with.

Anil K. Jain and Aditya Vailaya Shape-Based Retrieval: A Case Study with Trademark Image Databases Pattern recognition 1998, vol. 31, no9, pp. 1369-1390

There are many research elements which deals with partial or occluded shape/pattern matching.

Eli Saber,Yaowu Xu, A. Murat Tekalp Partial shape recognition by sub-matrix matching for partial matching guided image labeling Pattern Recognition 38 (2005) 1560 – 1573

Will expand this answer for more specific queries if you take this approach.

• Hey! I can't use pattern matching because that will take a very long time and too much computational work. Also the scale of the objects(signs), can be variable(the professor give us random images where the scale f the signs can very from +30% to -30%, so a pattern matching is useless. I need a faster approach in order to solve this one. – Rui Trovisco May 16 '12 at 12:53
• @RuiTrovisco I understand this. Which is why i kind of wrote - i would improve the answer based on your feedback. I have put some comments on your question. Please revert there. – Dipan Mehta May 16 '12 at 12:58

Here is a little inspiration showing the opposite of what you are seeking.

Start with fig3.

% Find background
labels = bwlabel(~fig3);
[n,idx] = hist(labels(:),0:max(labels(:)));
[bgrSize bgrLableIdx] = max(n);
bgr = (labels == idx(bgrLableIdx));
bgr = imopen(bgr,strel('disk',3));

% Remove border objects and cleanup
borderCleared = imclearborder(~bgr);
borderCleared = imopen(borderCleared,strel('disk',3));