嗨,任何人都可以提供一个简单的开放式cv程序来加载两个RGB图像,将其转换为灰度,计算直方图,然后比较它们的直方图。我在开放的cv站点看到了一个类似的程序,但他们使用HSV而不是灰度级,它是一个C++程序。我可以查看流程和一切......我不知道使用哪些函数以及它们的参数意味着什么...... Regards, Kiran比较opencv灰度图像的直方图
0
A
回答
10
以下是执行此操作的简单代码段。既然你没有告诉你想如何比较直方图,我建议直观地做。
#include <opencv2/opencv.hpp>
void show_histogram(std::string const& name, cv::Mat1b const& image)
{
// Set histogram bins count
int bins = 256;
int histSize[] = {bins};
// Set ranges for histogram bins
float lranges[] = {0, 256};
const float* ranges[] = {lranges};
// create matrix for histogram
cv::Mat hist;
int channels[] = {0};
// create matrix for histogram visualization
int const hist_height = 256;
cv::Mat3b hist_image = cv::Mat3b::zeros(hist_height, bins);
cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges, true, false);
double max_val=0;
minMaxLoc(hist, 0, &max_val);
// visualize each bin
for(int b = 0; b < bins; b++) {
float const binVal = hist.at<float>(b);
int const height = cvRound(binVal*hist_height/max_val);
cv::line
(hist_image
, cv::Point(b, hist_height-height), cv::Point(b, hist_height)
, cv::Scalar::all(255)
);
}
cv::imshow(name, hist_image);
}
int main (int argc, const char* argv[])
{
// here you can use cv::IMREAD_GRAYSCALE to load grayscale image, see image2
cv::Mat3b const image1 = cv::imread("C:\\workspace\\horse.png", cv::IMREAD_COLOR);
cv::Mat1b image1_gray;
cv::cvtColor(image1, image1_gray, cv::COLOR_BGR2GRAY);
cv::imshow("image1", image1_gray);
show_histogram("image1 hist", image1_gray);
cv::Mat1b const image2 = cv::imread("C:\\workspace\\bunny.jpg", cv::IMREAD_GRAYSCALE);
cv::imshow("image2", image2);
show_histogram("image2 hist", image2);
cv::waitKey();
return 0;
}
结果:
+0
非常感谢很多朋友......对不起,如果我含糊不清,但你给了我正在寻找的东西......再次感谢。 – user2236862
+2
请接受答案。谢谢。 – brotherofken
2
相关问题
- 1. 比较两幅图像特定区域的直方图? OpenCV
- 2. Android与opencv - 图像灰度
- 3. OpenCV:将灰度图像着色的直接方法
- 4. OpenCV的比较图像
- 5. 直方图比较
- 6. OpenCV for ANDROID图像比较
- 7. OpenCV的灰度图像矢量
- 8. OpenCV Java中的灰度反转图像
- 9. OpenCV的CUDA C++ C图像灰度
- 10. 的OpenCV - 绘制灰度图像
- 11. 比较直方图的比较方法在opencv中不工作3.1.0
- 12. OpenCV - 从图像中获取灰度值
- 13. 阅读图像灰度opencv 3.0.0-dev
- 14. OpenCV - 读取16位灰度图像
- 15. openCV将Mat转换为灰度图像
- 16. numpy图像中灰度值的直方图
- 17. 多层比较直方图
- 18. OpenCV的图像调整比较MATLAB的
- 19. 灰度图像
- 20. 面向灰度的直方图
- 21. 灰度的图像
- 22. 比较OpenCV中的直方图并规范化相似索引
- 23. 比较文件中的多个直方图OpenCV
- 24. 颜色比例在图像python opencv使用直方图
- 25. 直方图opencv
- 26. RGB彩色图像直方图使用OpenCV C++对比度拉伸
- 27. 在OpenCV中将灰度图像转换为二进制图像
- 28. 比较虹膜图像与opencv
- 29. 使用OpenCV比较两个图像
- 30. OpenCV:使用ORB比较多个图像
你是什么意思“比较直方图”? – brotherofken