title: Motion compensation in MATLAB
body: <p>I'm working on some Matlab code that would compare the prediction error between two video frames with and without motion compensation. I've already figured out how to get it to determine the prediction error without MC and the motion vector array, but I'm still working on getting it to determine what the initial frame would look like after MC. Would there be a function in MATLAB that would take in the original image <code>img1</code> and the motion vector array <code>motion</code> and output the motion compensated version of <code>img1</code> (<code>img1_mc</code>)? Thanks.</p>
  <pre><code>clear all; close all; clc;
  
  n = 4;
  block_size = 2^n+1;
  
  img1 = im2double(im2gray(imread('sull1.png')));
  img2 = im2double(im2gray(imread('sull2.png')));
  
  % Create a block matcher and alpha blender object.
  hbm = vision.BlockMatcher('ReferenceFrameSource', ...
          'Input port', 'BlockSize', [block_size, block_size]);
  hbm.OutputValue = 'Horizontal and vertical components in complex form';
  halphablend = vision.AlphaBlender;
  
  % Compute motion for the two images.
  motion = hbm(img1,img2);
  
  % Blend the two images.
  img12 = halphablend(img2,img1);
  
  % Prediction error without MC
  img_diff = img2 - img1 + 0.5*ones(size(img1)); 
  
  % Use a quiver plot to show the direction of motion on the images. 
  subplot(2, 2, 1);
  [X,Y] = meshgrid(1:block_size:size(img1,2), 1:block_size:size(img1,1));         
  imshow(img12)
  hold on
  quiver(X(:),Y(:),real(motion(:)),imag(motion(:)),0)
  hold off
  
  subplot(2, 2, 2);
  imshow(img_diff);
  
  subplot(2, 2, 3);
  imshow(img1_mc);
  </code></pre>