先看基于模板的
1.
3x3
1/9 | 1/9 | 1/9 |
---|---|---|
1/9 | 1/9 | 1/9 |
1/9 | 1/9 | 1/9 |
这个模板很明显,就是把当前像素的值用周围的像素值的平均值代替,产生模糊效果
// 模糊处理
void ImageProcess::BlurImage(CImage* srcImage,CImage* outImage, int blurType)
...{
CWaitCursor WaitCursor;
//设置进度条范围
((CMainFrame*)AfxGetMainWnd())->SetProgressRange(0,srcImage->GetWidth());
for(int x = 0;x < srcImage->GetWidth();x )
...{
//设置当前进度
((CMainFrame*)AfxGetMainWnd())->SetProgressPos(x);
for(int y = 0;y < srcImage->GetHeight();y )
...{
int r = 0,g = 0,b = 0;
for(int col = -blurType;col <= blurType;col )
...{
for(int row = -blurType;row <= blurType;row )
...{
COLORREF pixel;
//防止越界
if( (x row) < 0 ||(x row) >= srcImage->GetWidth() ||
(y col) < 0 || (y col) >= srcImage->GetHeight())
pixel = srcImage->GetPixel(x,y);
else
pixel = srcImage->GetPixel(x row,y col);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
}
}
//取平均值
int blocks = (blurType*2 1)*(blurType*2 1);
r /= blocks;
g /= blocks;
b /= blocks;
//写回图像
outImage->SetPixelRGB(x,y,r,g,b);
}
}
}
同样,也有5x5,7x5等等的模板,模板越大,处理后的图像就越模糊
2.
0 | -1 | 0 |
---|---|---|
-1 | 4 | -1 |
0 | -1 | 0 |
这是另一种模板,是为了增强当前像素与周围像素的差别,产生的效果就是:锐化 此时的模板叫Laplacian模板,当然,这不是唯一的一种形式,例如:
-1 -2 -1
0 0 0
1 2 1
-1 0 1
-2 0 2
-1 0 1
是两种简化运算的近似效果,可以取得更快的处理速度
// 锐化图像
void ImageProcess::SharpImage(CImage* srcImage,CImage* outImage, int sharpType)
...{
int Laplacian[3][9] =
...{
...{0,-1,0,-1,4,-1,0,-1,0},
...{-1,-2,-1,0,0,0,1,2,1},
...{-1,0,1,-2,0,2,-1,0,1}
};
CWaitCursor WaitCursor;
//设置进度条范围
((CMainFrame*)AfxGetMainWnd())->SetProgressRange(0,srcImage->GetWidth());
for(int x = 0;x < srcImage->GetWidth();x )
...{
//设置当前进度
((CMainFrame*)AfxGetMainWnd())->SetProgressPos(x);
for(int y = 0;y < srcImage->GetHeight();y )
...{
int r = 0,g = 0,b = 0,index = 0;;
for(int col = -1;col <= 1;col )
...{
for(int row = -1;row <= 1;row )
...{
COLORREF pixel;
//防止越界
if( (x row) < 0 ||(x row) >= srcImage->GetWidth() ||
(y col) < 0 || (y col) >= srcImage->GetHeight())
pixel = srcImage->GetPixel(x,y);
else
pixel = srcImage->GetPixel(x row,y col);
r = GetRValue(pixel) * Laplacian[sharpType][index];
g = GetGValue(pixel) * Laplacian[sharpType][index];
b = GetBValue(pixel) * Laplacian[sharpType][index];
index ;
}
}
//增强
COLORREF pixel = srcImage->GetPixel(x,y);
//r = GetRValue(pixel);
//g = GetGValue(pixel);
//b = GetBValue(pixel);
//处理颜色值溢出
r = (r > 255) ? 255 : r;
r = (r < 0) ? 0 : r;
g = (g > 255) ? 255 : g;
g = (g < 0) ? 0 : g;
b = (b > 255) ? 255 : b;
b = (b < 0) ? 0 : b;
//写回图像
outImage->SetPixelRGB(x,y,r,g,b);
}
}
}
3.基本的灰度变换 这应该是最简单的变换了,s=f(x,y),s为处理后的像素颜色值,而f(x,y)是什么函数,就决定了处理效果 如: 图像反转:s=L-1-r,常用于医学上的透视图的处理 对数变换:s=cLog(1 r),可以扩展被压缩的高值图像中的暗像素 幂次变换:s=cr^γ,这就是传说中的伽马校正! 代码示例,仅有对数变换,其它同理
// 对数变换
void ImageProcess::LogTransform(CImage* srcImage, CImage* outImage, int c)
...{
CWaitCursor WaitCursor;
//设置进度条范围
((CMainFrame*)AfxGetMainWnd())->SetProgressRange(0,srcImage->GetWidth());
for(int x = 0;x < srcImage->GetWidth();x )
...{
//设置当前进度
((CMainFrame*)AfxGetMainWnd())->SetProgressPos(x);
for(int y = 0;y < srcImage->GetHeight();y )
...{
int r = 0,g = 0,b = 0;
COLORREF pixel = srcImage->GetPixel(x,y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
r = (int)(c * log(1.0f r));
g = (int)(c * log(1.0f g));
b = (int)(c * log(1.0f b));
//处理颜色值溢出
r = (r > 255) ? 255 : r;
r = (r < 0) ? 0 : r;
g = (g > 255) ? 255 : g;
g = (g < 0) ? 0 : g;
b = (b > 255) ? 255 : b;
b = (b < 0) ? 0 : b;
//写回图像
outImage->SetPixelRGB(x,y,r,g,b);
}
}
}