Skip to content

Commit c254b4b

Browse files
committed
last changes motion estimation and stereoscopy
1 parent 6a9213b commit c254b4b

File tree

7 files changed

+23
-21
lines changed

7 files changed

+23
-21
lines changed

Motion-Estimation/Block_Matching.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
continue;
2929
else
3030
new_block =new_frame(new_xpos:new_xpos+(block_size-1),new_ypos:new_ypos+(block_size-1));
31-
MSE(m+p+1,n+p+1)=double(mean2(((reference_block-new_block).^2)));
31+
MSE(m+p+1,n+p+1)=double(mean2(((double(reference_block)-double(new_block)).^2)));
3232
end
3333
end
3434
end

Motion-Estimation/Motion_Compensation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
dx = motion_vect(2,vect_count);
2222
y_block = i + dy;
2323
x_block = j + dx;
24-
if(y_block>=1&&x_block>=1&&dy~=0&&dx~=0)
24+
if(y_block>=1&&x_block>=1&&dy~=0&&dx~=0&& (y_block+block_size-1<size(reference_frame,1) && x_block+block_size-1<size(reference_frame,2)))
2525
image_compensation(i:i+block_size-1,j:j+block_size-1) = reference_frame(y_block:y_block+block_size-1, x_block:x_block+block_size-1);
2626
end
2727
vect_count = vect_count + 1;

Motion-Estimation/Motion_Estimation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
%
1313

1414
[rows, cols] = size(reference_frame);
15-
motion_vectors = zeros(3,rows*cols/block_size^2);
15+
motion_vectors = zeros(3,round(rows*cols/block_size^2));
1616

1717
vect_count = 1;
1818
for posx = 1:block_size:rows-block_size

Motion-Estimation/draw_Optical_Flow.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function draw_Optical_Flow( reference_frame, motion_vect, block_size )
33

44
[row, col] = size(reference_frame);
55
mbCount = 1;
6-
subplot(2,2,4),subimage(reference_frame),title('Motion Vectors');
6+
subplot(2,2,4),subimage(uint8(reference_frame)),title('Motion Vectors');
77
hold on;
88
for i = 1:block_size:row-block_size
99
for j = 1:block_size:col-block_size

Motion-Estimation/image_PSNR.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function PSNR = image_PSNR(reference_image, estimated_image)
1+
function PSNR = image_PSNR(new_image, estimated_image)
22

3-
[row, col] = size(reference_image);
3+
[row, col] = size(new_image);
44
sum_err = 0;
55
for i = 1:row
66
for j = 1:col
7-
sum_err = sum_err + (reference_image(i,j) - estimated_image(i,j))^2;
7+
sum_err = sum_err + double(mean2(abs(double(new_image(i,j)) - double(estimated_image(i,j))).^2));
88
end
99
end
10-
mse = sum_err/(row*col);
11-
PSNR = 10*log10(double(255^2/mse));
10+
11+
PSNR = 10*log10(double(255^2/sum_err));

Motion-Estimation/script.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@
99
end
1010

1111
% Compute the motion vectors between 2 frames
12-
reference_frame = imread('../Test-images/Frames/image40.jpg');
12+
fileName = '../Test-images/Frames/image40.jpg';
13+
reference_frame = imread(fileName);
1314
new_frame = imread('../Test-images/Frames/image41.jpg');
1415

1516
% Transform in gray scale for simplification
16-
reference_frame = rgb2gray(reference_frame);
17-
new_frame = rgb2gray(new_frame);
17+
reference_frame = double(rgb2gray(reference_frame));
18+
new_frame = double(rgb2gray(new_frame));
1819

1920
% Plot the reference and new frames
20-
subplot(2,2,1),subimage(reference_frame),title('Reference Frame'); hold on;
21-
subplot(2,2,2),subimage(new_frame),title('New Frame');
21+
subplot(2,2,1),subimage(uint8(reference_frame)),title('Reference Frame'); hold on;
22+
subplot(2,2,2),subimage(uint8(new_frame)),title('New Frame');
2223

2324
% p is the search parameter, block_size is the size of the macroblock
24-
p=7;block_size=16;
25+
p=6;block_size=2;
2526

2627
% Motion estimation using an exhaustive method
2728
[motion_vect] = Motion_Estimation(reference_frame,new_frame,block_size,p);
2829

2930
% Motion competation using the motion vectors
3031
estimated_frame= Motion_Compensation(reference_frame, motion_vect, block_size);
31-
subplot(2,2,3),subimage(estimated_frame),title('Estimated Frame');
32+
subplot(2,2,3),subimage(uint8(estimated_frame)),title('Estimated Frame');
3233
draw_Optical_Flow( reference_frame, motion_vect, block_size );
3334
hold off;
3435

36+
3537
% Peak Signal-to-Noise Ratio (PSNR) to measure the distortion of the frame which is estimated and the predicted one
36-
PSNR = image_PSNR(new_frame, estimated_frame);
37-
fprintf('Peak-Signal-To-Noise Ratio: %d \n',PSNR);
38+
[PSNR1,mse,~] = measerr(new_frame, estimated_frame)
39+
3840

3941

Stereoscopy/stereoscopy_script.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@
141141
rightImage_dq(rows_r/2+1:rows_r, cols_r/2+1:cols_r,i) = dequantize_matrix(q_sub23_r(:,:,i),4,8);
142142
rightImage_synth(:,:,i) = haar_reverse_multilevel(rightImage_dq(:,:,i),2);
143143
end
144-
figure; image(uint8(rightImage_synth)), title('Right Image Synth');
145-
figure; image(uint8(leftImage_synth)), title('Left Image Synth');
144+
subplot(1,3,1),subimage(uint8(rightImage_synth)), title('Right Image Synth');
145+
subplot(2,3,2),subimage(uint8(leftImage_synth)), title('Left Image Synth');
146146

147147
% Encode each eye's image using filters of different (usually chromatically opposite) colors, red and blue
148148
rightImage_synth = imtranslate(rightImage_synth,[10, 0]);
149149

150-
figure,image(rightImage_synth), title('Anaglyph Synth');
150+
subplot(3,3,3),subimage(rightImage_synth), title('Anaglyph Synth');
151151
r = zeros(size(rightImage_synth));
152152
gb = zeros(size(leftImage_synth));
153153
r(:,:,1) = double(rightImage_synth(:,:,1));

0 commit comments

Comments
 (0)