图像处理——去除拍摄电子屏幕时产生的彩色波纹
1.中值滤波
1.将所求像素点周围的8个像素点以及本身存于一个数组中,再分别建立三个数组,分别是numR[9],numG[9],numB[9]。用于分别存储前九个像素点的R,G,B值,再依次对numR[9],numG[9],numB[9]进行快速排序,取三个数组的中值作为所求像素点的新的RGB值。如图1.1,图1.2。
图1.1
均值滤波运算过程如下
图1.2
中值滤波GDI+代码如下
for (int x = 0; x < w - 1; x++)
{
for (int y = 0; y < h - 1; y++)
{
int n = 0;
for (int col = -2; col <= 2; col++)
{
for (int row = -2; row <= 2; row++)
{
src->GetPixel(x + col, y + row, &color[n]);
numR[n] = color[n].GetR();
numG[n] = color[n].GetG();
numB[n] = color[n].GetB();
n++;
}
}
qsort(numR, 25, sizeof(numR[0]), cmp);
qsort(numG, 25, sizeof(numG[0]), cmp);
qsort(numB, 25, sizeof(numB[0]), cmp);
dest->SetPixel(x,y,Color(BYTE(numR[12]),BYTE(numG[12]),BYTE(numB[1])));
}
}
graph->DrawImage(dest, w+2, 0, w, h);
int cmp(const void*a, const void*b)
{
return*(int*)a - *(int*)b;
}
2.混合均值滤波
混合均值滤波是在均值滤波的基础上改进得来的一种算法,类似于混合中值滤波
图2.1
混合均值滤波GDI+算法如下
for (int x = 0; x < w - 1; x++)
{
for (int y = 0; y < h - 1; y++)
{
int n = 0;
for (int col = -1; col <= 1; col++)
{
for (int row = -1; row <= 1; row++)
{
src->GetPixel(x + col, y + row, &color[n]);
n++;
}
}
total_numR[0] = (color[1].GetR() + color[3].GetR() + color[4].GetR() + color[5].GetR() + color[7].GetR()) / 5;
total_numR[1] = (color[0].GetR() + color[2].GetR() + color[4].GetR() + color[6].GetR() + color[8].GetR()) / 5;
total_numR[2] = (total_numR[0] + total_numR[1] + color[4].GetR()) / 3;
total_numG[0] = (color[1].GetG() + color[3].GetG() + color[4].GetG() + color[5].GetG() + color[7].GetG()) / 5;
total_numG[1] = (color[0].GetG() + color[2].GetG() + color[4].GetG() + color[6].GetG() + color[8].GetG()) / 5;
total_numG[2] = (total_numG[0] + total_numG[1] + color[4].GetG()) / 3;
total_numB[0] = (color[1].GetB() + color[3].GetB() + color[4].GetB() + color[5].GetB() + color[7].GetB()) / 5;
total_numB[1] = (color[0].GetB() + color[2].GetB() + color[4].GetB() + color[6].GetB() + color[8].GetB()) / 5;
total_numB[2] = (total_numB[0] + total_numB[1] + color[4].GetB()) / 3;
dest->SetPixel(x, y, Color(BYTE(total_numR[2]+20), BYTE(total_numG[2]+20), BYTE(total_numB[2]+20)));
}
}
混合均值滤波可以将图片的彩色波纹减弱,并对原图产生较小的模糊效果,而中值滤波可以去掉弱彩色波纹,所以两个算法一起使用会达到对彩色波纹取出的效果,但由于中值滤波特性,在将波纹去掉的同时,图片也可能有较大程度的模糊。