Skip to content

Commit e3bb756

Browse files
committed
motion estimation
1 parent a91e9e5 commit e3bb756

File tree

271 files changed

+172
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+172
-29
lines changed

Motion-Estimation/Block_Matching.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function [ x_predicted, y_predicted, min_MSE ] = Block_Matching( reference_frame, new_frame, p, x, y, block_size )
2+
%Block_Matching Algorithm. Block Matching between 2 subsequent frames to detect motion estimation
3+
% reference_frame The frame{t-1}
4+
% new_frame The frame{t}
5+
% p Search parameter
6+
% x,y Position withing the frame
7+
% block_size Size of the macroblock
8+
9+
reference_block = reference_frame(x:x+block_size,y:y+block_size,:);
10+
MSE = zeros(p,p);
11+
12+
for new_xpos=x-p:1:x+(p-1)
13+
for new_ypos=y-p:1:y+(p-1)
14+
if(new_xpos+block_size>size(reference_frame,1) || new_ypos+block_size>size(reference_frame,2) || new_xpos<=0 || new_ypos<=0)
15+
MSE(new_xpos-(x-p)+1,new_ypos-(y-p)+1)=1000;
16+
else
17+
new_block =new_frame(new_xpos:new_xpos+block_size,new_ypos:new_ypos+block_size,:);
18+
MSE(new_xpos-(x-p)+1,new_ypos-(y-p)+1)=double(mean2(((reference_block-new_block).^2)));
19+
end
20+
end
21+
end
22+
23+
min_MSE = min(min(MSE));
24+
25+
[min_x,min_y]=find(min_MSE);
26+
27+
if(isempty(min_x)&&isempty(min_y)) % no movement detected
28+
x_predicted = x;
29+
y_predicted = y;
30+
else
31+
x_predicted = min_x+(x-p);
32+
y_predicted = min_y+(y-p);
33+
end
34+
35+
end
36+

Motion-Estimation/script.asv

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
video=VideoReader('../Test-images/Xylophone.mp4');
2+
n=video.NumberofFrames;
3+
4+
% Divide the video in frames
5+
for x=1:n
6+
frame=read(video,x);
7+
imwrite(frame,sprintf('../Test-images/Frames/image%d.jpg',x));
8+
end
9+
10+
% Compute the motion vectors between 2 frames
11+
reference_frame = imread('../Test-images/Frames/image40.jpg');
12+
new_frame = imread('../Test-images/Frames/image41.jpg');
13+
14+
figure;imshow(reference_frame),title('Reference Frame');imshow(new_frame),title('New Frame');
15+
16+
tic
17+
% Motion Estimation
18+
p=3;block_size=16;
19+
predicted_pos = zeros(round(size(reference_frame,1)/block_size),round(size(reference_frame,2)/block_size),2);
20+
i=1;j=1;
21+
%Display the optical flow
22+
figure;imshow(reference_frame),title('Motion Vectors');
23+
hold on;
24+
for posx = 1:block_size:size(reference_frame,1)-block_size
25+
for posy = 1:block_size:size(reference_frame,2)-block_size
26+
[predicted_posx,predicted_posy,min_MSE]=Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
27+
if(min_MSE>=0.4)
28+
fprintf('ovement detected, draw motion vectors')
29+
quiver(posx, posy, predicted_posx,predicted_posy,'color', 'b', 'linewidth', 1);
30+
end
31+
end
32+
end
33+
toc
34+
35+
%Display the optical flow
36+
figure;imshow(reference_frame),title('Optical Flow');
37+
hold on;
38+
for i = 1:size(predicted_pos,1)
39+
for j = 1:size(predicted_pos,2)
40+
if(predicted_pos(i,j,1)~=i ||predicted_pos(i,j,2)~=j)
41+
quiver(i, j, predicted_pos(i,j,1),predicted_pos(i,j,2),'color', 'b', 'linewidth', 1);
42+
end
43+
end
44+
end
45+
46+
47+
% Predict the next frame between 2 frames taken randomly
48+
reference_frame = imread('../Test-images/Frames/image1.jpg');
49+
50+
tic
51+
% Predict image
52+
p=3;block_size=16;N=2;
53+
MSE_frames = zeros(1,N);
54+
for i = 1:1:N
55+
min_MSE=0;
56+
path = sprintf('../Test-images/Frames/image%d.jpg',i);
57+
new_frame = imread(path);
58+
for posx = 1:size(reference_frame,1)
59+
for posy = 1:size(reference_frame,2)
60+
if(posx+block_size<size(reference_frame,1) && posy+block_size<size(reference_frame,2))
61+
[~,~,MSE]= Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
62+
min_MSE= min_MSE + MSE;
63+
end
64+
end
65+
end
66+
fprintf('Mean Square Error: %d \n',min_MSE);
67+
MSE_frames(i) = min_MSE;
68+
end
69+
toc
70+
pred_frame = find(min(MSE_frames(i)));
71+
fprintf('The predicted frame is: %d \n',pred_frame);
72+
73+
PSNR=10*(log(255^2/MSE_frames(pred_frame))/log(10));
74+
fprintf('Peak-Signal-To-Noise Rario of the predicted frame: %d \n',PSNR);

Motion-Estimation/script.m

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
video=VideoReader('../Test-images/Xylophone.mp4');
2+
n=video.NumberofFrames;
3+
4+
% Divide the video in frames
5+
for x=1:n
6+
frame=read(video,x);
7+
imwrite(frame,sprintf('../Test-images/Frames/image%d.jpg',x));
8+
end
9+
10+
% Compute the motion vectors between 2 frames
11+
reference_frame = imread('../Test-images/Frames/image40.jpg');
12+
new_frame = imread('../Test-images/Frames/image41.jpg');
13+
14+
figure;imshow(reference_frame),title('Reference Frame');imshow(new_frame),title('New Frame');
15+
16+
tic
17+
% Motion Estimation
18+
p=3;block_size=16;
19+
predicted_pos = zeros(round(size(reference_frame,1)/block_size),round(size(reference_frame,2)/block_size),2);
20+
i=1;j=1;
21+
%Display the optical flow
22+
figure;imshow(reference_frame),title('Motion Vectors');
23+
hold on;
24+
for posx = 1:block_size:size(reference_frame,1)-block_size
25+
for posy = 1:block_size:size(reference_frame,2)-block_size
26+
[predicted_posx,predicted_posy,min_MSE]=Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
27+
if(min_MSE>=0.4)
28+
fprintf('Movement detected, draw motion vectors')
29+
quiver(posx, posy, predicted_posx,predicted_posy,'color', 'b', 'linewidth', 1);
30+
end
31+
end
32+
end
33+
toc
34+
35+
% Predict the next frame between 2 frames taken randomly
36+
reference_frame = imread('../Test-images/Frames/image1.jpg');
37+
38+
tic
39+
% Predict image
40+
p=3;block_size=16;N=2;
41+
MSE_frames = zeros(1,N);
42+
for i = 1:1:N
43+
min_MSE=0;
44+
path = sprintf('../Test-images/Frames/image%d.jpg',i);
45+
new_frame = imread(path);
46+
for posx = 1:size(reference_frame,1)
47+
for posy = 1:size(reference_frame,2)
48+
if(posx+block_size<size(reference_frame,1) && posy+block_size<size(reference_frame,2))
49+
[~,~,MSE]= Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
50+
min_MSE= min_MSE + MSE; % Compute the sum between all macroblocks
51+
end
52+
end
53+
end
54+
fprintf('Mean Square Error: %d \n',min_MSE);
55+
MSE_frames(i) = min_MSE;
56+
end
57+
toc
58+
pred_frame = find(min(MSE_frames(i)));
59+
fprintf('The predicted frame is: %d \n',pred_frame);
60+
61+
PSNR=10*(log(255^2/MSE_frames(pred_frame))/log(10));
62+
fprintf('Peak-Signal-To-Noise Rario of the predicted frame: %d \n',PSNR);

Stereoscopy/stereoscopy_script.asv

Lines changed: 0 additions & 29 deletions
This file was deleted.

Test-images/Frames/image1.jpg

10 KB

Test-images/Frames/image10.jpg

10.1 KB

Test-images/Frames/image100.jpg

9.97 KB

Test-images/Frames/image101.jpg

10.1 KB

Test-images/Frames/image102.jpg

10.1 KB

Test-images/Frames/image103.jpg

9.94 KB

Test-images/Frames/image104.jpg

9.94 KB

Test-images/Frames/image105.jpg

9.99 KB

Test-images/Frames/image106.jpg

10.1 KB

Test-images/Frames/image107.jpg

10.1 KB

Test-images/Frames/image108.jpg

10.2 KB

Test-images/Frames/image109.jpg

10 KB

Test-images/Frames/image11.jpg

10.1 KB

Test-images/Frames/image110.jpg

10 KB

Test-images/Frames/image111.jpg

10.1 KB

Test-images/Frames/image112.jpg

10.2 KB

Test-images/Frames/image113.jpg

10.2 KB

Test-images/Frames/image114.jpg

10.4 KB

Test-images/Frames/image115.jpg

10.4 KB

Test-images/Frames/image116.jpg

10.4 KB

Test-images/Frames/image117.jpg

10.5 KB

Test-images/Frames/image118.jpg

10.5 KB

Test-images/Frames/image119.jpg

10.5 KB

Test-images/Frames/image12.jpg

10.1 KB

Test-images/Frames/image120.jpg

10.5 KB

Test-images/Frames/image121.jpg

10.5 KB

Test-images/Frames/image122.jpg

10.5 KB

Test-images/Frames/image123.jpg

10.5 KB

Test-images/Frames/image124.jpg

10.5 KB

Test-images/Frames/image125.jpg

10.5 KB

Test-images/Frames/image126.jpg

10.5 KB

Test-images/Frames/image127.jpg

9.89 KB

Test-images/Frames/image128.jpg

9.89 KB

Test-images/Frames/image129.jpg

11.3 KB

Test-images/Frames/image13.jpg

9.98 KB

Test-images/Frames/image130.jpg

11.3 KB

Test-images/Frames/image131.jpg

11.5 KB

Test-images/Frames/image132.jpg

11.5 KB

Test-images/Frames/image133.jpg

11.1 KB

Test-images/Frames/image134.jpg

11.1 KB

Test-images/Frames/image135.jpg

11.1 KB

Test-images/Frames/image136.jpg

11.1 KB

Test-images/Frames/image137.jpg

11.2 KB

Test-images/Frames/image138.jpg

11.2 KB

Test-images/Frames/image139.jpg

11.2 KB

Test-images/Frames/image14.jpg

9.97 KB

Test-images/Frames/image140.jpg

11.2 KB

Test-images/Frames/image141.jpg

11.2 KB

Test-images/Frames/image142.jpg

11.2 KB

Test-images/Frames/image143.jpg

11.2 KB

Test-images/Frames/image144.jpg

11.2 KB

Test-images/Frames/image145.jpg

11 KB

Test-images/Frames/image146.jpg

11 KB

Test-images/Frames/image147.jpg

10.7 KB

Test-images/Frames/image148.jpg

10.7 KB

Test-images/Frames/image149.jpg

10.4 KB

Test-images/Frames/image15.jpg

9.99 KB

Test-images/Frames/image150.jpg

10.4 KB

Test-images/Frames/image151.jpg

10.5 KB

Test-images/Frames/image152.jpg

10.5 KB

Test-images/Frames/image153.jpg

9.94 KB

Test-images/Frames/image154.jpg

9.96 KB

Test-images/Frames/image155.jpg

9.7 KB

Test-images/Frames/image156.jpg

9.68 KB

Test-images/Frames/image157.jpg

10 KB

Test-images/Frames/image158.jpg

9.99 KB

Test-images/Frames/image159.jpg

9.99 KB

Test-images/Frames/image16.jpg

10.1 KB

Test-images/Frames/image160.jpg

10 KB

Test-images/Frames/image161.jpg

9.4 KB

Test-images/Frames/image162.jpg

9.06 KB

Test-images/Frames/image163.jpg

9.07 KB

Test-images/Frames/image164.jpg

9.32 KB

Test-images/Frames/image165.jpg

9.78 KB

Test-images/Frames/image166.jpg

9.83 KB

Test-images/Frames/image167.jpg

9.65 KB

Test-images/Frames/image168.jpg

9.64 KB

Test-images/Frames/image169.jpg

9.36 KB

Test-images/Frames/image17.jpg

10.1 KB

Test-images/Frames/image170.jpg

9.38 KB

Test-images/Frames/image171.jpg

8.95 KB

Test-images/Frames/image172.jpg

8.98 KB

Test-images/Frames/image173.jpg

9.31 KB

Test-images/Frames/image174.jpg

9.35 KB

Test-images/Frames/image175.jpg

9.52 KB

Test-images/Frames/image176.jpg

9.54 KB

Test-images/Frames/image177.jpg

9.86 KB

Test-images/Frames/image178.jpg

9.86 KB

Test-images/Frames/image179.jpg

9.56 KB

Test-images/Frames/image18.jpg

10.1 KB

Test-images/Frames/image180.jpg

9.58 KB

Test-images/Frames/image181.jpg

9.32 KB

Test-images/Frames/image182.jpg

9.35 KB

Test-images/Frames/image183.jpg

9.01 KB

Test-images/Frames/image184.jpg

9.02 KB

Test-images/Frames/image185.jpg

9.32 KB

Test-images/Frames/image186.jpg

9.35 KB

Test-images/Frames/image187.jpg

9.48 KB

Test-images/Frames/image188.jpg

9.47 KB

Test-images/Frames/image189.jpg

9.9 KB

Test-images/Frames/image19.jpg

10 KB

Test-images/Frames/image190.jpg

9.9 KB

Test-images/Frames/image191.jpg

9.57 KB

Test-images/Frames/image192.jpg

9.59 KB

Test-images/Frames/image193.jpg

9.35 KB

Test-images/Frames/image194.jpg

9.36 KB

Test-images/Frames/image195.jpg

8.95 KB

Test-images/Frames/image196.jpg

8.98 KB

Test-images/Frames/image197.jpg

8.98 KB

Test-images/Frames/image198.jpg

9.03 KB

Test-images/Frames/image199.jpg

9.47 KB

Test-images/Frames/image2.jpg

9.99 KB

Test-images/Frames/image20.jpg

10 KB

Test-images/Frames/image200.jpg

9.47 KB

Test-images/Frames/image201.jpg

9.96 KB

Test-images/Frames/image202.jpg

9.98 KB

Test-images/Frames/image203.jpg

10.1 KB

Test-images/Frames/image204.jpg

10.2 KB

Test-images/Frames/image205.jpg

9.98 KB

Test-images/Frames/image206.jpg

9.99 KB

Test-images/Frames/image207.jpg

9.94 KB

Test-images/Frames/image208.jpg

10 KB

Test-images/Frames/image209.jpg

10 KB

Test-images/Frames/image21.jpg

9.99 KB

Test-images/Frames/image210.jpg

10.1 KB

Test-images/Frames/image211.jpg

10 KB

Test-images/Frames/image212.jpg

10.1 KB

Test-images/Frames/image213.jpg

10.1 KB

Test-images/Frames/image214.jpg

10.1 KB

Test-images/Frames/image215.jpg

9.61 KB

Test-images/Frames/image216.jpg

9.62 KB

Test-images/Frames/image217.jpg

9.42 KB

Test-images/Frames/image218.jpg

9.43 KB

Test-images/Frames/image219.jpg

9.6 KB

Test-images/Frames/image22.jpg

10.1 KB

Test-images/Frames/image220.jpg

9.63 KB

Test-images/Frames/image221.jpg

9.78 KB

Test-images/Frames/image222.jpg

9.79 KB

Test-images/Frames/image223.jpg

9.8 KB

Test-images/Frames/image224.jpg

9.79 KB

Test-images/Frames/image225.jpg

9.62 KB

Test-images/Frames/image226.jpg

9.67 KB

Test-images/Frames/image227.jpg

9.69 KB

Test-images/Frames/image228.jpg

9.69 KB

Test-images/Frames/image229.jpg

9.72 KB

Test-images/Frames/image23.jpg

10.1 KB

Test-images/Frames/image230.jpg

9.72 KB

Test-images/Frames/image231.jpg

9.79 KB

Test-images/Frames/image232.jpg

9.8 KB

Test-images/Frames/image233.jpg

9.8 KB

Test-images/Frames/image234.jpg

9.86 KB

Test-images/Frames/image235.jpg

9.85 KB

Test-images/Frames/image236.jpg

9.85 KB

Test-images/Frames/image237.jpg

9.92 KB

Test-images/Frames/image238.jpg

9.92 KB

Test-images/Frames/image239.jpg

9.92 KB

Test-images/Frames/image24.jpg

10.1 KB

Test-images/Frames/image240.jpg

9.91 KB

Test-images/Frames/image241.jpg

9.93 KB

Test-images/Frames/image242.jpg

10 KB

Test-images/Frames/image243.jpg

10 KB

Test-images/Frames/image244.jpg

10 KB

Test-images/Frames/image245.jpg

10 KB

Test-images/Frames/image246.jpg

10 KB

Test-images/Frames/image247.jpg

9.96 KB

Test-images/Frames/image248.jpg

9.94 KB

Test-images/Frames/image249.jpg

9.93 KB

Test-images/Frames/image25.jpg

10.1 KB

Test-images/Frames/image250.jpg

9.92 KB

Test-images/Frames/image251.jpg

9.92 KB

Test-images/Frames/image252.jpg

9.95 KB

Test-images/Frames/image253.jpg

9.96 KB

Test-images/Frames/image254.jpg

9.96 KB

Test-images/Frames/image255.jpg

9.96 KB

Test-images/Frames/image256.jpg

9.96 KB

Test-images/Frames/image257.jpg

9.96 KB

Test-images/Frames/image258.jpg

9.96 KB

Test-images/Frames/image259.jpg

9.96 KB

Test-images/Frames/image26.jpg

10.1 KB

Test-images/Frames/image260.jpg

10 KB

Test-images/Frames/image261.jpg

10 KB

Test-images/Frames/image262.jpg

10 KB

Test-images/Frames/image263.jpg

10 KB

Test-images/Frames/image264.jpg

10 KB

Test-images/Frames/image265.jpg

10 KB

Test-images/Frames/image266.jpg

10 KB

Test-images/Frames/image27.jpg

10.1 KB

Test-images/Frames/image28.jpg

10.1 KB

Test-images/Frames/image29.jpg

10.1 KB

Test-images/Frames/image3.jpg

9.99 KB

Test-images/Frames/image30.jpg

10.1 KB

Test-images/Frames/image31.jpg

10.1 KB

Test-images/Frames/image32.jpg

10.1 KB

Test-images/Frames/image33.jpg

10.4 KB

Test-images/Frames/image34.jpg

10.4 KB

Test-images/Frames/image35.jpg

11.3 KB

Test-images/Frames/image36.jpg

11.3 KB

Test-images/Frames/image37.jpg

11.1 KB

Test-images/Frames/image38.jpg

11.1 KB

Test-images/Frames/image39.jpg

10.7 KB

Test-images/Frames/image4.jpg

9.99 KB

Test-images/Frames/image40.jpg

10.7 KB

Test-images/Frames/image41.jpg

10.3 KB

Test-images/Frames/image42.jpg

10.3 KB

Test-images/Frames/image43.jpg

10.3 KB

Test-images/Frames/image44.jpg

10.3 KB

Test-images/Frames/image45.jpg

10.2 KB

Test-images/Frames/image46.jpg

10.2 KB

Test-images/Frames/image47.jpg

9.92 KB

Test-images/Frames/image48.jpg

9.91 KB

Test-images/Frames/image49.jpg

10.3 KB

Test-images/Frames/image5.jpg

9.99 KB

Test-images/Frames/image50.jpg

10.3 KB

Test-images/Frames/image51.jpg

10.3 KB

Test-images/Frames/image52.jpg

10.3 KB

Test-images/Frames/image53.jpg

9.7 KB

Test-images/Frames/image54.jpg

9.32 KB

Test-images/Frames/image55.jpg

9.32 KB

Test-images/Frames/image56.jpg

9.59 KB

Test-images/Frames/image57.jpg

10.1 KB

Test-images/Frames/image58.jpg

10.1 KB

Test-images/Frames/image59.jpg

9.87 KB

Test-images/Frames/image6.jpg

9.99 KB

Test-images/Frames/image60.jpg

9.87 KB

Test-images/Frames/image61.jpg

9.58 KB

Test-images/Frames/image62.jpg

9.58 KB

Test-images/Frames/image63.jpg

9.25 KB

Test-images/Frames/image64.jpg

9.28 KB

Test-images/Frames/image65.jpg

9.51 KB

Test-images/Frames/image66.jpg

9.54 KB

Test-images/Frames/image67.jpg

9.71 KB

Test-images/Frames/image68.jpg

9.76 KB

Test-images/Frames/image69.jpg

9.97 KB

Test-images/Frames/image7.jpg

9.98 KB

Test-images/Frames/image70.jpg

10 KB

Test-images/Frames/image71.jpg

9.68 KB

Test-images/Frames/image72.jpg

9.75 KB

Test-images/Frames/image73.jpg

9.48 KB

Test-images/Frames/image74.jpg

9.51 KB

Test-images/Frames/image75.jpg

9.11 KB

Test-images/Frames/image76.jpg

9.12 KB

Test-images/Frames/image77.jpg

9.5 KB

Test-images/Frames/image78.jpg

9.51 KB

Test-images/Frames/image79.jpg

9.58 KB

Test-images/Frames/image8.jpg

9.99 KB

Test-images/Frames/image80.jpg

9.58 KB

Test-images/Frames/image81.jpg

9.98 KB

Test-images/Frames/image82.jpg

9.97 KB

Test-images/Frames/image83.jpg

9.67 KB

Test-images/Frames/image84.jpg

9.66 KB

Test-images/Frames/image85.jpg

9.44 KB

Test-images/Frames/image86.jpg

9.42 KB

Test-images/Frames/image87.jpg

9.05 KB

Test-images/Frames/image88.jpg

9.05 KB

Test-images/Frames/image89.jpg

9.15 KB

Test-images/Frames/image9.jpg

10 KB

Test-images/Frames/image90.jpg

9.14 KB

Test-images/Frames/image91.jpg

9.53 KB

Test-images/Frames/image92.jpg

9.54 KB

Test-images/Frames/image93.jpg

9.75 KB

Test-images/Frames/image94.jpg

9.82 KB

Test-images/Frames/image95.jpg

10.1 KB

Test-images/Frames/image96.jpg

10.2 KB

Test-images/Frames/image97.jpg

9.94 KB

Test-images/Frames/image98.jpg

9.96 KB

Test-images/Frames/image99.jpg

9.91 KB

Test-images/Xylophone.mp4

316 KB
Binary file not shown.

0 commit comments

Comments
 (0)