| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
Contour Properties

Prev Tutorial: Contour Features
Next Tutorial: Contours : More Functions
Here we will learn to extract some frequently used properties of objects like Solidity, Equivalent Diameter, Mask image, Mean Intensity etc. More features can be found at Matlab regionprops documentation.

(NB : Centroid, Area, Perimeter etc also belong to this category, but we have seen it in last chapter)

# 1. Aspect Ratio

It is the ratio of width to height of bounding rect of the object.

\[Aspect \; Ratio = \frac{Width}{Height}\]

x,y,w,h = cv.boundingRect(cnt)
aspect_ratio = float(w)/h
cv::boundingRect
Rect boundingRect(InputArray array)
Calculates the up-right bounding rectangle of a point set or non-zero pixels of gray-scale image.

# 2. Extent

Extent is the ratio of contour area to bounding rectangle area.

\[Extent = \frac{Object \; Area}{Bounding \; Rectangle \; Area}\]

area = cv.contourArea(cnt)
x,y,w,h = cv.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area
cv::contourArea
double contourArea(InputArray contour, bool oriented=false)
Calculates a contour area.

# 3. Solidity

Solidity is the ratio of contour area to its convex hull area.

\[Solidity = \frac{Contour \; Area}{Convex \; Hull \; Area}\]

area = cv.contourArea(cnt)
hull = cv.convexHull(cnt)
hull_area = cv.contourArea(hull)
solidity = float(area)/hull_area
cv::convexHull
void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true)
Finds the convex hull of a point set.

# 4. Equivalent Diameter

Equivalent Diameter is the diameter of the circle whose area is same as the contour area.

\[Equivalent \; Diameter = \sqrt{\frac{4 \times Contour \; Area}{\pi}}\]

area = cv.contourArea(cnt)
equi_diameter = np.sqrt(4*area/np.pi)

# 5. Orientation

Orientation is the angle at which object is directed. Following method also gives the Major Axis and Minor Axis lengths.

(x,y),(MA,ma),angle = cv.fitEllipse(cnt)
cv::fitEllipse
RotatedRect fitEllipse(InputArray points)
Fits an ellipse around a set of 2D points.

# 6. Mask and Pixel Points

In some cases, we may need all the points which comprises that object. It can be done as follows:

mask = np.zeros(imgray.shape,np.uint8)
cv.drawContours(mask,[cnt],0,255,-1)
pixelpoints = np.transpose(np.nonzero(mask))
#pixelpoints = cv.findNonZero(mask)
cv::drawContours
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.

Here, two methods, one using Numpy functions, next one using OpenCV function (last commented line) are given to do the same. Results are also same, but with a slight difference. Numpy gives coordinates in (row, column) format, while OpenCV gives coordinates in (x,y) format. So basically the answers will be interchanged. Note that, row = y and column = x.

# 7. Maximum Value, Minimum Value and their locations

We can find these parameters using a mask image.

min_val, max_val, min_loc, max_loc = cv.minMaxLoc(imgray,mask = mask)
cv::minMaxLoc
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
Finds the global minimum and maximum in an array.

# 8. Mean Color or Mean Intensity

Here, we can find the average color of an object. Or it can be average intensity of the object in grayscale mode. We again use the same mask to do it.

mean_val = cv.mean(im,mask = mask)
cv::mean
Scalar mean(InputArray src, InputArray mask=noArray())
Calculates an average (mean) of array elements.

# 9. Extreme Points

Extreme Points means topmost, bottommost, rightmost and leftmost points of the object.

leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

For eg, if I apply it to an Indian map, I get the following result :

image

# Exercises

1. There are still some features left in matlab regionprops doc. Try to implement them.