Contour Properties#

Goal#

  • Here we will learn to extract some frequently used properties of objects like Solidity, Equivalent Diameter, Mask image, Mean Intensity etc.

1. Aspect Ratio#

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

\[ Aspect \; Ratio = \frac{Width}{Height} \]
let rect = cv.boundingRect(cnt);
let aspectRatio = rect.width / rect.height;

2. Extent#

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

\[ Extent = \frac{Object \; Area}{Bounding \; Rectangle \; Area} \]
let area = cv.contourArea(cnt, false);
let rect = cv.boundingRect(cnt));
let rectArea = rect.width * rect.height;
let extent = area / rectArea;

3. Solidity#

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

\[ Solidity = \frac{Contour \; Area}{Convex \; Hull \; Area} \]
let area = cv.contourArea(cnt, false);
cv.convexHull(cnt, hull, false, true);
let hullArea = cv.contourArea(hull, false);
let solidity = area / hullArea;

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}} \]
let area = cv.contourArea(cnt, false);
let equiDiameter = Math.sqrt(4 * area / Math.PI);

5. Orientation#

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

let rotatedRect = cv.fitEllipse(cnt);
let angle = rotatedRect.angle;

6. Mask and Pixel Points#

In some cases, we may need all the points which comprises that object.

We use the function: cv.transpose (src, dst)

Parameters

src

input array.

dst

output array of the same type as src.

7. Maximum Value, Minimum Value and their locations#

We use the function: cv.minMaxLoc(src, mask)

Parameters

src

input single-channel array.

mask

optional mask used to select a sub-array.

let result = cv.minMaxLoc(src, mask);
let minVal = result.minVal;
let maxVal = result.maxVal;
let minLoc = result.minLoc;
let maxLoc = result.maxLoc;

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.

We use the function: cv.mean (src, mask)

Parameters

src

input array that should have from 1 to 4 channels so that the result can be stored in Scalar.

mask

optional operation mask.

let average = cv.mean(src, mask);