📄 framedif3.m
字号:
%求解视频的帧差分布 (方法三:X2结合分块法)
%读取视频
clear all;
clc;
tic;%start time
shot=mpgread('..\videos\cut.mpg',[1:200],'truecolor');%读取视频
%shot=aviread('video\skiing.avi');
frames=size(shot,2);
w=size(shot(1,1).cdata,1);%width
h=size(shot(1,1).cdata,2);%height
clf;
%axis off;
%movie(shot,1,25,[50 50 0 0]);
%gray
count1=zeros(16,256);
count2=zeros(16,256);
%rgb
countr1=zeros(16,256);
countr2=zeros(16,256);
countg1=zeros(16,256);
countg2=zeros(16,256);
countb1=zeros(16,256);
countb2=zeros(16,256);
%%%%
framedif=zeros(frames,16);
framedifr=zeros(frames,16);
framedifg=zeros(frames,16);
framedifb=zeros(frames,16);
framedifrgb=zeros(frames,16);
%%%%%%%%%%%求帧间差%%%%%%%%%%%%%%
divnum=64;%district num
phase=256/divnum;%division
%%
gray1=double(rgb2gray(shot(1,1).cdata));
%%%%%%%
r=double(shot(1,1).cdata(:,:,1));
g=double(shot(1,1).cdata(:,:,2));
b=double(shot(1,1).cdata(:,:,3));
%%%partitioning
xblocksize=round(w/4-0.5);%%
yblocksize=round(h/4-0.5);%%
for xblocknum=1:4
for yblocknum=1:4
tempx1=(xblocknum-1)*xblocksize+1;
tempx2=tempx1+xblocksize-1;
tempy1=(yblocknum-1)*yblocksize+1;
tempy2=tempy1+yblocksize-1;
for j=tempx1:tempx2
for k=tempy1:tempy2
temp3=xblocknum+(yblocknum-1)*4;
count1(temp3,gray1(j,k)+1)=count1(temp3,gray1(j,k)+1)+1;
countr1(temp3,r(j,k)+1)=countr1(temp3,r(j,k)+1)+1;
countg1(temp3,g(j,k)+1)=countg1(temp3,g(j,k)+1)+1;
countb1(temp3,b(j,k)+1)=countb1(temp3,b(j,k)+1)+1;
end
end
end
end
district1=zeros(16,divnum);
district2=zeros(16,divnum);
%%%%%%%%%%%%%%%%%%%%%%%%%%
districtr1=zeros(16,divnum);
districtr2=zeros(16,divnum);
districtg1=zeros(16,divnum);
districtg2=zeros(16,divnum);
districtb1=zeros(16,divnum);
districtb2=zeros(16,divnum);
for i=1:16
for m=1:divnum
for n=1:phase
district1(i,m)=district1(i,m)+count1(i,(m-1)*phase+n);
districtr1(i,m)=districtr1(i,m)+countr1(i,(m-1)*phase+n);
districtg1(i,m)=districtg1(i,m)+countg1(i,(m-1)*phase+n);
districtb1(i,m)=districtb1(i,m)+countb1(i,(m-1)*phase+n);
end
end
end
for i=2:frames
%for c=1:3
gray2=double(rgb2gray(shot(1,i).cdata));
r=double(shot(1,i).cdata(:,:,1));
g=double(shot(1,i).cdata(:,:,2));
b=double(shot(1,i).cdata(:,:,3));
%%%%%%%
for xblocknum=1:4
for yblocknum=1:4
tempx1=(xblocknum-1)*xblocksize+1;
tempx2=tempx1+xblocksize-1;
tempy1=(yblocknum-1)*yblocksize+1;
tempy2=tempy1+yblocksize-1;
for j=tempx1:tempx2
for k=tempy1:tempy2
temp3=xblocknum+(yblocknum-1)*4;
count2(temp3,gray2(j,k)+1)=count2(temp3,gray2(j,k)+1)+1;
countr2(temp3,r(j,k)+1)=countr2(temp3,r(j,k)+1)+1;
countg2(temp3,g(j,k)+1)=countg2(temp3,g(j,k)+1)+1;
countb2(temp3,b(j,k)+1)=countb2(temp3,b(j,k)+1)+1;
end
end
end
end
%%%%%%%%
for block=1:16
for m=1:divnum
for n=1:phase
district2(block,m)=district2(block,m)+count2(block,(m-1)*phase+n);
districtr2(block,m)=districtr2(block,m)+countr2(block,(m-1)*phase+n);
districtg2(block,m)=districtg1(block,m)+countg2(block,(m-1)*phase+n);
districtb2(block,m)=districtb1(block,m)+countb2(block,(m-1)*phase+n);
end
end
end
%%%%%%%%%%%%这里暂时没有去除变化最大的块%%%%%%
for block=1:16
for m=1:divnum
if district2(block,m)~=0||district1(block,m)~=0
framedif(i,block)=framedif(i,block)+(district2(block,m)-district1(block,m))*(district2(block,m)-district1(block,m))/max(district1(block,m),district2(block,m));
end
if districtr2(block,m)~=0||districtr1(block,m)~=0
framedifr(i,block)=framedifr(i,block)+(districtr2(block,m)-districtr1(block,m))*(districtr2(block,m)-districtr1(block,m))/max(districtr1(block,m),districtr2(block,m));
end
if districtg2(block,m)~=0||districtg1(block,m)~=0
framedifg(i,block)=framedifg(i,block)+(districtg2(block,m)-districtg1(block,m))*(districtg2(block,m)-districtg1(block,m))/max(districtg1(block,m),districtg2(block,m));
end
if districtb2(block,m)~=0||districtb1(block,m)~=0
framedifb(i,block)=framedifb(i,block)+(districtb2(block,m)-districtb1(block,m))*(districtb2(block,m)-districtb1(block,m))/max(districtb1(block,m),districtb2(block,m));
end
end
framedifrgb(i,block)=(framedifr(i,block)+framedifg(i,block)+framedifb(i,block))/3;
end
%framedif(i,1)=framedif(i,1)/(2*w*h);
%framedifr(i,1)=framedifr(i,1)/(2*w*h);
%framedifg(i,1)=framedifg(i,1)/(2*w*h);
%framedifb(i,1)=framedifb(i,1)/(2*w*h);
framedifsum=sum(framedif');
framedifrsum=sum(framedifr');
framedifgsum=sum(framedifg');
framedifbsum=sum(framedifb');
framedifrgbsum=sum(framedifrgb');
if (framedifsum(1,i)>=50000)%%output the frames before and after the shot change
figure(1),
%subplot(121),imshow(shot(1,i-1).cdata),xlabel(strcat('第',int2str(i-1),'帧')),title('突变前图像帧');
%subplot(122),imshow(shot(1, i).cdata),xlabel(strcat('第',int2str( i),'帧')),title('突变后图像帧');
subplot('position',[0 0 0.5 1]),imshow(shot(1,i-1).cdata),xlabel(strcat('第',int2str(i-1),'帧')),title('突变前图像帧');
subplot('position',[0.5 0 0.5 1]),imshow(shot(1, i).cdata),xlabel(strcat('第',int2str( i),'帧')),title('突变后图像帧');
end
district1=district2;
districtr1=districtr2;
districtg1=districtg2;
districtb1=districtb2;
district2=zeros(16,divnum);
districtr2=zeros(16,divnum);
districtg2=zeros(16,divnum);
districtb2=zeros(16,divnum);
count2=zeros(16,256);
countr2=zeros(16,256);
countg2=zeros(16,256);
countb2=zeros(16,256);
end
%%%%%%%%%绘制图形%%%%%%%%%%%
figure(2),
%axis([0,100,0,60000])
axis auto;
plot(framedifsum);
hold;
%figure,
%plot(framedifrsum,'r');
%figure,
%plot(framedifgsum,'g');
%figure,
%plot(framedifbsum,'b');
%figure,
plot(framedifrgbsum,'-.k');
xlabel('帧数');% x label
ylabel('直方图帧差');%y label
title('X2分块法直方图帧差统计');%title setting
l=legend('灰度值','彩色量');
box on;
%set(gca,'Color','none');
%set(l,'color','none');
%text(90,65000,'突变边界');
%line([0,150],[60000,60000],'color','k','linestyle','-.');
toc;%统计时间
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -