Article Directory
-
-
- 1. Find the minimum and maximum values of the pixel values in the ROI region of interest
- 2. Find the mean and standard deviation (standard deviation) of the pixel values in the ROI region of interest
-
- (1) Mathematical concepts:
-
- Mean mean: the average value of the sample set;
- Variance variance: In probability theory, **variance** is used to measure the **degree of deviation** between a random variable and its mathematical expectation (ie mean value); variance in statistics (sample variance) is the difference between each sample data and the sample average The **average** of the **sum of squares** of the **difference**;
- Standard deviation standard deviation: reflects the degree of dispersion of a data set, the standard deviation is the arithmetic square root of the sample **variance**;
- (2) meanStdDev() function prototype:
- (3) Use the function `mean()` to find the mean value of the pixel values in the roi region of interest
-
Statistics of pixel values in the ROI region of interest, find the minimum, maximum, mean, and standard deviation (standard deviation) of the pixel values in the ROI region of interest
1. Find the minimum and maximum values of the pixel values in the ROI region of interest
- In OpenCV, the function
minMaxLoc()
is used to find the maximum and minimum values in the matrix and give their coordinates;- The function is not suitable for multi-channel matrices. If you need to traverse all channels to find the maximum and minimum values, first use the function
Mat::reshape()
to convert the matrix to a single-channel matrix, or usesplit()
,mixChannels()
, to extract a specific channel, and then useminMaxLoc()
the function to find the maximum value of the matrix and min;
minMaxLoc() function prototype:
(1) Prototype 1:
void cv::minMaxLoc( InputArray src,
double *minVal,
double *maxVal = 0,
Point *minLoc = 0,
Point *maxLoc = 0,
InputArray mask = noArray()
)
参数解释:
src:输入单通道矩阵
minVal:输入矩阵中的最小值(如果不需要返回,置为NULL)
maxVal:输入矩阵中的最大值(如果不需要返回,置为NULL)
minLoc:输入矩阵中的最小值的坐标(如果不需要返回,置为NULL)
maxLoc:输入矩阵中的最大值的坐标(如果不需要返回,置为NULL)
mask:可选参数,操作掩摸,用以标记求取哪些区域
(2) Prototype 2:
void minMaxLoc( const SparseMat &src,
double *minVal,
double *maxVal,
int *minIdx = 0,
int *maxIdx = 0
);
参数解释:
src:输入单通道矩阵
minVal:输入矩阵中的最小值(如果不需要返回,置为NULL)
maxVal:输入矩阵中的最大值(如果不需要返回,置为NULL)
minLoc:输入矩阵中的最小值的坐标(如果不需要返回,置为NULL)
maxLoc:输入矩阵中的最大值的坐标(如果不需要返回,置为NULL)
(3) In addition minMaxLoc()
, the function with the same usage as function prototype 1 minMaxIdx()
can also find the maximum and minimum values in the matrix:
minMaxIdx() function prototype:
void minMaxIdx( InputArray src,
double *minVal,
double *maxVal = 0,
int *minIdx = 0,
int *maxIdx = 0,
InputArray mask = noArray()
);
参数解释:
src:输入单通道矩阵
minVal:输入矩阵中的最小值(如果不需要返回,置为NULL)
maxVal:输入矩阵中的最大值(如果不需要返回,置为NULL)
minIdx:输入矩阵中的最小值的坐标(如果不需要返回,置为NULL)
maxIdx:输入矩阵中的最大值的坐标(如果不需要返回,置为NULL)
mask:可选参数,操作掩摸,用以标记求取哪些区域
2. Find the mean and standard deviation (standard deviation) of the pixel values in the ROI region of interest
(1) Mathematical concepts:
-
Variance variance: In probability theory, variance is used to measure the degree of deviation between a random variable and its mathematical expectation (ie mean) ; in statistics, variance (sample variance) is the average of the sum of the squares of the differences between each sample data and the sample mean ;
-
Standard deviation standard deviation: reflects the degree of dispersion of a data set, the standard deviation is the arithmetic square root of the sample variance ;
(2) meanStdDev() function prototype:
The standard deviation in opencv indicates the degree of change in light and shade of an image. The larger the standard deviation, the more obvious the change in light and shade;
void meanStdDev( InputArray src,
OutputArray mean,
OutputArray stddev,
InputArray mask=noArray()
);
参数解释:
src:输入矩阵,单通道的矩阵/多通道的矩阵;
mean:输出参数,计算出的平均值;
stddev:输出参数,计算出的标准差;
mask:可选参数,操作掩摸,用以标记求取哪些区域;
Example:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>
using namespace cv;
using namespace std;
int main() {
// 读取图像,BGR存储在Mat矩阵里
Mat src = cv::imread("C:\\cpp\\image\\suzy4.jpg");
if (src.empty()) {
printf("could not load image..../n");
return -1;
}
namedWindow("src", WINDOW_NORMAL);
imshow("src", src);
// 将矩阵转换为单通道矩阵
vector<Mat> mv;
split(src, mv);
double minv, maxv;
Point minLoc, maxLoc;
// 使用函数 minMaxLoc()找出矩阵中的最大值和最小值
for (int i = 0; i < mv.size(); i++)
{
minMaxLoc(mv[i], &minv, &maxv, &minLoc, &maxLoc, Mat());
cout << "channels:" << i << " min:" << minv << " max:" << maxv << endl;
}
// 求roi感兴趣区域内像素值的均值、标准差(标准方差)
Mat mean, stddev;
meanStdDev(src, mean, stddev);
cout << "means:" << mean << endl;
cout << "stddev:" << stddev << endl;
waitKey();
destroyAllWindows();
return 0;
}
(3) mean()
Find the mean value of the pixel values in the ROI region of interest through the function
mean() function prototype:
mean( InputArray src,
InputArray mask=noArray()
)
参数解释:
src:输入矩阵;
mask:可选参数,操作掩摸,用以标记求取哪些区域;
Code example:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
cv::Mat A1 = (cv::Mat_<uchar>(3, 3) << 1, 2, 3,
4, 5, 6,
7, 8, 9);
cout << "A1中的数据为:\n" << A1 << endl << endl;
cv::Scalar A1_mean;
// 通过函数mean()求roi感兴趣区域内像素值的均值
A1_mean = mean(A1);
cout << "通过函数mean计算得到的A1的均值为:" << A1_mean << endl << endl;
cv::Scalar A1_mean_2, A1_sd;
meanStdDev(A1, A1_mean_2, A1_sd);
cout << "通过函数meanStdDev计算得到的A1的均值为:" << A1_mean_2 << endl << endl;
cout << "通过函数meanStdDev计算得到的A1的标准差为:" << A1_sd << endl << endl;
return(0);
}