Skip to content

Commit d3f5522

Browse files
committed
Commit after merge
2 parents ff5edf5 + 6a9213b commit d3f5522

File tree

279 files changed

+4157
-0
lines changed

Some content is hidden

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

279 files changed

+4157
-0
lines changed

Motion-Estimation/Block_Matching.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
%
4+
% Input
5+
% reference_frame : The frame{t-1}
6+
% new_frame : The frame{t}
7+
% p : Search parameter
8+
% x,y : Position withing the frame
9+
% block_size : Size of the macroblock
10+
%
11+
% Ouput
12+
% motion_vect : Motion vector for each macroblock
13+
% min_MSE : The minimum MSE (Mean Square Error)
14+
%
15+
16+
reference_block = reference_frame(x:x+(block_size-1),y:y+(block_size-1));
17+
MSE = ones(2*p + 1, 2*p +1) * 65537;
18+
19+
% Search the correspondence of the macroblock of a reference frame
20+
% using an exhaustive method. This method searches for the minimum
21+
% cost function at each possible location with respect to the search parameter.
22+
23+
for m=-p:p
24+
for n=-p:p
25+
new_xpos = x + m;
26+
new_ypos = y + n;
27+
if(new_xpos+block_size>size(reference_frame,1) || new_ypos+block_size>size(reference_frame,2) || new_xpos<1 || new_ypos<1)
28+
continue;
29+
else
30+
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)));
32+
end
33+
end
34+
end
35+
36+
% Create the motion vectors with the minimum cost function.
37+
min = 70000;
38+
for i = 1:size(MSE,1)
39+
for j = 1:size(MSE,2)
40+
if (MSE(i,j) < min)
41+
min = MSE(i,j);dx = j; dy = i;
42+
end
43+
end
44+
end
45+
46+
min_MSE = min;
47+
x_predicted = dx-p-1;
48+
y_predicted = dy-p-1;
49+
50+
end
51+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function estimated_image = Motion_Compensation( reference_frame, motion_vect, block_size)
2+
%Motion_Compensation algorithm. Predicts the next image using the motion
3+
%vectors
4+
%
5+
% Input
6+
% reference_frame : The frame{t-1}
7+
% motion_vect : The motion vectors for each macroblock
8+
% block_size : Size of the macroblock
9+
%
10+
% Output
11+
% estimated_image : Result of using the motion vectors to predict a frame in a video stream
12+
%
13+
14+
[row, col] = size(reference_frame);
15+
vect_count = 1;
16+
image_compensation = reference_frame;
17+
18+
for i = 1:block_size:row-block_size
19+
for j = 1:block_size:col-block_size
20+
dy = motion_vect(1,vect_count);
21+
dx = motion_vect(2,vect_count);
22+
y_block = i + dy;
23+
x_block = j + dx;
24+
if(y_block>=1&&x_block>=1&&dy~=0&&dx~=0)
25+
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);
26+
end
27+
vect_count = vect_count + 1;
28+
end
29+
end
30+
31+
estimated_image = image_compensation;
32+
33+
end
34+

Motion-Estimation/Motion_Estimation.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function motion_vectors = Motion_Estimation(reference_frame, new_frame, block_size, p)
2+
%Motion_Estimation algorithm Computes motion vectors using exhaustive search method
3+
%
4+
% Input
5+
% reference_frame : The frame{t-1}
6+
% new_frame : The frame{t}
7+
% p : Search parameter
8+
% block_size : Size of the macroblock
9+
%
10+
% Ouput
11+
% motion_vectors : The motion vectors for each macroblock
12+
%
13+
14+
[rows, cols] = size(reference_frame);
15+
motion_vectors = zeros(3,rows*cols/block_size^2);
16+
17+
vect_count = 1;
18+
for posx = 1:block_size:rows-block_size
19+
for posy = 1:block_size:cols-block_size
20+
[predicted_posx,predicted_posy,min_MSE]=Block_Matching( reference_frame, new_frame, p, posx, posy, block_size );
21+
motion_vectors(1:2,vect_count) = [predicted_posx,predicted_posy];
22+
motion_vectors(3,vect_count) = min_MSE;
23+
vect_count = vect_count+1;
24+
end
25+
end
26+
27+
end
28+

Motion-Estimation/Tests/40-41.svg

Lines changed: 3305 additions & 0 deletions
Loading

Motion-Estimation/draw_Optical_Flow.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function draw_Optical_Flow( reference_frame, motion_vect, block_size )
2+
%UNTITLED Draw the motion vectors in the reference frame
3+
4+
[row, col] = size(reference_frame);
5+
mbCount = 1;
6+
subplot(2,2,4),subimage(reference_frame),title('Motion Vectors');
7+
hold on;
8+
for i = 1:block_size:row-block_size
9+
for j = 1:block_size:col-block_size
10+
dy = motion_vect(1,mbCount);
11+
dx = motion_vect(2,mbCount);
12+
y_block = i + dy;
13+
x_block = j + dx;
14+
if((y_block>=1&&x_block>=1&&dy~=0&&dx~=0) && (motion_vect(3,mbCount)>0.4) && (x_block<=size(reference_frame,1)) && (y_block<=size(reference_frame,2)))
15+
quiver(j,i,dx, dy,'color', 'b', 'linewidth', 1);
16+
end
17+
mbCount = mbCount + 1;
18+
end
19+
end
20+
hold off;
21+
22+
end
23+

Motion-Estimation/image_PSNR.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function PSNR = image_PSNR(reference_image, estimated_image)
2+
3+
[row, col] = size(reference_image);
4+
sum_err = 0;
5+
for i = 1:row
6+
for j = 1:col
7+
sum_err = sum_err + (reference_image(i,j) - estimated_image(i,j))^2;
8+
end
9+
end
10+
mse = sum_err/(row*col);
11+
PSNR = 10*log10(double(255^2/mse));

Motion-Estimation/script.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
% Read the video file
2+
video=VideoReader('../Test-images/Xylophone.mp4');
3+
n=video.NumberofFrames;
4+
5+
% Divide the video in frames
6+
for x=1:n
7+
frame=read(video,x);
8+
imwrite(frame,sprintf('../Test-images/Frames/image%d.jpg',x));
9+
end
10+
11+
% Compute the motion vectors between 2 frames
12+
reference_frame = imread('../Test-images/Frames/image40.jpg');
13+
new_frame = imread('../Test-images/Frames/image41.jpg');
14+
15+
% Transform in gray scale for simplification
16+
reference_frame = rgb2gray(reference_frame);
17+
new_frame = rgb2gray(new_frame);
18+
19+
% 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');
22+
23+
% p is the search parameter, block_size is the size of the macroblock
24+
p=7;block_size=16;
25+
26+
% Motion estimation using an exhaustive method
27+
[motion_vect] = Motion_Estimation(reference_frame,new_frame,block_size,p);
28+
29+
% Motion competation using the motion vectors
30+
estimated_frame= Motion_Compensation(reference_frame, motion_vect, block_size);
31+
subplot(2,2,3),subimage(estimated_frame),title('Estimated Frame');
32+
draw_Optical_Flow( reference_frame, motion_vect, block_size );
33+
hold off;
34+
35+
% 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+
39+

Stereoscopy/paddingZeros.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function paddedMatrix = paddingZeros(matrix, divisor)
2+
%paddedMatrix = zeros(size(matrix,1), size(matrix,2), size(matrix,3));
3+
rows = size(matrix,1);
4+
cols = size(matrix,2);
5+
paddedMatrix = wextend('ar','sym',matrix, round(rows/divisor)*divisor - rows, 'd');
6+
paddedMatrix = wextend('ac','sym',paddedMatrix, round(cols/divisor)*divisor - cols, 'l');
7+
end
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
[img1, colormap] = imread('../Test-images/stereoscopy-images/image3.jpg');
2+
figure; image(img1), title('Original Image'); % Original image
3+
4+
% Split them into 2 separate images
5+
leftImage = img1(:,1:(end/2),:);
6+
7+
rightImage = img1(:,end/2+1:end,:);
8+
figure; image(leftImage), title('Left Image');
9+
figure; image(rightImage), title('Right Image');
10+
11+
leftImage = paddingZeros(leftImage,4); %We need a dimension divisible by 4
12+
rightImage = paddingZeros(rightImage,4);
13+
14+
15+
% Encode each eye's image using filters of different (usually chromatically opposite) colors, red and blue
16+
r = zeros(size(leftImage));
17+
gb = zeros(size(rightImage));
18+
r(:,:,1) = double(leftImage(:,:,1));
19+
gb(:,:,2:3) = double(rightImage(:,:,2:3));
20+
anaglyph = uint8(r+gb);
21+
figure,image(uint8(r)), title('Red')
22+
figure,image(uint8(gb)), title('Cyan')
23+
figure,image(anaglyph), title('My Anaglyph')
24+
25+
% Create stereo anaglyph with Matlab function to check the result
26+
J = stereoAnaglyph(leftImage,rightImage);
27+
figure, image(uint8(J)), title('Anaglyph using Matlab function')
28+
29+
% Use TD3 to code them using 2 decomposition layers
30+
leftImage_haar = zeros(size(r,1),size(r,2),size(r,3));
31+
rightImage_haar =zeros(size(gb,1),size(gb,2),size(gb,3));
32+
rows_l=size(leftImage,1);
33+
rows_r=size(rightImage,1);
34+
cols_l=size(leftImage,2);
35+
cols_r=size(rightImage,2);
36+
sub11_l = zeros(rows_l/4,cols_l/4,3); sub11_r = zeros(rows_r/4,cols_r/4,3);
37+
sub12_l = zeros(rows_l/4,cols_l/4,3); sub12_r = zeros(rows_r/4,cols_r/4,3);
38+
sub13_l = zeros(rows_l/4,cols_l/4,3); sub13_r = zeros(rows_r/4,cols_r/4,3);
39+
sub14_l = zeros(rows_l/4,cols_l/4,3); sub14_r = zeros(rows_r/4,cols_r/4,3);
40+
sub21_l = zeros(rows_l/2,cols_l/2,3); sub21_r = zeros(rows_r/2,cols_r/2,3);
41+
sub22_l = zeros(rows_l/2,cols_l/2,3); sub22_r = zeros(rows_r/2,cols_r/2,3);
42+
sub23_l = zeros(rows_l/2,cols_l/2,3); sub23_r = zeros(rows_r/2,cols_r/2,3);
43+
44+
for i=1:3
45+
% temp = leftImage(:,:,i);
46+
temp = r(:,:,i);
47+
48+
leftImage_haar(:,:,i) = haar_transform_multilevel(temp,2);
49+
50+
sub11_l(:,:,i) = leftImage_haar(1:rows_l/4, 1:cols_l/4,i);
51+
sub12_l(:,:,i) = leftImage_haar(1:rows_l/4, cols_l/4+1:cols_l/2,i);
52+
sub13_l(:,:,i) = leftImage_haar(rows_l/4+1:rows_l/2, 1:cols_l/4,i);
53+
sub14_l(:,:,i) = leftImage_haar(rows_l/4+1:rows_l/2, cols_l/4+1:cols_l/2,i);
54+
sub21_l(:,:,i) = leftImage_haar(1:rows_l/2, cols_l/2+1:cols_l,i);
55+
sub22_l(:,:,i) = leftImage_haar(rows_l/2+1:rows_l, 1:cols_l/2,i);
56+
sub23_l(:,:,i) = leftImage_haar(rows_l/2+1:rows_l, cols_l/2+1:cols_l,i);
57+
58+
temp2 = gb(:,:,i);
59+
rightImage_haar(:,:,i) = haar_transform_multilevel(temp2,2);
60+
sub11_r(:,:,i) = rightImage_haar(1:rows_l/4, 1:cols_l/4,i);
61+
sub12_r(:,:,i) = rightImage_haar(1:rows_l/4, cols_l/4+1:cols_l/2,i);
62+
sub13_r(:,:,i) = rightImage_haar(rows_l/4+1:rows_l/2, 1:cols_l/4,i);
63+
sub14_r(:,:,i) = rightImage_haar(rows_l/4+1:rows_l/2, cols_l/4+1:cols_l/2,i);
64+
sub21_r(:,:,i) = rightImage_haar(1:rows_l/2, cols_l/2+1:cols_l,i);
65+
sub22_r(:,:,i) = rightImage_haar(rows_l/2+1:rows_l, 1:cols_l/2,i);
66+
sub23_r(:,:,i) = rightImage_haar(rows_l/2+1:rows_l, cols_l/2+1:cols_l,i);
67+
end
68+
figure;image(uint8(leftImage_haar));title('Left Image after haar filter');
69+
figure;image(uint8(rightImage_haar));title('Right Image after haar filter');
70+
71+
% With more detail
72+
figure
73+
subplot(4,4,1)
74+
image(sub11_l./255)
75+
subplot(4,4,2)
76+
image(sub12_l)
77+
subplot(4,4,5)
78+
image(sub13_l)
79+
subplot(4,4,6)
80+
image(sub14_l)
81+
% Now for the bigger subbands we are going to use bigger subplots
82+
subplot(4,4,[3:4 7:8])
83+
image(sub21_l)
84+
subplot(4,4,[9:10 13:14])
85+
image(sub22_l)
86+
subplot(4,4,[11:12 15:16])
87+
image(sub23_l)
88+
89+
% With more detail
90+
figure
91+
subplot(4,4,1)
92+
image(sub11_r./255)
93+
subplot(4,4,2)
94+
image(sub12_r)
95+
subplot(4,4,5)
96+
image(sub13_r)
97+
subplot(4,4,6)
98+
image(sub14_r)
99+
% Now for the bigger subbands we are going to use bigger subplots
100+
subplot(4,4,[3:4 7:8])
101+
image(sub21_r)
102+
subplot(4,4,[9:10 13:14])
103+
image(sub22_r)
104+
subplot(4,4,[11:12 15:16])
105+
image(sub23_r)
106+
107+
leftImage_dq = zeros(size(leftImage,1),size(leftImage,2),size(leftImage,3));
108+
leftImage_synth = zeros(size(leftImage,1),size(leftImage,2),size(leftImage,3));
109+
110+
rightImage_dq =zeros(size(rightImage,1),size(rightImage,2),size(rightImage,3));
111+
rightImage_synth = zeros(size(rightImage,1),size(rightImage,2),size(rightImage,3));
112+
113+
for i=1:3
114+
q_sub11_l(:,:,i) = sub11_l(:,:,i);
115+
q_sub12_l(:,:,i) = quantize_matrix(sub12_l(:,:,i),4);
116+
q_sub13_l(:,:,i) = quantize_matrix(sub13_l(:,:,i),4);
117+
q_sub14_l(:,:,i) = quantize_matrix(sub14_l(:,:,i),4);
118+
q_sub21_l(:,:,i) = quantize_matrix(sub21_l(:,:,i),3);
119+
q_sub22_l(:,:,i) = quantize_matrix(sub22_l(:,:,i),3);
120+
q_sub23_l(:,:,i) = quantize_matrix(sub23_l(:,:,i),3);
121+
122+
q_sub11_r(:,:,i) = sub11_r(:,:,i);
123+
q_sub12_r(:,:,i) = quantize_matrix(sub12_r(:,:,i),4);
124+
q_sub13_r(:,:,i) = quantize_matrix(sub13_r(:,:,i),4);
125+
q_sub14_r(:,:,i) = quantize_matrix(sub14_r(:,:,i),4);
126+
q_sub21_r(:,:,i) = quantize_matrix(sub21_r(:,:,i),3);
127+
q_sub22_r(:,:,i) = quantize_matrix(sub22_r(:,:,i),3);
128+
q_sub23_r(:,:,i) = quantize_matrix(sub23_r(:,:,i),3);
129+
130+
%Synthesis
131+
leftImage_dq(1:rows_l/4, 1:cols_l/4,i)= q_sub11_l(:,:,i);
132+
leftImage_dq(1:rows_l/4, cols_l/4+1:cols_l/2,i) = dequantize_matrix(q_sub12_l(:,:,i),4,8);
133+
leftImage_dq(rows_l/4+1:rows_l/2, 1:cols_l/4,i) = dequantize_matrix(q_sub13_l(:,:,i),4,8);
134+
leftImage_dq(rows_l/4+1:rows_l/2, cols_l/4+1:cols_l/2,i) = dequantize_matrix(q_sub14_l(:,:,i),4,8);
135+
leftImage_dq(1:rows_l/2, cols_l/2+1:cols_l,i) = dequantize_matrix(q_sub21_l(:,:,i),3,8);
136+
leftImage_dq(rows_l/2+1:rows_l, 1:cols_l/2,i) = dequantize_matrix(q_sub22_l(:,:,i),3,8);
137+
leftImage_dq(rows_l/2+1:rows_l, cols_l/2+1:cols_l,i) = dequantize_matrix(q_sub23_l(:,:,i),4,8);
138+
leftImage_synth(:,:,i) = haar_reverse_multilevel(leftImage_dq(:,:,i),2);
139+
140+
rightImage_dq(1:rows_r/4, 1:cols_r/4,i)= q_sub11_r(:,:,i);
141+
rightImage_dq(1:rows_r/4, cols_r/4+1:cols_r/2,i) = dequantize_matrix(q_sub12_r(:,:,i),4,8);
142+
rightImage_dq(rows_r/4+1:rows_r/2, 1:cols_r/4,i) = dequantize_matrix(q_sub13_r(:,:,i),4,8);
143+
rightImage_dq(rows_r/4+1:rows_r/2, cols_r/4+1:cols_r/2,i) = dequantize_matrix(q_sub14_r(:,:,i),4,8);
144+
rightImage_dq(1:rows_r/2, cols_r/2+1:cols_r,i) = dequantize_matrix(q_sub21_r(:,:,i),3,8);
145+
rightImage_dq(rows_r/2+1:rows_r, 1:cols_r/2,i) = dequantize_matrix(q_sub22_r(:,:,i),3,8);
146+
rightImage_dq(rows_r/2+1:rows_r, cols_r/2+1:cols_r,i) = dequantize_matrix(q_sub23_r(:,:,i),4,8);
147+
rightImage_synth(:,:,i) = haar_reverse_multilevel(rightImage_dq(:,:,i),2);
148+
end
149+
<<<<<<< HEAD
150+
figure; image(uint8(rightImage_synth));
151+
figure; image(uint8(leftImage_synth));
152+
153+
%Anaglyph of the synthesis
154+
anaglyph_synth = uint8(leftImage_synth+rightImage_synth);
155+
figure,image(anaglyph_synth), title('Synthesized Anaglyph')
156+
157+
function paddedMatrix = paddingZeros(matrix, divisor)
158+
%paddedMatrix = zeros(size(matrix,1), size(matrix,2), size(matrix,3));
159+
rows = size(matrix,1);
160+
cols = size(matrix,2);
161+
paddedMatrix = wextend('ar','sym',matrix, round(rows/divisor)*divisor - rows, 'd');
162+
paddedMatrix = wextend('ac','sym',paddedMatrix, round(cols/divisor)*divisor - cols, 'l');
163+
end
164+
=======
165+
figure; image(uint8(rightImage_synth)), title('Right Image Synth');
166+
figure; image(uint8(leftImage_synth)), title('Left Image Synth');
167+
168+
% Encode each eye's image using filters of different (usually chromatically opposite) colors, red and blue
169+
rightImage_synth = imtranslate(rightImage_synth,[10, 0]);
170+
171+
figure,image(rightImage_synth), title('Anaglyph Synth');
172+
r = zeros(size(rightImage_synth));
173+
gb = zeros(size(leftImage_synth));
174+
r(:,:,1) = double(rightImage_synth(:,:,1));
175+
gb(:,:,2:3) = double(leftImage_synth(:,:,2:3));
176+
anaglyph = uint8(r+gb);
177+
figure,image(anaglyph), title('Anaglyph Synth');
178+
179+
% Create stereo anaglyph with Matlab function to check the result
180+
J = stereoAnaglyph(rightImage_synth,leftImage_synth);
181+
figure, image(uint8(J)), title('Anaglyph using Matlab function')
182+
183+
>>>>>>> 6a9213beae2e0128673f00ecc9ab774415114025

0 commit comments

Comments
 (0)