⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motion_estimation_2d.m

📁 Motion_Estimation 對兩張圖片做motion 估測的程式 可show出貼補的結果與residual
💻 M
字号:
%!!!The function also use the function "EBMA" for motion estimation of every macroblock
% An example of main function calling this function is "Prob6_12.m" and "Prob6_13.m", which can be entered on the command window.


function [Target_Img,Anchor_Img,Predict_Img,ox,oy,px,py,PSNR]=Motion_Estimation_2D(TargetName,AnchorName,Img_Height,Img_Width,BlockSize,rangs,range,K)

%Motion_Estimation_2D compute Motion Vectors in Anchor Frame from Target Frame in either integer or half-pel accuracy
%
%	TargetName,AnchorName: 
%		File Names of Target Frame and Anchor Frame
%	Img_Height,Img_Width:  
%		Image Height and Width of a Frame
%  BlockSize:             
%		The size of Macro Block in Frame is BlockSize(1) by BlockSize(2)
%	rangs,range:      
%		The Search Field in Frame A is from (rangs(1),rangs(2)) to (range(1),range(2))
%	K:
%		The search accuracy: 1 integer pel 2 half pel
%  Target_Img,Anchor_Img,Predict_Img:
%		Image Matrix for Target Frame, Anchor Frame, Predicted Frame
%	ox,oy,px,py:
%		The location of Motion vector is (ox,oy), (px,py) for the direction 
%	PSNR
%		The peak signal and noise ratio between original image and predicted image
%	Author: Xiaofeng Xu, Polytechnic University  4/21/2002


%Read images from files
%TargetName='D:\yuvimage\break\test2.yuv';
fid = fopen(TargetName,'r');
Target_Img= fread(fid,[Img_Height,Img_Width]);
fclose(fid);

fid = fopen(AnchorName,'r');
Anchor_Img= fread(fid,[Img_Height,Img_Width]);
fclose(fid);


%-----------------------------------------------------------------------


down4_Target_Img=Target_Img(1:4:Img_Height,1:4:Img_Width);
%figure(3);
%imshow(uint8(Target_Img).');

down4_Anchor_Img=Anchor_Img(1:4:Img_Height,1:4:Img_Width);
figure(6);
imshow(uint8(down4_Anchor_Img).');

down4_Img_Height=Img_Height/4;
down4_Img_Width=Img_Width/4;

t=0;

down4_Predict_Img=down4_Target_Img;
t0 = clock;
rangs=[-8,-8];
range=[8,8];

m=1;
%Search for all the blocks in Anchor Images. 
for i=1:BlockSize(1):down4_Img_Height-BlockSize(1)+1
   % BlockSize:             
   % The size of Macro Block is BlockSize(1) by BlockSize(2)
   % Caculate the search range in Target Images.
   RangeStart(1)=i*K-t+rangs(1)*K;
   RangeEnd(1)=i*K-t+BlockSize(1)*K-1+range(1)*K;
   if RangeStart(1)<1
      RangeStart(1)=1;
   end   
   if RangeEnd(1)>down4_Img_Height*K
      RangeEnd(1)=down4_Img_Height*K;
   end
   % RangeStart,RangeEnd:      search range
   % The Search Field in Frame A is from (RangeStart(1),RangeStart(2))
   % to (RangeEnd(1),RangeEnd(2))
   for j=1:BlockSize(2):down4_Img_Width-BlockSize(2)+1
      RangeStart(2)=j*K-t+rangs(2)*K;
      RangeEnd(2)=j*K-t+BlockSize(2)*K-1+range(2)*K;
	   if RangeStart(2)<1
   	   RangeStart(2)=1;
	   end   
   	if RangeEnd(2)>down4_Img_Width*K
      	RangeEnd(2)=down4_Img_Width*K;
	   end
      %Get the best estimation from Target Image.
      [px(m), py(m), down4_Predict_Img(i:i+BlockSize(1)-1,j:j+BlockSize(2)-1)]=EBMA(down4_Target_Img , down4_Anchor_Img , BlockSize , [i,j] , RangeStart , RangeEnd ,K);
      ox(m)=j+BlockSize(2)/2;
      oy(m)=i+BlockSize(1)/2;
      m=m+1;
      j;
      i;
  end
end

 
down4_Error_Img=down4_Anchor_Img-down4_Predict_Img;

%Caculate PSNR
PSNR=10*log10(255*255/mean(mean((down4_Error_Img.^2))));
PSNR
etime(clock,t0)

%Display the results
figure(1);
imshow(uint8(down4_Anchor_Img).');
%-------------------------
hold on
quiver(oy,ox,py,px);%礶mv

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -