图像直方图的均衡化

2019-05-27 11:16:45

直方图均衡化是用于提高图像对比度一种方式;

  • 直方图均衡化涉及到几个概念如下:
  1. 图像直方图:表示图像中每一灰度出现频次的统计关系,横轴代表灰度值,纵轴代表该灰度值在整张图片中出现次数;
  2. 概率密度函数PDF:表示图像中每一灰度出现的概率的统计关系,横轴代表灰度值,纵轴代表该灰度值在整张图片中出现概率;
  3. 累计概率分布函数CDF: 就是对PDF的累加(实质是PDF的积分);
  • 用CDF对原图进行灰度值映射的优点有:
  1. 不改变原图灰度值的相对大小(CDF是单调的);
  2. 动态的调整原图的动态范围;

Talk is cheap, Show me the code.

void operation::equalHist(const Mat& sImg, Mat& outImg)
{
    outImg = sImg.clone();
    int pixelCount[256] = { 0 };
    uchar pixelEqual[256] = { 0 };

//PDF
double grayProb[256] = { 0.0 };
//CDF
double grayDensityProb[256] = { 0.0 }; int imgRow = sImg.rows; int imgCol = sImg.cols; for (int i = 0; i < imgRow; ++i) { const uchar* pSrcData = sImg.ptr<uchar>(i); for (int j = 0; j < imgCol; ++j) { uchar pixValue = pSrcData[j]; for (int k = 0; k < 256; ++k) { if (pixValue == k) { pixelCount[k]++; } } } } int allPixelNum = imgRow * imgCol; for (int m = 0; m < 256; ++m) { grayProb[m] = pixelCount[m]*1.0 / allPixelNum; } grayDensityProb[0] = grayProb[0]; for (int k = 1; k < 256; ++k) { grayDensityProb[k] = grayDensityProb[k - 1] + grayProb[k]; } //equalization. for (int k = 0; k < 256; ++k) { pixelEqual[k] = (uchar)(255 * grayDensityProb[k]+ 0.5); } for (int i = 0; i < imgRow; ++i) { uchar* pData = outImg.ptr<uchar>(i); for (int j = 0; j < imgCol; ++j) { pData[j] = pixelEqual[pData[j]]; } } }
posted @ 2019-05-27 11:25  xxxqcb  阅读(216)  评论(0编辑  收藏  举报