當影像銳度如果太高希望使其看起來較為自然一點時可採用"Mean Filter"
以3*3的 「遮罩」為例,透過「Spatial Convolution」對整張影像進行處理。
取得遮罩之像素矩陣後加總除以總數(計算平均值)。
實驗成果如下圖所示。可發現影像中的雜訊點較為模糊(平滑)。
C#
private Bitmap meanFilter(Bitmap bitmap, int maskWidth, int maskHeight)
{
int width = bitmap.Width, height = bitmap.Height;
Bitmap dstBitmap = new Bitmap(bitmap);
byte[,] pix = ImageExtract.getimageArray(bitmap);
byte[,] resPix = new byte[3, width * height];
for (int y = 1; y < (height - 1); y++)
{
for (int x = 1; x < (width - 1); x++)
{
//b,g,r
for (int c = 0; c < 3; c++)
{
//mask
int current = x + y * width;
byte[] mask = new byte[maskWidth * maskHeight];
for (int my = 0; my < maskHeight; my++)
for (int mx = 0; mx < maskWidth; mx++)
{
int pos = current + (mx - 1) + ((my - 1) * width);
mask[mx + my * maskWidth] = pix[c, pos];
}
resPix[c, current] = calcMeans(mask);
}
}
}
ImageExtract.writeImageByArray(resPix, dstBitmap);
return dstBitmap;
}
完整程式碼 : https://github.com/Lung-Yu/ImageToolBox/blob/master/ImageProcessToolBox/MeanFilter.cs
文章標籤
全站熱搜