# How to filter objects on basis of orientation of regionprops

I am trying to detect straight vertical lines in an image. I used vertical edge detection and then I tried to check which objects has the orientation > 80 & < 100.

Input Image:

Code:

% Vertical Edge Detection

v = edge(img,'sobel','vertical');
v = bwlabel(v);
stats = regionprops(v, 'All');

% ids = find([stats.Orientation] > 80 & [stats.Orientation] < 100);
% v = ismember(v,ids);

figure,imshow(v);title('Vertical');


Problem:

The problem is that most of the objects are having some weird orientation (some -ve value e.g. -53.xyz)

Output:

when I try to filter the objects, it gives me a full black image as an output.

Someone please explain this behavior. How can I get only straight vertical lines?

I don't have MATLAB, but here are a few ideas:

Since you use regionprops, you could use the Orientation parameter - which is pretty much exactly what you want to have.

Another idea

• Create an matrix of the same size as your image. The matrix has entries that give a linear variation in the perpendicular orientation that you desire (it basically is a gradient field in the perpendicular orientation, e.g. with values ranging from 0 to 1).

• Use bwlabel to label all your candidate lines

• Loop over all labels and create a binary mask for each label. Multiply the mask with the gradient matrix.

• Calculate the variance inside the mask. Since the gradient is perpendicular to your direction, the variance should be 0 for a perfect line. For a not-so-perfect-line, there is a certain variation. You could use

• an empirical threshold
• check the math for an more elaborate threshold, because one can certainly find a function $\sigma^2(\alpha)$, with $\alpha$ being the angle of the region of interest in the mask.

The Sobel filter you used bases on the gradient of the image. It seems (I am not using Matlab) you specified to "find" the "objects" that have significant horizontal gradient (so: the most vertical). But -- as you see -- not only the vertical lines have horizontal gradient...

I am not able to give you the complete answer, as I do not know Matlab's image processing toolbox, but maybe the hint will be useful: Try to find the horizontal lines in the same way. This will give you the skewed ones as well, but not the vertical ones. Once you subtract the 'horizontal plus skewed' set from 'vertical plus skewed' objects, the only remaining will be pure vertical lines. It will be much easier then to find the vertical ones.

This will not be just simple subtract operations -maybe some morphological operations will be necessary.

Are you sure you are using ismember correctly?

It looks like ismember returns a boolean array... so plotting it might not be giving you what you intend.

Can you loop through stats and just pull out the appropriate entries in v?

At least try plotting the stats.Centroid for all the objects with the right orientation, to see if that's giving what you expect.