图像的边界匹配
function bmaclear all
close all
R=imread('1.bmp');
C=imread('2.bmp');%read two frames
R=uint8(R);
figure;imshow(R);
M=288;
N=352;
R=double(R);
C=double(C);
%lose
framesize=M*N; % 一帧所有像素
blocksize=8; % 块的大小
vnum=M/blocksize; % 垂直方向块个数
hnum=N/blocksize; % 水平方向块个数
for i=1:vnum
i1=(i-1)*blocksize+1;
for j=1:hnum
j1=(j-1)*blocksize+1;
if mod(i,2)==0 & mod(j,2)==0 & i~=vnum & j~=hnum % 离散丢失
%if ((i==2 | i==10 | i==14) & j~=1 & j~=hnum)| (i==6 & j>=6 & j<=20) | (i==7 & j>=12 & j<=18) | (i==13 & j>=6 & j<=12) | (i==15 & j>=8 & j<=18)
%if ((i==2 | i==3) & j~=1 & j~=hnum) | ((i==9 | i==10) & j~=1 & j~=hnum) | ((i==14 | i==15) & j~=1 & j~=hnum)
%if (mod(i,2)==0 & i~=vnum & j~=1 & j~=hnum) | (mod(i,3)==0 & i~=vnum & j~=1 & j~=hnum)
concealmentok(i,j)=0;
for m=0:blocksize-1
for n=0:blocksize-1
E(i1+m,j1+n)=0; % 丢失的像素值以0代替
end
end
else
concealmentok(i,j)=1;
for m=0:blocksize-1
for n=0:blocksize-1
E(i1+m,j1+n)=C(i1+m,j1+n);
end
end
end
reMovVector(i,j).x=0;
reMovVector(i,j).y=0;
end
end
E=uint8(E);
figure;imshow(E);
imwrite(E,'erro2.bmp');
E=double(E);
%当前帧C图像2,前一帧R图像1,差错帧E图像erro2
tic
for i=1:vnum
for j=1:hnum
if concealmentok(i,j)==0
min=100000;
for k=(i-1)*blocksize-7:(i-1)*blocksize+9
for l=(j-1)*blocksize-7:(j-1)*blocksize+9
err2=0;
if concealmentok(i,j-1)~=0 % 左
for n=1:blocksize
err2=err2+abs(R(k+n-1,l)-E((i-1)*blocksize+n,(j-1)*blocksize));
end
end
if concealmentok(i-1,j)~=0 % 上
for n=1:blocksize
err2=err2+abs(R(k,l+n-1)-E((i-1)*blocksize,(j-1)*blocksize+n));
end
end
if concealmentok(i,j+1)~=0 % 右
for n=1:blocksize
err2=err2+abs(R(k+n-1,l+blocksize-1)-E((i-1)*blocksize+n,j*blocksize+1));
end
end
if concealmentok(i+1,j)~=0 % 下
for n=1:blocksize
err2=err2+abs(R(k+blocksize-1,l+n-1)-E(i*blocksize+1,(j-1)*blocksize+n));
end
end
err=err2;
if err<=min
min=err;
reMovVector(i,j).x=(i-1)*blocksize+1-k; % 运动矢量按像素算
reMovVector(i,j).y=(j-1)*blocksize+1-l;
end
end
end
for m=1:blocksize
for n=1:blocksize
E((i-1)*blocksize+m,(j-1)*blocksize+n)=R((i-1)*blocksize+m-reMovVector(i,j).x,(j-1)*blocksize+n-reMovVector(i,j).y);
end
end
concealmentok(i,j)=1;
end
end
end
toc
figure;
E=uint8(E);
figure;imshow(E);
imwrite(E,'berrocon21.bmp');
E=double(E);
s=0; %计算掩盖后帧与原始帧之间的PSNR
for i=1:M
for j=1:N
s=s+(C(i,j)-E(i,j))^2;
end
end
mse=s/(M*N);
p=10*log10(255*255/mse)