像素的mean,std,min,max以及统计的方法
//像素基本操作
#include <iostream>
#include<opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("src.jpg");
if (src.empty())
{
printf("open file failed");
return -1;
}
//1. 最大最小值 只支持单通道
// 分离通道
vector<Mat> channels;
split(src, channels);
imshow("B", channels[0]);
// 最大最小值
double min, max;
// 最大最小值的坐标
Point minLoc, maxLoc;
minMaxLoc(channels[0], &min, &max, &minLoc, &maxLoc);
cout << min << endl << max << endl << minLoc << endl << maxLoc;
//2. 均值标准差,这个支持3通道
//mean函数
Scalar m = mean(src);
Mat mm, ss;
meanStdDev(src, mm, ss);
cout << m << endl << mm << endl << ss;
//3. 统计像素值
vector<int>hist(256, 0);
vector<int>hist2(256, 0);
auto data = src.data;
for (int row = 0; row < src.rows; row++)
{
uchar* src_row = src.ptr<uchar>(row);
for (int col = 0; col < src.cols; col++)
{
//data指针方式
int pv = *data++; //B
hist[pv]++;
pv = *data++; //G
hist[pv]++;
pv = *data++; //R
hist[pv]++;
//ptr指针方式
int pv2 = *src_row++;
hist2[pv2]++;
pv2 = *src_row++;
hist2[pv2]++;
pv2 = *src_row++;
hist2[pv2]++;
}
}
//data指针和ptr指针遍历结果一样
cout << endl << "0:" << hist[0] << endl << "255:" << hist[255];
cout << endl << "0:" << hist2[0] << endl << "255:" << hist2[255];
waitKey(0);
destroyAllWindows();
return 0;
}
这里分别举例了data指针和ptr指针的用法