# 图像增强算法

7,269次阅读

1、对数图像增强算法

void LogEnhance(IplImage* img, IplImage* dst)
{
// 由于oldPixel:[1,256],则可以先保存一个查找表
uchar lut[256] ={0};

double temp = 255/log(256);

for ( int i =0; i<255; i++)
{
lut[i] = (uchar)(temp* log(i+1)+0.5);
}

for( int row =0; row <img->height; row++)
{
uchar *data = (uchar*)img->imageData+ row* img->widthStep;
uchar *dstData = (uchar*)dst->imageData+ row* dst->widthStep;

for ( int col = 0; col<img->width; col++)
{
for( int k=0; k<img->nChannels; k++)
{
uchar t1 = data[col*img->nChannels+k];
dstData[col*img->nChannels+k] = lut[t1];
}
}
}
}</span><span style="font-size:18px;">
</span>

2、指数图像增强算法

void ExpEnhance(IplImage* img, IplImage* dst)
{
// 由于oldPixel:[1,256],则可以先保存一个查找表
uchar lut[256] ={0};

double temp = 1.0/255.0;

for ( int i =0; i<255; i++)
{
lut[i] = (uchar)(temp*i*i+0.5);
}

for( int row =0; row height; row++)
{
uchar *data = (uchar*)img->imageData+ row* img->widthStep;
uchar *dstData = (uchar*)dst->imageData+ row* dst->widthStep;

for ( int col = 0; colwidth; col++)
{
for( int k=0; knChannels; k++)
{
uchar t1 = data[col*img->nChannels+k];
dstData[col*img->nChannels+k] = lut[t1];
}
}
}
}

3、加Masaic算法

uchar getPixel( IplImage* img, int row, int col, int k)
{
return ((uchar*)img->imageData + row* img->widthStep)[col*img->nChannels +k];
}

void setPixel( IplImage* img, int row, int col, int k, uchar val)
{
((uchar*)img->imageData + row* img->widthStep)[col*img->nChannels +k] = val;
}

// nSize:为尺寸大小，奇数
// 将邻域的值用中心像素的值替换
void Masic(IplImage* img, IplImage* dst, int nSize)
{
int offset = (nSize-1)/2;
for ( int row = offset; row <img->height - offset; row= row+offset)
{
for( int col= offset; col<img->width - offset; col = col+offset)
{
int val0 = getPixel(img, row, col, 0);
int val1 = getPixel(img, row, col, 1);
int val2 = getPixel(img, row, col, 2);
for ( int m= -offset; m<offset; m++)
{
for ( int n=-offset; n<offset; n++)
{
setPixel(dst, row+m, col+n, 0, val0);
setPixel(dst, row+m, col+n, 1, val1);
setPixel(dst, row+m, col+n, 2, val2);
}
}
}
}
}

4、曝光过度问题处理

// 过度曝光原理：图像翻转，然后求原图与反图的最小值

void ExporeOver(IplImage* img, IplImage* dst)
{
for( int row =0; row height; row++)
{
uchar *data = (uchar*)img->imageData+ row* img->widthStep;
uchar *dstData = (uchar*)dst->imageData+ row* dst->widthStep;
for ( int col = 0; colwidth; col++)
{
for( int k=0; knChannels; k++)
{
uchar t1 = data[col*img->nChannels+k];
uchar t2 = 255 - t1;
dstData[col*img->nChannels+k] = min(t1,t2);
}
}
}
}

5、高反差保留 高反差保留主要是将图像中颜色、明暗反差较大两部分的交界处保留下来，比如图像中有一个人和一块石头，那么石头的轮廓线和人的轮廓线以及面部、服装等有明显线条的地方会变被保留，儿其他大面积无明显明暗变化的地方则生成中灰色。其表达形式为：dst = r*(img – Blur(img))。

Mat HighPass(Mat img)
{
Mat temp;
GaussianBlur(img, temp,Size(7,7),1.6,1.6);

int r=3;
Mat diff = img + r*(img-temp); //高反差保留算法
return diff;
}