在图像处理中为了处理上的方便经常需要对图像进行分块,分块后需要对整块进行处理。
此处给出了两种定位篡改块的处理方式。
lilizong【at】gmail
方式1:直接对所在块操作。
%%%%%%%%%%%%
clear;
clc;
key1=32;
key2=32;
A=imread('../image/hetong.bmp');
V=imread('../image/2.bmp');
A=im2bw(A);
V=im2bw(V);
[m,n]=size(A);
AC=cell(key1,key2);%将图像分为8*8的块
mkey1=floor(m/key1);
nkey2=floor(m/key2);
for i=1:mkey1
for j=1:nkey2
AC{i,j}=A((i-1)*key1+1:i*key1,(j-1)*key2+1:j*key2);
end
end
VC=cell(key1,key2);%将图像分为8*8的块
for i=1:mkey1
for j=1:nkey2
VC{i,j}=V((i-1)*key1+1:i*key1,(j-1)*key2+1:j*key2);
end
end
a=zeros(key1,key2);
%用来作为临时数组,保存从每个8*8块中提取出来的数值
B=zeros(mkey1,nkey2);
%用来存放8*8分块的均值
for i=1:(mkey1)
for j=1:(nkey2)
a=AC(i,j);
ab=cell2mat(a);
%取得当前的块,并将之转化
w=0;
for ii=1:key1
for jj=1:key2
w=w+ab(ii,jj);
end
end
v=VC(i,j);
av=cell2mat(v);
%取得当前的块,并将之转化
w2=0;
for ii=1:key1
for jj=1:key2
w2=w2+av(ii,jj);
end
end
if w~=w2 B(i,j)=0;
else
B(i,j)=1;
end
end
end
subplot(1,3,1),imshow(A);
set(gca,'Title',text('String','原始图像'))
subplot(1,3,2),imshow(V);
set(gca,'Title',text('String','篡改图像'))
subplot(1,3,3),imshow(B,[]);
set(gca,'Title',text('String',[num2str(key1),'*',num2str(key2),'定位图像']))%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
方式2:直接将分块处理为一个像素,然后再对定位图像进行缩放。
A=imread('../image/hetong.bmp');
V=imread('../image/2.bmp');
av=xor(A,V);
A=av;
figure,imshow(av);
key1=8;
key2=8;
mkey1=512/key1;
nkey2=512/key2;
B=cell(key1,key2);%将图像分为8*8的块
for i=1:mkey1
for j=1:nkey2
B{i,j}=A((i-1)*key1+1:i*key1,(j-1)*key2+1:j*key2);
end
end
a=zeros(key1,key2);
%用来作为临时数组,保存从每个8*8块中提取出来的数值
averageB=zeros(mkey1,nkey2);
%用来存放8*8分块的均值
for i=1:(mkey1)
for j=1:(nkey2)
b=B(i,j);
b=cell2mat(b);
%取得当前的块,并将之转化
w=0;
for ii=1:key1
for jj=1:key2
if b(ii,jj)==1
w=100;
end
end
end if w==100
averageB(i,j)=0;
else
averageB(i,j)=255;
end
end
end
w=imresize(averageB,key1);
figure,imshow(w,[]);
w=double(w);
imwrite(w,'../image/re.bmp');