# OpenCV Development Notes: red fat man 8 minutes to take you to understand the moment of the image (illustrated + easy to understand + program source code)

If the article is original, it cannot be reproduced without permission Original blog address: https://blog.csdn.net/qq21497936
Original blogger blog navigation: https://blog.csdn.net/qq21497936/article/details/102478062 Blog address: https://blog.csdn.net/qq21497936/article/details/106257036 Dear readers, knowledge is infinite and manpower is poor. Either we need to change our needs, or we need to find professionals, or we need to do our own research The collection of development technologies (including Qt practical technology, raspberry pie, 3D, OpenCV, OpenGL, ffmpeg, OSG, SCM, software and hardware combination, etc.) is continuously updated (click on the portal)
OpenCV development column (click on the portal) Previous:< OpenCV Development Notes (57): red fat man 8 minutes to take you in-depth understanding of histogram back projection (illustrated + easy to understand + program source code)>
Next: continuous supplement

<br>

# preface

Red fat man, come!
In recognition, sometimes we need to recognize the area and distance energy. If the distance between the two pupils, the width and height of the mobile phone, etc., the moment of the image is one of the bases for these specific scalars.

<br>

# Demo     <br>

# Moment of image

## summary

The   moment function is one of the necessary methods in image analysis, which is widely used, such as pattern recognition, target classification, target recognition and azimuth estimation, image coding and reconstruction. A moment set calculated from a digital figure one day usually describes the global characteristics of the figure shape, and provides a lot of information about different types of set characteristics of the image, such as size, direction and shape.

• First moment: related to shape;
• Second moment: shows the extent of curve expansion around the straight-line average;
• Third moment: the measurement of the symmetry of the mean;

# Find the outline

## Calculate all moments of the image (up to third order)

.

``````Moments moments( InputArray array, bool binaryImage = false );
``````
• Parameter 1: input array of InputArray type, input parameters can be raster image (single channel, 8-bit or floating-point 2D array) or 2D array (lN or NI);
• Parameter 2: binary image of bool type, the default value is false. If this parameter is true, all non-zero pixels are 1. This parameter is only used for images; Note: the return value of this parameter is to return the result after running.

## The prototype of calculating contour area function

used to calculate the area of a partial profile or the entire profile.

``````doube contourArea (InputArray contour , bool oriented=false );
``````
• Parameter 1: contour of InputArray type, input vector, 2D point (contour vertex), can be std::vector or Mat type;
• Parameter 2: oriented of bool type, oriented to region identifier. If true, this function returns a signed area value whose plus or minus depends on the direction of the profile (clockwise or counterclockwise). According to this characteristic, we can determine the position of the contour according to the symbol of the area. It should be noted that this parameter has a default value of false, which means that it is returned as an absolute value without symbols;

## Used to calculate the perimeter of a closed profile or the length of a curve.

``````double arcLength (InputArray curve , boo1 c1osed);
``````
• Parameter 1: curve of InputArray type, 2D point set of input, which can be std::vector or Mat type;
• Parameter 2: closed of bool type, an identifier used to indicate whether the curve is closed or not, with a default value of closed, indicating that the curve is closed;

<br>

# Demo source code

``````void OpenCVManager::testMoments()
{
QString fileName1 =
"E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/15.jpg";
int width = 400;
int height = 300;

cv::resize(srcMat, srcMat, cv::Size(width, height));

cv::String windowName = _windowTitle.toStdString();
cvui::init(windowName);

cv::Mat windowMat = cv::Mat(cv::Size(srcMat.cols * 2, srcMat.rows * 3),
srcMat.type());

int threshold1 = 200;
int threshold2 = 100;
while(true)
{
windowMat = cv::Scalar(0, 0, 0);

cv::Mat mat;
cv::Mat dstMat;
cv::Mat grayMat;
cv::Mat tempMat;
cv::Mat hullMat;
hullMat = srcMat.clone();

// copy the original image to the left
mat = windowMat(cv::Range(srcMat.rows * 0, srcMat.rows * 1),
cv::Range(srcMat.cols * 0, srcMat.cols * 1));
cv::addWeighted(mat, 0.0f, srcMat, 1.0f, 0.0f, mat);

{
// Gray scale
cv::cvtColor(srcMat, grayMat, CV_BGR2GRAY);
cv::cvtColor(grayMat, tempMat, CV_GRAY2BGR);
// copy
mat = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
cv::Range(srcMat.cols * 0, srcMat.cols * 1));
cv::addWeighted(mat, 0.0f, tempMat, 1.0f, 0.0f, mat);

cvui::printf(windowMat,
srcMat.rows * 1 + 100,
srcMat.cols * 0 + 20,
"threshold1");
cvui::trackbar(windowMat,
srcMat.rows * 1 + 100,
srcMat.cols * 0 + 50,
200,
&threshold1,
0,
255);
cvui::printf(windowMat,
srcMat.rows * 1 + 100,
srcMat.cols * 0 + 100, "threshold2");
cvui::trackbar(windowMat,
srcMat.rows * 1 + 100,
srcMat.cols * 0 + 130,
200,
&threshold2,
0,
255);
// Using edge detection
cv::Canny(grayMat, dstMat, threshold1, threshold2, 3);
// copy
mat = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
cv::Range(srcMat.cols * 1, srcMat.cols * 2));
cv::Mat rgbMat;
cv::cvtColor(dstMat, rgbMat, CV_GRAY2BGR);
cv::addWeighted(mat, 0.0f, rgbMat, 1.0f, 0.0f, mat);

// Find the outline
std::vector<std::vector<cv::Point>> contours;
cv::findContours(dstMat, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
// Draw a profile
for(int index = 0; index < contours.size(); index++)
{
cv::drawContours(hullMat, contours, index, cv::Scalar(0, 0, 255), 2);
}
// copy
mat = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
cv::Range(srcMat.cols * 0, srcMat.cols * 1));
cv::addWeighted(mat, 0.0f, hullMat, 1.0f, 0.0f, mat);

// Calculate all moments of the image
std::vector<cv::Moments> mu(contours.size());
for(int index = 0; index < contours.size(); index++)
{
mu[index] = cv::moments(contours[index], false);
}
// Calculate the area of the profile
for(int index = 0; index < contours.size(); index++)
{
double area = cv::contourArea(contours[index]);
cvui::printf(windowMat,
srcMat.cols * 1,
srcMat.rows * 2 + 15 * index,
"%d/%d: contorsArea = %f",
index,
contours.size(),
area);
double length = cv::arcLength(contours[index], true);
cvui::printf(windowMat,
srcMat.cols * 1 + width / 2 + 30,
srcMat.rows * 2 + 15 * index,
"arcLength = %f",
length);
}
}
// to update
cvui::update();
// display
cv::imshow(windowName, windowMat);
// esc key exit
if(cv::waitKey(25) == 27)
{
break;
}
}
}
``````

<br>

# Project template: corresponding version No. v1.52.0

corresponding version No. v1.52.0

<br>