CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal, CV_OUT double* maxVal = 0, CV_OUT Point* minLoc = 0, CV_OUT Point* maxLoc = 0, InputArray mask = noArray());
CV_EXPORTS_W Scalar mean(InputArray src, InputArray mask = noArray());
参数一:src,通道数为 1 到 4 个的输入图像;
参数二:mask,掩码,通过此参数可以设置求平均值的范围。
N
=
∑
I
:
mask
(
I
)
≠
0
1
M
c
=
(
∑
I
:
mask
(
I
)
≠
0
mtx
(
I
)
c
)
/
N
\begin{array}{l} N = \sum _{I: \; \texttt{mask} (I) \ne 0} 1 \\ M_c = \left ( \sum _{I: \; \texttt{mask} (I) \ne 0}{ \texttt{mtx} (I)_c} \right )/N \end{array}
N=∑I:mask(I)=01Mc=(∑I:mask(I)=0mtx(I)c)/N
CV_EXPORTS_W void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev, InputArray mask=noArray());
参数一:src,通道数为 1 到 4 个的输入图像;
参数二:mean,均值;
参数三:stddev,标准差;
参数四:mask,掩码,通过此参数可以设置极值的查找范围。
N = ∑ I , mask ( I ) ≠ 0 1 mean c = ∑ I : mask ( I ) ≠ 0 src ( I ) c N stddev c = ∑ I : mask ( I ) ≠ 0 ( src ( I ) c − mean c ) 2 N \begin{array}{l} N = \sum _{I, \texttt{mask} (I) \ne 0} 1 \\ \texttt{mean} _c = \frac{\sum_{ I: \; \texttt{mask}(I) \ne 0} \texttt{src} (I)_c}{N} \\ \texttt{stddev} _c = \sqrt{\frac{\sum_{ I: \; \texttt{mask}(I) \ne 0} \left ( \texttt{src} (I)_c - \texttt{mean} _c \right )^2}{N}} \end{array} N=∑I,mask(I)=01meanc=N∑I:mask(I)=0src(I)cstddevc=N∑I:mask(I)=0(src(I)c−meanc)2
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; int main() { cout << CV_VERSION << endl; Mat lena = imread("../image/lena.tif", IMREAD_ANYCOLOR); Mat lenaGray = imread("../image/lena.tif", IMREAD_GRAYSCALE); double min, max; Point minLoc, maxLoc; minMaxLoc(lenaGray, &min, &max, &minLoc, &maxLoc); cout << "最小值:" << min << ", 位于:" << minLoc << endl; cout << "最大值:" << max << ",位于:" << maxLoc << endl; Scalar scalar = mean(lena); cout << "平均值:" << scalar << endl; Mat meanMat,stdDevMat; meanStdDev(lena, meanMat, stdDevMat); cout << "平均值:" << meanMat << endl; cout << "标准差:" << stdDevMat << endl; waitKey(0); return 0; }