彩色图像的直方图均衡化--基于OpenCV中EqualizeHist_Demo实现



本文给出基于彩色图像直方图均衡化的OpenCV代码与结果示例!

具体内容包含:

1. 灰度图像直方图均衡化

2. 对RGB三通道各自均衡化后,再组合输出结果

3. RGB图像转化为HSI,YUV,YCbCr颜色空间后,对亮度通道进行均衡化运算后再转回RGB空间

[cpp] view plain copy print ?
  1. /** 
  2.  * @function EqualizeHist_Demo.cpp 
  3.  * @brief Demo code for equalizeHist function 
  4.  * @author OpenCV team 
  5.  */  
  6.   
  7. #include "opencv2/imgcodecs.hpp"  
  8. #include "opencv2/highgui/highgui.hpp"  
  9. #include "opencv2/imgproc/imgproc.hpp"  
  10. #include <iostream>  
  11. #include <stdio.h>  
  12.   
  13. using namespace cv;  
  14. using namespace std;  
  15.   
  16. // add by frank, 2014-09-25  
  17. Mat equalizeChannelHist(const Mat & inputImage)  
  18. {  
  19.     if( inputImage.channels() >= 3 )  
  20.     {  
  21.         vector<Mat> channels;  
  22.         split(inputImage, channels);  
  23.   
  24.         Mat B,G,R;  
  25.   
  26.         equalizeHist( channels[0], B );  
  27.         equalizeHist( channels[1], G );  
  28.         equalizeHist( channels[2], R );  
  29.   
  30.         vector<Mat> combined;  
  31.         combined.push_back(B);  
  32.         combined.push_back(G);  
  33.         combined.push_back(R);  
  34.   
  35.         Mat result;  
  36.         merge(combined, result);  
  37.   
  38.         return result;  
  39.     }  
  40.   
  41.     return Mat();  
  42. }  
  43.   
  44. Mat equalizeIntensityHist(const Mat & inputImage)  
  45. {  
  46.     if(inputImage.channels() >= 3)  
  47.     {  
  48.         Mat ycrcb;  
  49.   
  50.         cvtColor(inputImage, ycrcb, COLOR_BGR2YCrCb);  
  51.   
  52.         vector<Mat> channels;  
  53.         split(ycrcb, channels);  
  54.   
  55.         equalizeHist(channels[0], channels[0]);  
  56.   
  57.         Mat result;  
  58.         merge(channels,ycrcb);  
  59.   
  60.         cvtColor(ycrcb, result, COLOR_YCrCb2BGR);  
  61.   
  62.         return result;  
  63.     }  
  64.   
  65.     return Mat();  
  66. }  
  67.   
  68. void getGrayImageHistImage(const Mat & src, Mat & histImage)  
  69. {  
  70.     Mat hist;  
  71.     int histSize = 256;  
  72.   
  73.     calcHist(&src, 1, 0, Mat(), hist, 1, &histSize, 0);  
  74.     normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);  
  75.   
  76.     histImage = Scalar::all(255);  
  77.     int binW = cvRound((double)histImage.cols/histSize);  
  78.   
  79.     forint i = 0; i < histSize; i++ )  
  80.         rectangle( histImage, Point(i*binW, histImage.rows),  
  81.         Point((i+1)*binW, histImage.rows - cvRound(hist.at<float>(i))),  
  82.         Scalar::all(0), -1, 8, 0 );  
  83. }  
  84.   
  85.   
  86. int main( intchar** argv )  
  87. {  
  88.     Mat src, dst;  
  89.     Mat intensity_color_dst;  
  90.     Mat channel_color_dst;  
  91.   
  92.     const char* source_gray_window = "Source Gray Image";  
  93.     const char* equalized_gray_window = "Equalized Gray Image";  
  94.     const char* source_color_window = "Source Color Image";  
  95.     const char* equalized_intensity_color_window = "Equalized Intensity Color Image";  
  96.     const char* equalized_channels_color_window = "Equalized Channels Color Image";  
  97.   
  98.     /// Load image  
  99.     src = imread( argv[1], 1 );  
  100.   
  101.     if( src.empty() )  
  102.     {   
  103.         cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;  
  104.         return -1;  
  105.     }  
  106.   
  107.     /// color image intensity equalization  
  108.     {  
  109.         intensity_color_dst = equalizeIntensityHist(src);  
  110.           
  111.         namedWindow( source_color_window, WINDOW_AUTOSIZE );  
  112.         namedWindow( equalized_intensity_color_window, WINDOW_AUTOSIZE );  
  113.   
  114.         imshow( source_color_window, src );  
  115.         imshow( equalized_intensity_color_window, intensity_color_dst );  
  116.     }  
  117.   
  118.     /// color image each channel equalization  
  119.     {  
  120.         channel_color_dst = equalizeChannelHist(src);  
  121.         namedWindow( equalized_channels_color_window, WINDOW_AUTOSIZE );  
  122.         imshow( equalized_channels_color_window, channel_color_dst );  
  123.     }  
  124.   
  125.     /// gray image equalization  
  126.     {  
  127.         cvtColor( src, src, COLOR_BGR2GRAY );  
  128.         equalizeHist( src, dst );  
  129.   
  130.         namedWindow( source_gray_window, WINDOW_AUTOSIZE );  
  131.         namedWindow( equalized_gray_window, WINDOW_AUTOSIZE );  
  132.   
  133.         imshow( source_gray_window, src );  
  134.         imshow( equalized_gray_window, dst );  
  135.   
  136.         /// get source gray image Histogram  
  137.         Mat graySrc_histImage = Mat::ones(200, 260, CV_8U)*255;  
  138.         getGrayImageHistImage(src, graySrc_histImage);  
  139.         imshow("source gray image histogram", graySrc_histImage);  
  140.   
  141.         /// get equalized gray image Histogram  
  142.         Mat grayDst_histImage = Mat::ones(200, 260, CV_8U)*255;  
  143.         getGrayImageHistImage(dst, grayDst_histImage);  
  144.         imshow("Equalized gray image histogram", grayDst_histImage);  
  145.     }  
  146.   
  147.     /// Wait until user exits the program  
  148.     waitKey(0);  
  149.   
  150.     return 0;  
  151. }  

执行结果,如下图所示:

         

           原灰度图像                                                          直方图均衡化增强后图像

     

             原彩色图像               RGB各通道直方图均衡化后图像           YCbCr 亮度通道Y直方图均衡化后图像

分RGB通道均衡化后图像颜色有失真情况,而亮度通道均衡化结果不会,主要原因:

Histogram equalization is a non-linear process. Channel splitting and equalizing each channel separately is not the proper way for equalization of contrast. Equalization involves Intensity values of the image not the color components. So for a simple RGB color image, HE should not be applied individually on each channel. Rather, it should be applied such that intensity values are equalized without disturbing the color balance of the image. So, the first step is to convert the color space of the image from RGB into one of the color space which separates intensity values from color components. Some of these are:
  HSV/HLS
  YUV
  YCbCr
Convert the image from RGB to one of the above mentioned color spaces. YCbCr is preferred as it is designed for digital images.Perform HE of the intensity plane Y. Convert the image back to RGB.

你可能感兴趣的:(opencv,直方图均衡化,彩色图像)