1 function [ ] = picSmooth0(A ,s) 2 %picSmooth0 此处显示有关此函数的摘要 3 %picSmooth0(A,s) A为图像矩阵 s为3*3变换矩阵 4 % 此处显示详细说明 5 [M,N,a]=size(A); 6 if a == 3 7 B=rgb2gray(A); 8 else 9 B=A; 10 end 11 B=double(B); %防止溢出 12 P=zeros(M,N); 13 k=0; 14 sum=0; 15 [m,n]=size(s); 16 if( m~=3 || n~=3) 17 fprintf(‘请输入正确的3*3空间域平滑模板‘); 18 else 19 for i=1:3 20 for j=1:3 21 sum=sum+s(i,j); 22 end 23 end 24 25 for i=1:M 26 for j = 1:N 27 k=0; 28 if((0<i-1&&i-1<=M) && (0<j-1&&j-1<=N)) 29 k=k+s(1,1)*B(i-1,j-1); 30 end 31 if(0<i&&i<=M && 0<j-1&&j-1<=N) 32 k=k+s(2,1)*B(i,j-1); 33 end 34 if(0<i+1&&i+1<=M && 0<j-1&&j-1<=N) 35 k=k+s(3,1)*B(i+1,j-1); 36 end 37 38 if(0<i-1&&i-1<=M && 0<j&&j<=N) 39 k=k+s(1,2)*B(i-1,j); 40 end 41 if(0<i&&i<=M && 0<j&&j<=N) 42 k=k+s(2,2)*B(i,j); 43 end 44 if(0<i+1&&i+1<=M && 0<j&&j<=N) 45 k=k+s(3,2)*B(i+1,j); 46 end 47 48 if(0<i-1&&i-1<=M && 0<j+1&&j+1<=N) 49 k=k+s(1,3)*B(i-1,j+1); 50 end 51 if(0<i&&i<=M && 0<j+1&&j+1<=N) 52 k=k+s(2,3)*B(i,j+1); 53 end 54 if(0<i+1&&i+1<=M && 0<j+1&&j+1<=N) 55 k=k+s(3,3)*B(i+1,j+1); 56 end 57 P(i,j)=k; 58 end 59 end 60 end 61 P=P/sum; 62 P=uint8(P); 63 B=uint8(B); 64 65 figure; 66 imshow(B); 67 figure; 68 imshow(P); 69 70 end
转载请注明来源
时间: 2024-10-24 05:54:00