c++ 求二维mat的直方图并绘图
//计算并返回图像的灰度直方图
void cahist(cv::Mat img) {
if (img.empty()) {
std::cout << "Cannot load image" << std::endl;
return;
}
//cv::imshow("image", image);
const int bins[1] = { 16383 };
float hranges[2] = { 0,16383 };
const float* ranges[1] = { hranges };
cv::Mat hist;
// 计算直方图
cv::calcHist(&img, 1, 0, cv::Mat(), hist, 1, bins, ranges);
//只统计8000 - 13000 实际有16383个区间
double xx[5000] = {};
double yy[5000] = {};
for (int i = 8000; i < 13000; i++) {
xx[i - 8000] = i - 8000;
yy[i - 8000] = hist.at<float>(i, 0);
}
int N{ 5000 };
MatPlotInit();
plot(xx, yy, N);
Sleep(60000);
MatPlotClose();
}
需要用到MatPlot这个工具 https://github.com/MacroUniverse/MatPlot
安装参照 https://wuli.wiki/online/MtPlot.html