![]() |
| 點圖可放大 |
圖片上經常會看到有一點一點雜訊這種雜訊稱之為胡椒鹽雜訊(Salt and pepper noise),這種雜訊我們可以利用中值濾波器將它給濾除,讓圖片比較接近原始的狀態。
什麼是中值濾波器呢?
假設圖檔裡面有一組矩陣內容如下,我們以97為中心點找出3*3的範圍所有的數字,然後將這組數字依照大小經過排列得到0,2,3,3,4,6,10,15,97,接下然我們選擇中間那個數字4 取代原本的97做輸出,這樣的做法則稱為中值濾波器。
#include <iostream>
#include <cstdlib>
#include "bmp.h"
using namespace std;
int R[MaxBMPSizeX][MaxBMPSizeY];
int G[MaxBMPSizeX][MaxBMPSizeY];
int B[MaxBMPSizeX][MaxBMPSizeY];
int width, height;
int main(int argc, char* argv[])
{
int x, y;
// 加強值
int value;
char* file_name;
// 圖檔名稱
file_name = "Butterfly_gray_pepper.bmp";
// 開啟並讀取全彩(24bits)bmp 影像圖檔,如果是灰階影像只要存取RGB其中一項即可!
// RGB 三個 channel 的值存入 R, G, B 三個自訂的陣列中
// 陣列大小規定是 MaxBMPSizeX * MaxBMPSizeY
// 同時取得該影像圖檔的高度和寬度
open_bmp( file_name, R, G, B, width, height);
// 影像處理(中值濾波)
for (x = 1 ; x < width-1 ; x++) {
for (y = 1; y < height-1 ; y++) {
int k = 0;
int window[9];
for (int xx = x - 1; xx < x + 2; ++xx)
for (int yy = y - 1; yy < y + 2; ++yy)
window[k++] = R[xx][yy];
// Order elements (only half of them)
for (int m = 0; m < 5; ++m)
{
int min = m;
for (int n = m + 1; n < 9; ++n)
if (window[n] < window[min])
min = n;
// Put found minimum element in its place
int temp = window[m];
window[m] = window[min];
window[min] = temp;
}
R[x][y]= window[4];
G[x][y]= window[4];
B[x][y]= window[4];
}
}
// 儲存處理結果至新的圖檔中
save_bmp("new_filter.bmp", R, G, B);
// 關閉 bmp 影像圖檔
close_bmp();
system("PAUSE");
return 0;
}
(執行環境Visual C 2012 不能執行請重新編譯)








沒有留言:
張貼留言
俗話說
凡走過必留下痕跡,凡住過必留下鄰居
凡爬過必留下樓梯,凡來過必留下IP
看過文章之後歡迎留下您寶貴的意見喔!