#include<iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class calc_hist
{
private:
int channel;
int hist_size;
float hrange[2];
const float* range;
public:
calc_hist() :channel(0), hist_size(256)//构造函数
{
hrange[0] = 0;
hrange[1] = 256;
range = {hrange};
};
Mat process(Mat& image)
{
Mat hist;
calcHist(&image, 1, &channel, Mat(), hist, 1, &hist_size, &range);
double minvalue = 0;
double maxvalue = 0;
minMaxLoc(hist,&minvalue,&maxvalue,0,0);//点位hist中的最大最小值。
int x, y,xp,yp;//坐标点。以及它的前边相邻的坐标。
Mat histmap(256,256,CV_8U,Scalar(256));
imshow("histmap",histmap);
for (int i = 0; i < 256; i++)
{
x = i;
y = static_cast<int>(hist.at<float>(i) / maxvalue * 256*0.9);
if (x != 0)
{
line(histmap, Point(x, 256 - y), Point(xp, 256 - yp), Scalar(0),2);
}
xp = x;
yp = y;
}
return histmap;
}
};
//计算灰度图像的直方图简单方法。
int main()
{
Mat image1 = imread("大教堂.jpg");
imshow("原图",image1);
calc_hist xx;
Mat t=xx.process(image1);
imshow("结果",t);
waitKey(0);
return 0;
}
简单的求灰度图的直方图的方法
最新推荐文章于 2023-10-13 12:16:54 发布