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.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.
We use the function: cv.convexityDefects (contour, convexhull, convexityDefect)
contour | input contour. |
convexhull | convex hull obtained using convexHull that should contain indices of the contour points that make the hull |
convexityDefect | the output vector of convexity defects. Each convexity defect is represented as 4-element(start_index, end_index, farthest_pt_index, fixpt_depth), where indices are 0-based indices in the original contour of the convexity defect beginning, end and the farthest point, and fixpt_depth is fixed-point approximation (with 8 fractional bits) of the distance between the farthest contour point and the hull. That is, to get the floating-point value of the depth will be fixpt_depth/256.0. |
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.
We use the function: cv.pointPolygonTest (contour, pt, measureDist)
contour | input contour. |
pt | point tested against the contour. |
measureDist | if true, the function estimates the signed distance from the point to the nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not. |
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.
We use the function: cv.matchShapes (contour1, contour2, method, parameter)
contour1 | first contour or grayscale image. |
contour2 | second contour or grayscale image. |
method | comparison method, see cv::ShapeMatchModes |
parameter | method-specific parameter(not supported now). |