圖像均值濾波java代碼,數字圖像處理 均值濾波
編寫用均值濾波去噪的matlab程序,用兩種方法實現.(重謝)
方法一:filter2
成都創新互聯公司專注于成都企業網站建設,響應式網站設計,商城開發。成都網站建設公司,為成都等地區提供建站服務。全流程按需策劃,專業設計,全程項目跟蹤,成都創新互聯公司專業和態度為您提供的服務
clear?all;
I=imread('lena.bmp');
%讀入預處理圖像
imshow(I)
%顯示預處理圖像
K1=filter2(fspecial('average',3),I)/255;
%進行3*3均值濾波
K2=filter2(fspecial('average',5),I)/255;
%進行5*5均值濾波
K3=filter2(fspecial('average',7),I)/255;
%進行7*7均值濾波
figure,imshow(K1)
figure,imshow(K2)
figure,imshow(K3)
方法二:雙循環語句,移動平均法
%均值濾波
clc,clear;
f=imread('lena.bmp');
subplot(121),imshow(f),title('原圖');
f1=imnoise(f,'gaussian',0.002,0.0008);
%subplot(222),imshow(f1),title('添加高斯噪聲圖');
k1=floor(3/2)+1;
k2=floor(3/2)+1;
X=f1;
[M,N]=size(X);
uint8?Y=zeros(M,N);
funBox=zeros(3,3);
for?i=1:M-3
for?j=1:N-3
funBox=X(i:i+3,j:j+3);
s=sum(funBox(:));
h=s/9;
Y(i+k1,j+k2)=h;
end;
end;
Y=Y/255;
subplot(122),imshow(Y),title('均值濾波');
實現圖:
均值濾波
一. 均值濾波簡介和原理
均值濾波,是圖像處理中常用的手段,從頻率域觀點來看均值濾波是一種低通濾波器,高頻信號將會去掉。均值濾波可以幫助消除圖像尖銳噪聲,實現圖像平滑,模糊等功能。理想的均值濾波是用每個像素和它周圍像素計算出來的平均值替換圖像中每個像素。
? ? 以3*3均值濾波器為例,均值濾波器算法原理如下圖:
二. 用均值濾波器對椒鹽噪聲污染后的圖像去噪
? ? python 源碼:
import cv2
import numpy as np
# mean filter
def mean_filter(img, K_size=3):
H, W, C = img.shape
# zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
tmp = out.copy()
# filtering
for y in range(H):
? ? for x in range(W):
? ? ? ? for c in range(C):
? ? ? ? ? ? out[pad + y, pad + x, c] = np.mean(tmp[y: y + K_size, x: x + K_size, c])
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
return out
# Read image
img = cv2.imread("../paojie_sp1.jpg")
# Mean Filter
out = mean_filter(img, K_size=5)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
三. 實驗結果:
? ? 可以看到,均值濾波后,圖像中噪聲雖然有所減弱,但是圖像變模糊了。因為均值濾波器過濾掉了圖像中的高頻分量,所以圖像的邊緣都變模糊了。(去除一定量椒鹽噪聲,可以考慮使用中值濾波)
四. 參考內容:
怎樣用MATLAB實現中值和均值濾波
中值濾波樓上答了,5*5的均值濾波代碼 w2=fspecial('average',[5 5]); %% 先定義一個濾波器 h=imfilter(a,w2,'replicate'); %%讓圖像通過濾波器 imshow(h); imwrite(h,'8.jpg');
均值濾波是
I=medfilt2(a,[3 3],'symmetric')
可以在matlab中查詢medfilt函數的用法,本例是使用3*3的濾波器采用鏡像邊界法做均值濾波。
求分析均值濾波去除圖像噪聲效果不好的原因(有圖有代碼,VC++)
應該另外開辟一個空間保存新計算的圖像像素值,你這樣是把前面計算好的中值或者均值算到它臨近點的3*3區域里了,這樣的效果應該是圖像從左上到右下越來越白或者越來越黑。
對于椒鹽噪聲,中值濾波的效果好;對于高斯噪聲,均值濾波的效果好。
用matlab圖像處理均值濾波 不轉換成灰度 直接把彩色的圖片進行處理的代碼
rgb=imread('flower.jpg');
fR=rgb(:,:,1);
fG=rgb(:,:,2);
fB=rgb(:,:,3);
w=fspecial('average');
fR_filtered=imfilter(fR,w);
fG_filtered=imfilter(fG,w);
fB_filtered=imfilter(fB,w);
rgb_filtered=cat(3,fR_filtered,fG_filtered,fB_filtered);
用MATLAB編程實現均值濾波算法?
1:smoothingAverageFilterMain.mclc;clear;fid = fopen('lenai.raw');temp= fread(fid, [256,256]);LenaRaw=uint8(temp');subplot(1,2,1) Imshow(LenaRaw);title('原始圖像')subplot(1,2,2) Imshow(smoothingAverageFilter(LenaRaw,3));title('自制函數,使用用3*3模板,均值濾波圖像')2:smoothingAverageFilter.mfunction returnData=smoothingAverageFilter(arg,arg2)[Iwidth,Ilength]=size(arg);temp=double(arg);returnData=zeros(Iwidth,Ilength);totalLength=arg2*arg2;for i=1:Iwidth-arg2+1 for j=1:Ilength-arg2+1 % temp(i,j)=average(arg(i:i+arg2,j:j+arg2)); sum=0.0; for n=1:arg2 for k=1:arg2 sum=sum+temp(i+n-1,j+k-1); end end returnData(i,j)=sum/totalLength; endendreturnData=uint8(returnData);end
新聞標題:圖像均值濾波java代碼,數字圖像處理 均值濾波
分享地址:http://www.xueling.net.cn/article/hopsjc.html