Reputation: 1305
I am working on an application to recognize line-like features inside circle/elliptical shapes. The shapes look like the following (two shown here):
Each object (of 100+) will be captured individually by video; the capture is a manual/physical process (i.e. I am holding the camera each time). I have full control over the camera so I can position it consistently for each capture.
Right now I'm trying to use OpenCV to do the recognition. I was able to modify the sample 'face recognition' app to use another Haar identifier XML file, but this seems to only handle detection of the outside circle/ovals.
I am interested in generating an object per sample, to describe the 5 interior lines for further processing:
{
1: { length: 20, avg_thick: 2.3 },
2: { length: 4, avg_thick: 2.0 },
3: { length: 9.1, avg_thick: 2.1 },
4: { length: 2, avg_thick: 1.9 },
5: { length: 17, avg_thick: 2.1 }
}
This is my first project involving image recognition. What algorithms or procedure should I use/research to achieve this? Thanks!
UPDATE:
Since the images will be photographed by hand, they are not pure black/white. Trying to apply thresholding makes the (feint) lines inside the shapes sometimes disappear. How could I improve the thresholding results?
Upvotes: 4
Views: 1544
Reputation: 20287
If the lines are approximately straight, use the Hough transform to find all lines, and the circle version of the Hough transform to find all circles/ellipses (you can then check is the bounding circle/ellipse has been found, and which lines are inside it, for example).
If the lines are not straight: you mean "narrow elongated regions" not lines, right? :) You'd have to skeletonize (probably threshold first). Helpful tutorial: "Skeletonization using OpenCV-Python". Since you need the width (=distance from skeleton to the edges) as well, use skimage.morphology.medial_axis(..., return_distance=True). You will likely also need some way to go through the branches of each skeleton and trim short branches (there is nothing off-the-shelf that already does that, sorry).
Haar type approach won't work at all, it only works (even theoretically) with features that have fixed relative position and shape. You want some kind of geometric feature extraction algorithm, not image recognition.
EDIT: Sample code in python:
import numpy, scipy, scipy.ndimage, skimage.morphology, matplotlib.pyplot
img = scipy.ndimage.imread("test.png")
# quick and dirty threshold
binary_img = img[:,:,0] < 0.1
# skeletonize
skel_binary, skel_distances = skimage.morphology.medial_axis(binary_img, return_distance=True)
# find individual lines
structure_element = scipy.ndimage.generate_binary_structure(2,2)
skel_labels, num_skel_labels = scipy.ndimage.measurements.label(skel_binary, structure=structure_element)
for n in range(1, num_skel_labels + 1):
# make a binary label for this line
line = (skel_labels == n)
# calculate width from skeleton
mean_width = 2 * numpy.mean( skel_distances[ line ] )
print "line %d: width %f" % (n, mean_width)
# you need some way to find the ends of a line
# perhaps the most distant pair of points?
# show the labels
# the circle is also labelled
# you need some way to check which label is the circle and exclude that
matplotlib.pyplot.imshow(skel_labels)
matplotlib.pyplot.show()
This produces reasonable results on the images you posted above, and also (to check line thickness works) on a version of those images scaled up 10x. It doesn't deal with intersecting lines, maybe you can do a graph algorithm for that. Also you do need to exclude the outside circle somehow (it does seem to be always n=1 because labelling happens from the top-left and the first labelled area it finds is the circle).
EDIT: How to (or whether to) threshold is an interesting question. You can try automatic thresholding, perhaps based on Otsu's method, or based on gaussian mixture (example). I think you may likely get best results with some kind of statistical model of the background and foreground color and brightness, combined with locally adaptive thresholding. Really depends on the nature of your images.
Upvotes: 6
Reputation: 479
Upvotes: 1