项目功能实现:对一张图像进行统计最大、最小像素值、均差以及方差的值
按照之前的博文结构来,这里就不在赘述了
一、头文件
pixel_statistic.h
#pragma once
#include<opencv2/opencv.hpp>
using namespace cv;
class Pixel_Statistic {
public:
void pixel_statistic(Mat& image);
};
#pragma once
二、函数实现
pixel_statistic.cpp
double min_value, max_value;
因为像素值是浮点数,故接收的数据类型定义为double
std::vector<Mat>yy;
split(image, yy);
对三颜色通道的图片进行通道分割
具体分割实现思路可参考博文:十一、图像颜色通道分离与合并
minMaxLoc(yy[i], &min_value, &max_value, &min_location, &max_location, Mat());
需要循环遍历每个通道,并统计计算最大、最小像素值的数值和位置
OpenCV颜色通道为BGR,yy[0]是B通道
meanStdDev(image, mean, stddev);
得到的是三颜色通道的均值和方差总和,不是单通道的均值和方差
获取每个颜色通道的均值和方差,对yy进行统计
yy[0]是B通道,BGR以此类推即可
#include"pixel_statistic.h"
#include<iostream>
#include<opencv2/opencv.hpp>
void Pixel_Statistic::pixel_statistic(Mat& image) {
double min_value, max_value;
Point min_location, max_location;
Mat mean, stddev;
std::vector<Mat>yy;
split(image, yy);//minMaxLoc统计的是单通道的图片,故需要通过split将三通道进行分离
for (int i = 0; i < yy.size(); i++) {//每个不同颜色通道依次进行统计
minMaxLoc(yy[i], &min_value, &max_value, &min_location, &max_location, Mat());
std::cout << "NO.channels is: " << i << " min_value is: " << min_value << " min_location is:" << min_location << " max_value is : " << max_value <<" max_location is : "<<max_location << std::endl;
}
meanStdDev(image, mean, stddev);//得到的是三颜色通道的均值和方差总和,不是单通道的均值和方差
std::cout << "all channel's means is; \n" << mean << std::endl;
std::cout << "all channel's stddev is; \n" << stddev << std::endl;
//想要获得单通道的均值和方差,我的想法思路是 通过split进行分离通道,然后挨个进行计算统计即可
}
三、主函数
yy_main.cpp
#include <opencv2/opencv.hpp>
#include <iostream>
#include "pixel_statistic.h"
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = cv::imread("E:/C++_workspace/beyond.jpg", IMREAD_COLOR);
if (src.empty()) {
printf("load image is false...\n");
return -1;
}
namedWindow("yanyu", WINDOW_FREERATIO);
imshow("yanyu", src);
Pixel_Statistic yy;
yy.pixel_statistic(src);
waitKey(0);
destroyAllWindows();
return 0;
}
项目结构如下:
运行结果如下: