C++ OpenCV(三):图像像素统计

API

最大值和最小值

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());
  • 参数一:src,单通道输入图像;
  • 参数二:minVal,最小值;
  • 参数三:maxVal,最大值;
  • 参数四:minLoc,最小值坐标;
  • 参数五:maxLoc,最大值坐标;
  • 参数六:mask,掩码,通过此参数可以设置极值的查找范围。

均值

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)​=0​1Mc​=(∑I:mask(I)​=0​mtx(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)​=0​1meanc​=N∑I:mask(I)​=0​src(I)c​​stddevc​=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;
}

运行效果

C++ OpenCV(三):图像像素统计

上一篇:5个常见的交叉验证技术介绍和可视化


下一篇:opencv-位运算