OpenCV
3.4.17
Open Source Computer Vision
|
Prev Tutorial: Contour Properties
Next Tutorial: Contours Hierarchy
In this chapter, we will learn about
We saw what is convex hull in second chapter about contours. Any deviation of the object from this hull can be considered as convexity defect.
OpenCV comes with a ready-made function to find this, cv.convexityDefects(). A basic function call would look like below:
It returns an array where each row contains these values - [ start point, end point, farthest point, approximate distance to farthest point ]. We can visualize it using an image. We draw a line joining start point and end point, then draw a circle at the farthest point. Remember first three values returned are indices of cnt. So we have to bring those values from cnt.
And see the result:
This function finds the shortest distance between a point in the image and a contour. It returns the distance which is negative when point is outside the contour, positive when point is inside and zero if point is on the contour.
For example, we can check the point (50,50) as follows:
In the function, third argument is measureDist. If it is True, it finds the signed distance. If False, it finds whether the point is inside or outside or on the contour (it returns +1, -1, 0 respectively).
OpenCV comes with a function cv.matchShapes() which enables us to compare two shapes, or two contours and returns a metric showing the similarity. The lower the result, the better match it is. It is calculated based on the hu-moment values. Different measurement methods are explained in the docs.
I tried matching shapes with different shapes given below:
I got following results:
See, even image rotation doesn't affect much on this comparison.