0


OPENCV图像直方图以及均值化

直方图是我们在照片中使用来查看图像中每个值有多少像素,照片中的每个像素的值都从0(黑色)到255(白色),图的左侧代表音阶的暗色调,右侧代表较亮的色调。在彩色摄影中,每个像素对于每种颜色都有其自己的值(0-255)。图片中的直方图显示了每种颜色(红色,蓝色和绿色)的像素值分布.

图像直方图,也叫灰度直方图,反映了图像像素分布的统计特征,是图像处理中简单有效的工具,图像直方图广泛地应用于图像处理的各个领域,如:特征提取,图像匹配,灰度图像的阈值分割,基于颜色的图像检索以及图像分类。

图像的直方图的形态很大程度上可直观的反映图像的质量情况,比如根据下图所示,会很快发现一张图片是否过量还是过暗。

图像直方图的优点是,计算量较小,且具有图像平移,旋转,缩放不变性等。

数学原理

图像灰度直方图计算:

\mathbf{P(r)=\frac{n_r}{W*H} \ \ \ \ \ r\in[0,L-1]}

其中,W,H分别表示图像的宽和高,n_r表示图像中像素值为r的数量,P(r)表示图像中像素值为r的比率,L是指图像灰度级数,比如,8BIT的灰度图像L=2^8=256.其存在256个色阶,即0-255。

OPENCV获取直方图:

  1. //获得直方图
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <iostream>
  6. using namespace cv;
  7. using namespace std;
  8. int main(int argc, char** argv)
  9. {
  10. //----------------------example 1-------------------------------//
  11. Mat src, dst;
  12. /// Load image
  13. src = imread("/home/caozilong/165823915.jpg");
  14. if (!src.data)
  15. {
  16. cout << "load image failed" << endl;
  17. return -1;
  18. }
  19. /// Separate the image in 3 places ( R, G and B )
  20. vector<Mat> rgb_planes;
  21. Mat hsv;
  22. cvtColor(src, hsv, COLOR_BGR2HSV);
  23. split(hsv, rgb_planes);
  24. split(src, rgb_planes);
  25. /// Establish the number of bins
  26. int histSize = 256;
  27. /// Set the ranges ( for R,G,B) )
  28. float range[] = { 0, 255 };
  29. const float* histRange = { range };
  30. bool uniform = true; bool accumulate = false;
  31. Mat r_hist, g_hist, b_hist;
  32. /// Compute the histograms:
  33. calcHist(&rgb_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
  34. calcHist(&rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
  35. calcHist(&rgb_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
  36. // Draw the histograms for R, G and B
  37. int hist_w = 600; int hist_h = 400;
  38. int bin_w = cvRound((double)hist_w / histSize);
  39. Mat rgb_hist[3];
  40. for (int i = 0; i < 3; ++i)
  41. {
  42. rgb_hist[i] = Mat(hist_h, hist_w, CV_8UC3, Scalar::all(0));
  43. }
  44. Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
  45. /// Normalize the result to [ 0, histImage.rows-10]
  46. normalize(r_hist, r_hist, 0, histImage.rows - 10, NORM_MINMAX);
  47. normalize(g_hist, g_hist, 0, histImage.rows - 10, NORM_MINMAX);
  48. normalize(b_hist, b_hist, 0, histImage.rows - 10, NORM_MINMAX);
  49. /// Draw for each channel
  50. for (int i = 1; i < histSize; i++)
  51. {
  52. line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
  53. Point(bin_w * (i), hist_h - cvRound(r_hist.at<float>(i))),
  54. Scalar(0, 0, 255), 1);
  55. line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
  56. Point(bin_w * (i), hist_h - cvRound(g_hist.at<float>(i))),
  57. Scalar(0, 255, 0), 1);
  58. line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),
  59. Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))),
  60. Scalar(255, 0, 0), 1);
  61. }
  62. for (int j = 0; j < histSize; ++j)
  63. {
  64. int val = saturate_cast<int>(r_hist.at<float>(j));
  65. rectangle(rgb_hist[0], Point(j * 2 + 10, rgb_hist[0].rows), Point((j + 1) * 2 + 10, rgb_hist[0].rows - val), Scalar(0, 0, 255), 1, 8);
  66. val = saturate_cast<int>(g_hist.at<float>(j));
  67. rectangle(rgb_hist[1], Point(j * 2 + 10, rgb_hist[1].rows), Point((j + 1) * 2 + 10, rgb_hist[1].rows - val), Scalar(0, 255, 0), 1, 8);
  68. val = saturate_cast<int>(b_hist.at<float>(j));
  69. rectangle(rgb_hist[2], Point(j * 2 + 10, rgb_hist[2].rows), Point((j + 1) * 2 + 10, rgb_hist[2].rows - val), Scalar(255, 0, 0), 1, 8);
  70. }
  71. /// Display
  72. namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
  73. namedWindow("wnd");
  74. imshow("calcHist Demo", histImage);
  75. imshow("wnd", src);
  76. imshow("R", rgb_hist[0]);
  77. imshow("G", rgb_hist[1]);
  78. imshow("B", rgb_hist[2]);
  79. waitKey();
  80. }
  1. g++ new.cpp `pkg-config --cflags --libs opencv`

直方图均值化

直方图均衡化是通过拉伸像素强度的分布范围,使得在0~255灰阶上的分布更加均衡提高了图像的对比度,达到改善图像主观视觉效果的目的。对比度较低的图像适合使用直方图均衡化方法来增强图像细节。

  1. //彩色图像直方图均衡化
  2. #include<opencv2/opencv.hpp>
  3. #include<iostream>
  4. #include<cmath>
  5. using namespace cv;
  6. using namespace std;
  7. const char* output = "histogram iamge";
  8. int main(int argc, char** argv)
  9. {
  10. Mat src, dst, dst1;
  11. src = imread("/home/caozilong/165823915.jpg");
  12. if (!src.data)
  13. {
  14. printf("could not load image...\n");
  15. return -1;
  16. }
  17. char input[] = "input image";
  18. namedWindow(input, 0);
  19. namedWindow(output, 0);
  20. resizeWindow(input, 600, 400);
  21. imshow(input, src);
  22. //分割通道
  23. vector<Mat>channels;
  24. split(src, channels);
  25. Mat blue, green, red;
  26. blue = channels.at(0);
  27. green = channels.at(1);
  28. red = channels.at(2);
  29. //分别对BGR通道做直方图均衡化
  30. equalizeHist(blue, blue);
  31. equalizeHist(green, green);
  32. equalizeHist(red, red);
  33. //合并通道
  34. merge(channels, dst);
  35. resizeWindow(output, 600, 400);
  36. imshow(output, dst);
  37. waitKey(0);
  38. return 0;
  39. }
  1. g++ main.cpp `pkg-config --cflags --libs opencv`

可以看到对比度确实增加了

但是对于那些曝光良好,直方图表现均匀的图像,均值化的效果并不明显:

然后,进行均值化后的效果,可以发现,对比度的增加效果并不明显:

另一个例子,原始图片:

图像直方图:

进行直方图均值化后的图像:

对应的直方图为:


结束!


本文转载自: https://blog.csdn.net/tugouxp/article/details/125027037
版权归原作者 papaofdoudou 所有, 如有侵权,请联系我们删除。

“OPENCV图像直方图以及均值化”的评论:

还没有评论