图像的均值表示图像整体的亮暗程度,图像的均值越大图像整体越亮。标准方差表示图像中明暗变化的对比程度,标准差越大表示图像中明暗变化越明显。

计算图像均值

计算图像每个通道的均值

Scalar mean(InputArray src, InputArray mask = noArray());

       该函数用来求取图像矩阵的每个通道的平均值,函数的第一个参数用来输入待求平均值的图像矩阵,其通道数目可以在1到4之间。需要注意的是,该函数的返回值是一个cv::Scalar类型的变量,函数的返回值有4位,分别表示输入图像4个通道的平均值,如果输入图像只有1个通道,那么返回值的后三位都为0,例如输入该函数一个单通道平均值为1的图像,输出的结果为[1,0,0,0],可以通过cv::Scalar[n]查看第n个通道的平均值。该函数的第二个参数用于控制图像求取均值的范围。

计算图像均值和标准方差

cv::meanStdDev(im, meanMat, meanStdMat);
  • src:待求平均值的图像矩阵。
  • mean:图像每个通道的平均值,参数为Mat类型变量。
  • stddev:图像每个通道的标准方差,参数为Mat类型变量。
  • mask:掩模,用于标记求取哪些区域的平均值和标准方差。

该函数的第一个参数与前面mean()函数第一个参数相同,都可以是1-4通道的图像,不同之处在于该函数没有返回值,图像的均值和标准方差输出在函数的第二个和第三个参数中,区别于mean()函数,用于存放平均值和标准方差的是Mat类型变量,变量中的数据个数与第一个参数通道数相同,如果输入图像只有一个通道,该函数求取的平均值和标准方差变量中只有一个数据。

求一张RGB图像的每个通道的均值和标准方差

这个在深度学习图像预处理会用到  标准化的时候需要知道数据图像的均值 和 标准方差

示例代码:

//
// Created by smallflyfly on 2021/6/9.
//

#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace std;
using namespace cv;

int main() {

    Mat im = imread("test.jpg");

    // 图像每个通道均值
    Scalar mean;
    mean = cv::mean(im);
    cout << mean << endl;
    cout << mean[0] << " " << mean[1] << " " <<  mean[2] << endl;

    // 图像均值 和 标准方差
    Mat meanMat, stdMat;

    cv::meanStdDev(im, meanMat, stdMat);

    cout << "图像均值 和 标准方差" << endl;
    cout << meanMat << endl;
    cout << stdMat << endl;

    cout << meanMat.at<double>(0) << " " << meanMat.at<double>(1) << " " << meanMat.at<double>(2) << endl;
    cout << stdMat.at<double>(0) << " " << stdMat.at<double>(1) << " " << stdMat.at<double>(2) << endl;

    return 0;
}

 

GitHub 加速计划 / opencv31 / opencv
211
14
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:9 个月前 )
b3cb517c Updating doc markdown to include API in FastCV gemm HAL 1 天前
8a0ea789 Fix #25696: Solved the problem in Subdiv2D, empty delaunay triangulation #27149 Detailed description Expected behaviour: Given 4 points, where no three points are collinear, the Delaunay Triangulation Algorithm should return 2 triangles. Actual: The algorithm returns zero triangles in this particular case. Fix: The radius of the circumcircle tends to infinity when the points are closer to form collinear points, so the problem occurs because the super-triangles are not large enough, which then results in certain edges are not swapped. The proposed solution just increases the super triangle, duplicating the value of constant for example. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐