Basic properties of profile

Posted by Warzone RTS on Wed, 26 Jan 2022 06:53:44 +0100

Basic properties of profile

the measure of area

brief introduction

  • Contour area refers to the area enclosed by all pixels in each contour, in pixels
  • Through the size of the contour area, we can further analyze the implicit information of each contour, such as distinguishing the size of objects and identifying different objects through the contour area

API

OpenCV4 provides the contourArea() function to count the area enclosed by contour pixels

double cv::contourArea(InputArray contour, bool oriented = false)
  • Contour: the pixel point of the contour. The data type is vector < point > or Mat
  • oriented: whether the area has directionality. true indicates that the area has directionality, false indicates that the area does not have directionality, and the default is false
    • When the parameter is true, it indicates that the statistical area is directional. When the contour vertex is given clockwise and counterclockwise, the statistical area is opposite to each other; When the parameter value is false, it means that the statistical area does not have directionality, and the absolute value of contour area is output

Supplement: the polygon area formed by connecting two adjacent pixels one by one is the statistical area of contour area. The connecting line between three consecutive pixels may be on the same straight line. Therefore, in order to reduce the number of input contour pixels, you can only input the vertex pixels of the contour, such as the contour of a triangle. The contour may have all pixels on each edge. However, when counting the area, you can only input three vertices of the triangle.

Example code:

In this program, the three vertices of the contour of a right triangle and the midpoint of the hypotenuse are given. The calculated contour area is equal to the area of the triangle

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
    // The triangle outline is represented by 4 points
    vector<Point> contour;
    contour.push_back(Point2f(0, 0));
    contour.push_back(Point2f(10, 0));
    contour.push_back(Point2f(10, 10));
    contour.push_back(Point2f(5, 5));
    double area = contourArea(contour);
    cout << "area = " << area << endl;

    Mat_<Vec3b> img = imread("/home/kslas/OpenCV/erode.png");
    Mat_<uchar> gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);   // Convert to grayscale image
    GaussianBlur(gray, gray, Size(9, 9), 2, 2);  // Smooth filtering
    Mat_<uchar> binary;
    threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);  // Adaptive binarization

    vector<vector<Point>> contours;   // outline
    vector<Vec4i> hierarchy;   // Store contour structure variables
    findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < contours.size(); i++)
    {
        double area1 = contourArea(contours[i]);
        cout << "The first" << i << "Contour area" << area1 << endl;
    }

    return 0;
}

Operation results:

Perimeter

brief introduction

  • Although the perimeter of the contour cannot directly reflect the size and shape of the contour area, it can be combined with the contour area to obtain more information about the contour area area. For example, when the ratio of the area of a certain area to the square of the perimeter is 1:16, the area is square

API

OpenCV4 provides the arcLength() function for detecting the contour perimeter or curve length

double cv::arcLength(InputArray curve, bool closed)
  • Curve: two-dimensional pixel points of contour or curve. The data type is vector < point > or Mat
  • Closed: indicates whether the contour or curve is closed. true indicates closed

Supplement: the statistical length of this function is the distance between two adjacent pixels of the contour or curve. For example, when calculating the contour length composed of three vertices A, B and C of A triangle, if the second parameter of the function is true, the statistical length is the sum of the lengths of three sides AB, BC and CA of the triangle; If this parameter is false, the statistical length is the sum of the distance lengths of the successive connecting lines from A to C, that is, the sum of the lengths of AB and BC

Example code:

In this program, the three vertices of a right triangle contour and the midpoint of the hypotenuse are given, and the arcLength() function is used to count the length of the contour under closed and non closed conditions respectively

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
    // The triangle outline is represented by 4 points
    vector<Point> contour;
    contour.push_back(Point2f(0, 0));
    contour.push_back(Point2f(10, 0));
    contour.push_back(Point2f(10, 10));
    contour.push_back(Point2f(5, 5));

    double length0 = arcLength(contour, true);
    double lenght1 = arcLength(contour, false);
    cout << "length0 = " << length0 << endl;
    cout << "length1 = " << lenght1 << endl;

    Mat_<Vec3b> img = imread("/home/kslas/OpenCV/erode.png");
    Mat_<uchar> gray, binary;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    GaussianBlur(gray, gray, Size(9, 9), 2, 2);
    threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < contours.size(); i++)
    {
        double length2 = arcLength(contours[i], true);
        cout << "The first" << i << "Contour length = " << length2 << endl;
    }

    return 0;
}

Operation results:

Topics: OpenCV