马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
b=imread('11.bmp');
bb=rgb2gray(b);
c=imread('12.bmp');
cc=rgb2gray(c);
D=imsubtract(bb,cc);
[m,n] = size(D);
k = floor(7/2);
A2 = zeros(m+2*k,n+2*k);
A2(k+1:m+k,k+1:n+k) = D;
A2(1:k,1:k) = D(1,1);
A2(1:k,n+k+1:n+2*k) = D(1,n);
A2(m+k+1:m+2*k,n+k+1:n+2*k) = D(m,n);
A2(m+k+1:m+2*k,1:k) = D(m,1);
for i = 1:k
A2(i,k+1:n+k) =D(1,1:n);
A2(k+1:m+k,n+k+i) = D(1:m,n);
A2(m+k+i,k+1:n+k) = D(m,1:n);
A2(k+1:m+k,i) = D(1:m,1);
end;
B=zeros(m,n);
pixel_block = zeros(1,7^2);
for i = 1:m
for j = 1:n
k = floor(7/2);
pixel_block = reshape(A2(i:i+2*k,j:j+2*k),7^2,1);
sorted_block = sort(pixel_block);
median_position = ceil(7^2 / 2);
block_median = sorted_block (median_position);
B(i,j) = block_median;
end;
end;
B = uint8(B);
C=zeros(m,n);
for i = 1:m
for j = 1:(n-1)
C(i,j)=B(i,j+1);
end;
end;
for i = 1:m
C(i,n)=0;
end;
C = uint8(C);
CC=imsubtract(C,B);
T=10;
for i = 1:m
for j = 1:n
if CC(i,j)>T
CC(i,j)=255;
else CC(i,j)=0;
end;
end;
end;
DD=imsubtract(B,C);
for i = 1:m
for j = 1:n
if DD(i,j)>T
DD(i,j)=255;
else DD(i,j)=0;
end;
end;
end;
I=imadd(CC,DD);
E=zeros(m,n);
for i = 1:(m-1)
for j = 1:n
E(i,j)=B(i+1,j);
end;
end;
for j = 1:n
E(m,j)=0;
end;
E = uint8(E);
EE=imsubtract(E,B);
T=10;
for i = 1:m
for j = 1:n
if EE(i,j)>T
EE(i,j)=255;
else EE(i,j)=0;
end;
end;
end;
FF=imsubtract(B,E);
for i = 1:m
for j = 1:n
if FF(i,j)>T
FF(i,j)=255;
else FF(i,j)=0;
end;
end;
end;
J=imadd(EE,FF);
P=imadd(I,J);
imshow(P);
[m,n]=size(P);
W=zeros(m,n);
for i=2:m-1
for j=2:n-1
p1=P(i,j);
p2=P(i-1,j);
p3=P(i-1,j+1);
p4=P(i,j+1);
p5=P(i+1,j+1);
p6=P(i+1,j);
p7=P(i+1,j-1);
p8=P(i,j-1);
p9=P(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else W(i,j)=P(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
W(i,j)=P(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
W(i,j)=P(i,j);
else
if p2*p4*p6~=0|p4*p6*p8~=0
W(i,j)=P(i,j);
else W(i,j)=0;
end;
end;
end;
end;
end;
[m,n]=size(W);
R=zeros(m,n);
for i=2:m-1
for j=2:n-1
p1=W(i,j);
p2=W(i-1,j);
p3=W(i-1,j+1);
p4=W(i,j+1);
p5=W(i+1,j+1);
p6=W(i+1,j);
p7=W(i+1,j-1);
p8=W(i,j-1);
p9=W(i-1,j-1);
if p1==255
if p2==255
count2=1;
else count2=0;
end;
if p3==255
count3=1;
else count3=0;
end;
if p4==255
count4=1;
else count4=0;
end;
if p5==255
count5=1;
else count5=0;
end;
if p6==255
count6=1;
else count6=0;
end;
if p7==255
count7=1;
else count7=0;
end;
if p8==255
count8=1;
else count8=0;
end;
if p9==255
count9=1;
else count9=0;
end;
else R(i,j)=W(i,j);
end;
count=count2+count3+count4+count5+count6+count7+count8+count9;
if count<2|count>6
R(i,j)=W(i,j);
else
Q(1:8)=[p2,p3,p4,p5,p6,p7,p8,p9];
a=0;
for k=1:7
if Q(k)<Q(k+1)
a=a+1;
end;
end;
if a~=1
R(i,j)=W(i,j);
else
if p2*p4*p8~=0|p2*p6*p8~=0
R(i,j)=W(i,j);
else R(i,j)=0;
end;
end;
end;
end;
end;
figure,imshow(R) |