matlab-均值濾波


均值濾波

主要思想為鄰域平均法,即用幾個像素灰度的平均值來代替每個像素的灰度。有效抑制加性噪聲。
缺點:容易引起圖像模糊,可以對其進行改進,主要避開對景物邊緣的平滑處理。

均值濾波器的缺點是存在着邊緣模糊的問題。對椒鹽噪聲不起作用

 

 P5 = (P1+P2+P3+P4+P5+P6+P7+P8+P9)/9

 

 P5 = (P1+P2+P3+P4+P6+P7+P8+P9)/8   方便在硬件上做運算 >>3

matlab代碼

 1 clc
 2 clear
 3 clear all 
 4 close all
 5 %%%對圖像做均值濾波處理
 6 img = imread('1.png');
 7 figure(1)
 8 subplot(2,2,1),imshow(img),title('原始圖像')
 9 %%%將彩色圖像轉灰度圖像
10 img_gray = rgb2gray(img);
11 subplot(2,2,2),imshow(img_gray),title('RGB-GRAY灰度圖像')
12 %%%加入椒鹽噪聲
13 img_salt=imnoise(img_gray,'salt & pepper',0.05);
14 subplot(2,2,3),imshow(img_salt),title('加入椒鹽噪聲后')
15 %%%系統自帶的均值濾波  系統自帶的均值濾波輸入參數為2維圖像
16 img_mid=filter2(fspecial('average',3),img_salt)/255;
17 subplot(2,2,4),imshow(img_mid),title('對噪聲圖像均值濾波后');

Mean_Img = img_salt;
[ROW,COL]= size(Mean_Img);
for r = 2:1:ROW-1
for c = 2:1:COL-1
Mean_Img(r,c) = (img_salt(r-1, c-1) + img_salt(r-1, c) + img_salt(r-1, c+1) + img_salt(r, c-1) + img_salt(r, c) + img_salt(r, c+1) + img_salt(r+1, c-1) + img_salt(r+1, c) + img_salt(r+1, c+1)) / 9;
end
end
figure(2)
imshow(img_mid),title('對噪聲圖像均值濾波后');


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM