我要评分
获取效率
正确性
完整性
易理解

contourArea

Usage

Calculates the area of a contour. Contour is a curve joining all the continuous points along the object boundary in an image.

Interface

1
double contourArea(InputArray contour, bool oriented = false);

Parameters

Parameter

Description

Value Range

Input/Output

contour

Input contour.

Not null, single channel

Input

oriented

Returned area. The default value is false.

  • true: The returned area is signed to determine the orientation of the contour (clockwise or anticlockwise).
  • false: The returned area is an absolute value.

Input

Return Value

  • Contour area

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Create a binary image.
    cv::Mat src = (cv::Mat_<uint8_t>(5, 5) << 0, 0, 0, 0, 0,
                                              0, 255, 255, 255, 0,
                                              0, 255, 0, 255, 0,
                                              0, 255, 255, 255, 0,
                                              0, 0, 0, 0, 0);

    // Find contours.
    std::vector<std::vector<cv::Point>> contours;
    cv::findContours(src, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

    // Calculate the area of the first contour.
    double area = cv::contourArea(contours[0]);

    std::cout << "Contour_area: " << area << std::endl;

    return 0;
}

Output:

1
Contour area: 4.0