1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| a=imread('file.jpg'); t=15; s=size(a); b=zeros(s(1)-1,s(2)-1,3); d=zeros(s(1)-1,s(2)-1,3); b1=zeros(s(1)-1,s(2)-1,2); for i=1:s(1)-1 for j=1:s(2)-1 for k=1:3 b(i,j,k)=a(i,j,k)-a(i+1,j,k); d(i,j,k)=a(i,j,k)-a(i,j+1,k); end end end for i=1:s(1)-1 for j=1:s(2)-1 b1(i,j,1)=b(i,j,1)+b(i,j,2)+b(i,j,3); b1(i,j,2)=d(i,j,1)+d(i,j,2)+d(i,j,3); end end c=zeros(s(1)-1,s(2)-1); for i=1:s(1)-1 for j=1:s(2)-1 if b1(i,j,1)>t || b1(i,j,2)>t c(i,j)=255; else c(i,j)=0; end end end
e=decrease(c,5,3);
e=increase(e,5,4);
imshow(e)
function result=decrease(data,value,times) result=data; s=size(data); for t=1:times for i=2:s(1)-1 for j=2:s(2)-1 if data(i,j)==255 if data(i-1,j-1)+data(i,j-1)+data(i+1,j-1)+data(i-1,j)+data(i+1,j)+data(i-1,j+1)+data(i,j+1)+data(i+1,j+1)<255*value result(i,j)=0; end end end end data=result; end end
function result=increase(data,value,times) result=data; s=size(data); for t=1:times for i=2:s(1)-1 for j=2:s(2)-1 if data(i,j)==0 if data(i-1,j-1)+data(i,j-1)+data(i+1,j-1)+data(i-1,j)+data(i+1,j)+data(i-1,j+1)+data(i,j+1)+data(i+1,j+1)>255*value result(i,j)=255; end end end end data=result; end end
|