width=size(imread(‘文件名'),2); %获取图像宽
length=size(imread(‘文件名'),1); %获取图像长
g=imread(['D:\文件及下载相关\桌面\代码 - 副本\代码\Image\Set5\' Names{k}]);
g2=im2bw(g,0.5);
[dm,dn]=size(g2);
batchedge.m:
clear;clc;close all; Files=dir('D:\文件及下载相关\桌面\代码 - 副本\代码\Image\Set5\*.bmp'); N=length(Files); Names={7}; for k=1:N Names{k}=Files(k).name; g=imread(['D:\文件及下载相关\桌面\代码 - 副本\代码\Image\Set5\' Names{k}]); g2=im2bw(g,0.5); [dm,dn]=size(g2); image_new=zeros(2*dm,2*dn); for i=1:2*dm for j=1:2*dn %进行插值运算 if (rem(i,2)==1)&&(rem(j,2)==1) image_new(i,j)=g(round(i/2),round(j/2)); else image_new(i,j)=0; end end end Ax=image_new;Ay=image_new; gaosi=[-1/16 9/16 9/16 -1/16]; for i=4:2:2*dm-4 for j=4:2:2*dn-4 x=[image_new(i-3,j-3) image_new(i-1,j-1) image_new(i+1,j+1) image_new(i+3,j+3)]; y=[image_new(i+3,j-3) image_new(i+1,j-1) image_new(i-1,j+1) image_new(i-3,j+3)]; Ax(i,j)=sum(x.*gaosi); Ay(i,j)=sum(y.*gaosi); end end for i=4:2:2*dm-4 for j=4:2:2*dn-4 zx=[Ax(i-2,j-2) Ax(i-1,j-1) Ax(i,j) Ax(i+1,j+1) Ax(i+2,j+2)]; zy=[Ay(i+2,j-2) Ay(i+1,j-1) Ay(i,j) Ay(i-1,j+1) Ay(i-2,j+2)]; x=[image_new(i-1,j-1) image_new(i+1,j+1) image_new(i+1,j-1) image_new(i-1,j+1)]; mx=mean(x); pzx=sum((zx-mx).^2)/4+0.1; pzy=sum((zy-mx).^2)/4+0.1; wx=pzy/(pzx+pzy);wy=1-wx; image_new(i,j)=wx*Ax(i,j)+wy*Ay(i,j); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Ax=image_new;Ay=image_new; for i=4:2:2*dm-4 for j=5:2:2*dn-4 x=[image_new(i-3,j) image_new(i-1,j) image_new(i+1,j) image_new(i+3,j)]; y=[image_new(i,j-3) image_new(i,j-1) image_new(i,j+1) image_new(i,j+3)]; Ax(i,j)=sum(x.*gaosi); Ay(i,j)=sum(y.*gaosi); end end for i=4:2:2*dm-4 for j=5:2:2*dn-4 zx=[Ax(i-2,j) Ax(i-1,j) Ax(i,j) Ax(i+1,j) Ax(i+2,j)]; zy=[Ay(i,j-2) Ay(i,j-1) Ay(i,j) Ay(i,j+1) Ay(i,j+2)]; x=[image_new(i-1,j) image_new(i,j-1) image_new(i+1,j) image_new(i,j+1)]; mx=mean(x); pzx=sum((zx-mx).^2)/4+0.1; pzy=sum((zy-mx).^2)/4+0.1; wx=pzy/(pzx+pzy);wy=1-wx; image_new(i,j)=wx*Ax(i,j)+wy*Ay(i,j); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Ax=image_new;Ay=image_new; for i=5:2:2*dm-4 for j=4:2:2*dn-4 x=[image_new(i-3,j) image_new(i-1,j) image_new(i+1,j) image_new(i+3,j)]; y=[image_new(i,j-3) image_new(i,j-1) image_new(i,j+1) image_new(i,j+3)]; Ax(i,j)=sum(x.*gaosi); Ay(i,j)=sum(y.*gaosi); end end for i=5:2:2*dm-4 for j=4:2:2*dn-4 zx=[Ax(i-2,j) Ax(i-1,j) Ax(i,j) Ax(i+1,j) Ax(i+2,j)]; zy=[Ay(i,j-2) Ay(i,j-1) Ay(i,j) Ay(i,j+1) Ay(i,j+2)]; x=[image_new(i-1,j) image_new(i,j-1) image_new(i+1,j) image_new(i,j+1)]; mx=mean(x); pzx=sum((zx-mx).^2)/4+0.1; pzy=sum((zy-mx).^2)/4+0.1; wx=pzy/(pzx+pzy);wy=1-wx; image_new(i,j)=wx*Ax(i,j)+wy*Ay(i,j); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure; image_new=uint8(image_new); image_new=uint8(image_new); path='D:\文件及下载相关\桌面\代码 - 副本\代码\Image\Set5\'; file=Files(k).name; pathfile=fullfile(path,file); imwrite(image_new,pathfile,'bmp'); end