Matlab 版 (精华区)
发信人: fork (撒哈拉沙漠的沙), 信区: Matlab
标 题: 一个通用的模板处理程序
发信站: 哈工大紫丁香 (Fri Mar 19 13:01:28 2004), 站内信件
先给出测试程序:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename='lena_std.jpg';
fmt='jpg';
A = imread(filename,fmt);
%T=[1,-2,1;-2,4,-2;1,-2,1];
T=[1,2,1;0,0,0;-1,-2,-1];
A=A(:,:,1);
Y= SmartMask(double(A),T);
imshow(Y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
以下给出通用的模板处理程序,其中X可以是任意2维图像矩阵,T为任意2维
模板矩阵。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Y=SmartMask(X,T)
[M,N]=size(X);
[m,n]=size(T);
left=ceil(m/2-1);
top=ceil(n/2-1);
right=floor(m/2);
bottom=floor(n/2);
Y=zeros(M,N);
extendX=zeros(M+left+right,N+top+bottom);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the 4 corners
for i=1:left
for j=1:top
extendX(i,j)=X(1,1);
end
end
for i=1+left+M:left+M+right
for j=1:top
extendX(i,j)=X(1,N);
end
end
for i=1:left
for j=1+top+N:top+N+bottom
extendX(i,j)=X(M,1);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the 4 lines
for i=1+left+M:left+M+right
for j=1+top+N:top+N+bottom
extendX(i,j)=X(M,N);
end
end
for i=1:left
extendX(1+top:top+N,i)=X(:,1);
end
for i=left+M+1:left+M+right
extendX(1+top:top+N,i)=X(:,M);
end
for i=1:top
extendX(i,left+1:left+M)=X(1,:);
end
for i=top+N+1:top+N+bottom
extendX(i,left+1:left+M)=X(N,:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the mid square
extendX(1+top:top+N,1+left:left+M)=X;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%process the square
for i=1+left:left+M
for j=1+top:top+N
temp=0;
for Ti=1:m
for Tj=1:n
temp=temp+extendX(i+Ti-1-left,j+Tj-1-top)*T(Ti,Tj);
end
end
Y(i-left,j-top)=temp;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% make the range
max=0;
min=1000;
for i=1:M
for j=1:N
if(min>Y(i,j))
min=Y(i,j);
end
if(max<Y(i,j))
max=Y(i,j);
end
end
end
inter=256/(max-min);
for i=1:M
for j=1:N
Y(i,j)=(Y(i,j)-min)*inter;
end
end
Y=uint8(Y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--
※ 来源:.哈工大紫丁香 bbs.hit.edu.cn [FROM: 219.217.225.36]
※ 修改:.fork 於 Mar 29 10:57:42 2004 修改本文.[FROM: 219.217.225.36]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.441毫秒