上一篇《OpenCV第八篇灰度直方图》介绍对灰度直方图,本篇将介绍直方图的均衡化,这是图像增强的常用方法。直方图均衡化的数学原理这里就不介绍了,有兴趣可以查阅专业书籍。下面来看看灰度直方图均衡化的函数——cvEqualizeHist
一.cvEqualizeHist
函数功能:直方图均衡化,该函数能归一化图像亮度和增强对比度
函数原型:
/* equalizes histogram of 8-bit single-channel image */
CVAPI(void) cvEqualizeHist( const CvArr* src, CvArr* dst );
第一个参数表示输入图像,必须为灰度图(8位,单通道图)。
第二个参数表示输出图像
函数说明:
该函数采用如下法则对输入图像进行直方图均衡化:
1:计算输入图像的直方图H。
2:直方图归一化,因此直方块和为255。
3:计算直方图积分,H'(i) = Sum(H(j)) (0<=j<=i)。
4:采用H'作为查询表:dst(x, y) = H'(src(x, y))进行图像变换。
在维基百科上对灰度直方图均衡化有个很好的对比,参见下图(网址:http://zh.wikipedia.org/zh-cn/%E7%9B%B4%E6%96%B9%E5%9B%BE%E5%9D%87%E8%A1%A1%E5%8C%96)
可以看出直方图均衡化对图像增强的效果很不错,对图像细节部分能起到明显的突出增强效果。下面我们自己动手写一个灰度直方图均衡化的程序,代码如下:
-
-
- #include <opencv2/opencv.hpp>
- #include <opencv2/legacy/compat.hpp>
- using namespace std;
- #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
- void FillWhite(IplImage *pImage)
- {
- cvRectangle(pImage, cvPoint(0, 0), cvPoint(pImage->width, pImage->height), CV_RGB(255, 255, 255), CV_FILLED);
- }
-
- CvHistogram* CreateGrayImageHist(IplImage **ppImage)
- {
- int nHistSize = 256;
- float fRange[] = {0, 255};
- float *pfRanges[] = {fRange};
- CvHistogram *pcvHistogram = cvCreateHist(1, &nHistSize, CV_HIST_ARRAY, pfRanges);
- cvCalcHist(ppImage, pcvHistogram);
- return pcvHistogram;
- }
-
- IplImage* CreateHisogramImage(int nImageWidth, int nScale, int nImageHeight, CvHistogram *pcvHistogram)
- {
- IplImage *pHistImage = cvCreateImage(cvSize(nImageWidth * nScale, nImageHeight), IPL_DEPTH_8U, 1);
- FillWhite(pHistImage);
-
-
- float fMaxHistValue = 0;
- cvGetMinMaxHistValue(pcvHistogram, NULL, &fMaxHistValue, NULL, NULL);
-
-
- int i;
- for(i = 0; i < nImageWidth; i++)
- {
- float fHistValue = cvQueryHistValue_1D(pcvHistogram, i);
- int nRealHeight = cvRound((fHistValue / fMaxHistValue) * nImageHeight);
- cvRectangle(pHistImage,
- cvPoint(i * nScale, nImageHeight - 1),
- cvPoint((i + 1) * nScale - 1, nImageHeight - nRealHeight),
- cvScalar(i, 0, 0, 0),
- CV_FILLED
- );
- }
- return pHistImage;
- }
- int main( int argc, char** argv )
- {
- const char *pstrWindowsSrcTitle = "原图(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsGrayTitle = "灰度图(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsHistTitle = "直方图(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsGrayEqualizeTitle = "灰度图-均衡化后(http://blog.csdn.net/MoreWindows)";
- const char *pstrWindowsHistEqualizeTitle = "直方图-均衡化后(http://blog.csdn.net/MoreWindows)";
-
-
- IplImage *pSrcImage = cvLoadImage("013.jpg", CV_LOAD_IMAGE_UNCHANGED);
- IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
- IplImage *pGrayEqualizeImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
-
-
- cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);
-
- int nHistImageWidth = 255;
- int nHistImageHeight = 150;
- int nScale = 2;
-
-
- CvHistogram *pcvHistogram = CreateGrayImageHist(&pGrayImage);
- IplImage *pHistImage = CreateHisogramImage(nHistImageWidth, nScale, nHistImageHeight, pcvHistogram);
-
-
- cvEqualizeHist(pGrayImage, pGrayEqualizeImage);
-
-
- CvHistogram *pcvHistogramEqualize = CreateGrayImageHist(&pGrayEqualizeImage);
- IplImage *pHistEqualizeImage = CreateHisogramImage(nHistImageWidth, nScale, nHistImageHeight, pcvHistogramEqualize);
-
-
-
- cvWaitKey(0);
-
- return 0;
- }
运行结果如下所示:
可以看出,灰度直方图均衡化对灰度图的图像增强效果明显,读者可以尝试将灰度直方图均衡化加入到《OpenCV第六篇轮廓检测下》中,看看均衡化后对轮廓检测的提升效果。
由于人眼对彩色更为敏感,下一篇《OpenCV第十一篇 彩色直方图均衡化》将对彩色图像进行直方图均衡化,让大家对直方图均衡化有一个更加直观的了解。