I have a basic code for Full search motion estimation but it do not run, can any body help?

10 views (last 30 days)
this is the code:
inFile1 = 'tt040.ras';% table tennis sequence
inFile2 = 'tt041.ras';
A = imread(inFile1); % frame #1
B = imread(inFile2); % frame #2
N = 8;% block size is N x N pixels
W = 16; % search window size is W x W pixels
% Make image size divisible by 8
[X,Y,Z] = size(A);
Height = double (floor(X/8)*8);
Width = double (floor(Y/8)*8);
Depth = Z;
clear X Y Z
t1 = cputime; % start CPU time
t2 = cputime; % end CPU time (t2-t1) is the time for motion estimation
figure,quiver(x,y,'k'),title('Motion Vector')
sprintf('MSE with MC = %3.3f',std2(pf)*std2(pf))
sprintf('MSE without MC = %3.3f',std2(single(A)-single(B))*std2(single(A)-single(B)))
figure,imshow(pf,[]),title('MC prediction')
figure,imshow(single(A)-single(B),[]),title('prediction without MC')
[hNoMC,BinNoMC] = hist(single(A)-single(B),256);
[hMC,BinMC] = hist(pf,256);
figure,plot(BinMC,hMC,'k'),title('histogram of p frame with MC')
figure,plot(BinNoMC,hNoMC,'k'),title('Histogram of p frame with no MC')
function [x,y,pf]= MCpredictFull{A,B,N,W}
%[x,y,pf] = MCpredictFull(A,B,N,W);
% Computes motion vectors of N x N moving blocks
% in an intensity image using full search and
% does a motion compensated prediction to a single pel accuracy
% Input:
% A = reference frame
% B = current frame
% N = block size (nominal value is 8, assumed square)
% W = search window (2N x 2N)
% Output:
% x = horizontal component of motion vector
% y = Vertical component of motion vector
% pf = motion compensated prediction image, same size as input image
[Height,Width] = size(A);
% pad input images on left, right, top, and bottom
% padding by replicating works better than padding w/ zeros, which is
% better than symmetric which is better than circular
A1 = double(padarray(A,[W/2 W/2],'replicate'));
B1 = double(padarray(B,[W/2 W/2],'replicate'));
x = int16(zeros(Height/N,Width/N));% x-component of motion vector
y = int16(zeros(Height/N,Width/N));% y-component of motion vector
% Find motion vector by exhaustive search to a single pel accuracy
figure,imshow(B), title('Superimposed motion vectors')
hold on % display image & superimpose motion vectors
for r = N:N:Height
rblk = floor(r/N);
for c = N:N:Width
cblk = floor(c/N);
D = 1.0e+10;% initial city block distance
for u = -N:N
for v = -N:N
d = B1(r+1:r+N,c+1:c+N)-A1(r+u+1:r+u+N,c+v+1:c+v+N);
d = sum(abs(d(:)));% city block distance between pixels
if d < D
D=d;
x(rblk,cblk) = v; y(rblk,cblk) = u;
end
end
end
quiver(c+y(rblk,cblk),r+x(rblk,cblk),x(rblk,cblk),y(rblk,cblk),'k','LineWidth',1)
end
end
hold off
% Reconstruct current frame using prediction error & reference frame
N2 = 2*N;
pf = zeros(Height,Width); % prediction frame
Br = zeros(Height,Width); % reconstructed frame
for r = 1:N:Height
rblk = floor(r/N) + 1;
for c = 1:N:Width
cblk = floor(c/N) + 1;
x1 = x(rblk,cblk); y1 = y(rblk,cblk);
pf(r:r+N-1,c:c+N-1) = B1(r+N:r+N2-1,c+N:c+N2-1)-A1(r+N+y1:r+y1+N2-1,c+N+x1:c+x1+N2-1);
Br(r:r+N-1,c:c+N-1) = A1(r+N+y1:r+y1+N2-1,c+N+x1:c+x1+N2-1)+ pf(r:r+N-1,c:c+N-1);
end
end
figure,imshow(uint8(round(Br))),title('Reconstructed image')
  3 Comments
Jan
Jan on 2 Dec 2011
Please use a proper code-formatting as explained at the link Adnrei has posted, or at the "Markup help" link. I've done the formatting for you this time.

Sign in to comment.

Accepted Answer

Jan
Jan on 2 Dec 2011
I guess, that the shown code is a script - an M-file, which does not start with "function".
Then you try to define a function inside this script by "Function [x,y,pf] = MCpredict_Full(A,B,N,W)". At first you cannot define functions inside scripts (see: Answers:functions in scripts). At second the uppercase "F" is not accepted.
Anyhow, both bugs do not appear, because a third error hides them:
switch 'PredictionType'
You do not want the string 'PredictionType', but the variable having this name - remove the quote characters.
To catch such bugs, it is strongly recommended to include an otherwise in all switch constructions ever always in all cases even if it is impossible for sure.
Some other ideas:
if mod(X,8)~= 0
Height = floor(X/8)*8;
else
Height = X;
end
Easier - the smaller the code, the less chances to include a bug:
Height = floor(X/8)*8;
In:
pf = douBle(zeros(Height,Width))
The "douBle" needs a lowercase "b", same for "aBs()". It seems like you have replaced all "b" by "B". But "double" is not needed at all, because zeros creates doubles as default. Later on "pf = single(zeros(Height,Width))" can be improved: "pf = zeros(Height,Width, 'single')". The same for "zeros(Height/N, Width/N, 'int16')".
In:
Indx = isnan(z1);
z1(Indx == 1) = CurrentBlk(Indx==1)
the "== 1" wastes a lot of time: For a stable comparison the values of the variable Indx have to be converted to the type DOUBLE. But because Indx is a LOGICAL array already, the "== 1" can be omitted completely:
z1(Indx) = CurrentBlk(Indx)
The number and quality of the bugs are a hint, that there are more bugs in the code.
  3 Comments

Sign in to comment.

More Answers (1)

Mridula Bhan
Mridula Bhan on 2 Sep 2015
How to define the function "Function [x,y,pf] = MCpredict_Full(A,B,N,W)" here?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!