可通过opencv中的计算直方图函数
clacHist
计算出直方图,对于非图像数据可以通过存入Mat
类型中使用该方法计算
#include<iostream>
#include<opencv2\opencv.hpp>
#include<time.h>
using namespace cv;
int main()
{
Mat src(Size(1000, 1), CV_8UC1);
srand(time(NULL));
for (int i = 0; i < 1000; ++i)
{
src.at<uchar>(0, i) = rand() % 100+50;
}
Mat imageHist;
Mat imageNormalize;
const int histSize = 255; //定义灰度级数量
float histR[] = { 0,255 }; //定义每个维度下取值范围
const float *histRange = histR;
//计算直方图
calcHist(&src, 1, 0, Mat(), imageHist, 1, &histSize, &histRange, true, false);
//直方图归一化到范围[0,histSize]
normalize(imageHist, imageNormalize, 0, histSize, NORM_MINMAX, -1, Mat());
//创建直方图画布
Mat imageShowHist(histSize, histR[1], CV_8UC3, Scalar(255, 0, 0));
//分别画出每个灰度级下的直方图分布
for (int i = 0; i<histSize; i++)
{
line(imageShowHist, Point(i, histR[1]), Point(i, histR[1] - cvRound(imageNormalize.at<float>(i))), Scalar(0, 255, 255), 1, 8, 0);
}
imshow("Hist", imageShowHist);
waitKey();
return 0;
}
image.png
网友评论