opencv:像素统计信息
Posted wbyixx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv:像素统计信息相关的知识,希望对你有一定的参考价值。
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//Mat src = imread("f:/images/starry_night.jpg", IMREAD_GRAYSCALE);
Mat src = imread("f:/images/starry_night.jpg");
if (src.empty())
{
printf("Could not find the image!
");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
int w = src.cols;
int h = src.rows;
int ch = src.channels();
printf("w: %d, h: %d, ch: %d
", w, h, ch);
double min_val;
double max_val;
// 最大值,最小值,所在的像素点的位置
Point minloc;
Point maxloc;
// 找到图片的最大最小值
/* 只支持一个通道 */
//minMaxLoc(src, &min_val, &max_val, &minloc, &maxloc, Mat());
//printf("min: %.2f, max: %.2f
", min_val, max_val);
// 像素值统计信息
vector<int> hist(256);
for (int i = 0; i < 256; i++) {
hist[i] = 0;
}
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
int pv = src.at<uchar>(row, col);
hist[pv]++; // 统计每一个像素值的出现次数
}
}
// 均值、方差
Scalar s = mean(src);
printf("mean: (%.2f, %.2f, %.2f)
", s[0], s[1], s[2]);
Mat mm, mstd;
meanStdDev(src, mm, mstd);
printf("mstd rows: %d, cols: %d
", mstd.rows, mstd.cols);
printf("mm: rows: %d, cols: %d
", mm.rows, mm.cols);
printf("mstd: %.2f, %.2f, %.2f
", mstd.at<double>(0,0), mstd.at<double>(1, 0), mstd.at<double>(2, 0));
printf("mm: %.2f, %.2f, %.2f
", mm.at<double>(0, 0), mm.at<double>(1, 0), mm.at<double>(2, 0));
src = Mat::zeros(Size(512, 512), CV_8UC3);
src = Scalar(0, 0, 255); // 创建一个纯红色的图像
imshow("red src", src);
printf("
一个纯红色的图像
");
meanStdDev(src, mm, mstd);
printf("mstd rows: %d, cols: %d
", mstd.rows, mstd.cols);
printf("mm: rows: %d, cols: %d
", mm.rows, mm.cols);
printf("mstd: %.2f, %.2f, %.2f
", mstd.at<double>(0, 0), mstd.at<double>(1, 0), mstd.at<double>(2, 0));
printf("mm: %.2f, %.2f, %.2f
", mm.at<double>(0, 0), mm.at<double>(1, 0), mm.at<double>(2, 0));
waitKey(0);
destroyAllWindows();
return 0;
}
以上是关于opencv:像素统计信息的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV统计米粒数目-计算联通区域的个数及联通区域内像素的个数
OpenCV 完整例程50. 图像增强—直方图统计量图像增强