C/C++教程

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

本文主要是介绍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(三):图像像素统计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!