Motion compensation in MATLAB
13 views (last 30 days)
Show older comments
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. Can someone please explain what's wrong with my code? Thanks.
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);
img_diff = img2 - img1 + 0.5*ones(size(img1));
% img_diff = imabsdiff(img2, img1);
% Compute MC'd image
img1_mc = zeros(size(img1));
for i = 1:size(motion, 1)
for j = 1:size(motion, 2)
if block_size*i > size(img1, 1)
dim1 = block_size*(i-1)+1 : size(img1, 1);
else
dim1 = block_size*(i-1)+1 : block_size*i;
end
if block_size*j > size(img2, 2)
dim2 = block_size*(j-1)+1 : size(img2, 2);
else
dim2 = block_size*(j-1)+1 : block_size*j;
end
img1_mc(dim1,dim2) = motion(i,j)*img1(dim1,dim2);
end
end
% 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
img_diff2 = img1_mc - img1 + 0.5*ones(size(img1));
subplot(2, 2, 2);
imshow(img_diff);
subplot(2, 2, 3);
imshow(img_diff2);
0 Comments
Answers (1)
ALI NADEEM
on 23 Nov 2022
can you tell me Im also working on DPCM video frame I have to stroe prediction error or difference image part value that are distibuting betwwen -255 and +255 that is later on as a codebbok transfer to decoder part for reconstruction how could I store them my frames are uint8 kindly tell me
0 Comments
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!