This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_gen.m
116 lines (108 loc) · 4.45 KB
/
image_gen.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%% Single frame investigation
% You can wrap this section in loops to plot effects of different
% strategies, block size etc.
clear; clc; close all;
% Load the video sequence
video = VideoReader('source.avi');
% Basic parameters (IV)
reference_frame_update_cycle = 2;
block_size = 16;
search_range = 7;
num_of_frames = 2*12; % pick a frame you want to use here
% Viewing parameters
show_frame = false;
show_vectors = false;
show_difference = false;
show_predicted = false;
pause_time = 0; % how long the figures stay unwritten to, if you do multiple frames
% Acting loop
frame = 1;
% DVs you may be interested in
avg_MAD = 0; % average mean difference during motion estimation, measure of effectiveness/accuracy
num_compare = 0; % number of blocks that are compared, a measure for cost of estimation, will be the same across frames (* block_size^2 = about number FLOPs)
mean_difference_MAD = 0; % mean difference between prediction frame and original frame, measure of accuracy
% peak signal to noise ratio, a measure of error (peak~peak^2(which is 1 as the frame is converted to double)/MSE(RMSE^2)) in db
PSNR_func = @(mat_1, mat_2) 10*log10(1/rmse(mat_1, mat_2, 'all')^2);
% the main difference between the MAD should be from the zero padding
% calculation, but I'm not super certain.
% for if you decide to do multiple frames
avg_MAD_over_frames = 0;
mean_difference_MAD_over_frames = 0;
num_compare_over_frames = 0;
PSNR_over_frames = 0;
num_estimate = 0;
while frame < num_of_frames % adjust how many frames this will be done on
% get reference frame every few frames (which is typically transmitted as a whole)
if mod(frame, reference_frame_update_cycle) == 1
reference_frame = im2double(readFrame(video));
reference_frame_gray = im2gray(reference_frame);
reference_frame_number = frame;
frame = frame + 1;
end
% read the frame
current_frame = im2double(readFrame(video));
current_frame_gray = im2gray(current_frame);
% compute the motion vectors, change the method used here
[motion_vec, avg_MAD, num_compare] = motionEstimationByNTSS(reference_frame_gray, current_frame_gray, block_size, search_range);
num_compare_over_frames = num_compare_over_frames + num_compare;
avg_MAD_over_frames = avg_MAD_over_frames + avg_MAD;
if show_vectors
figure(1)
plot_motion(motion_vec, block_size);
end
if show_frame
figure(2);
subplot(1, 2, 1);
imshow(reference_frame);
title(sprintf("Reference (Frame %d)", reference_frame_number));
subplot(1, 2, 2);
imshow(current_frame);
title(sprintf("Current (Frame %d)", frame))
end
% compensate and find prediction error
[predicted_frame, prediction_difference, mean_difference_MAD] = motionCompensation(reference_frame, current_frame, motion_vec, block_size);
mean_difference_MAD_over_frames = mean_difference_MAD_over_frames + mean_difference_MAD;
PSNR_over_frames = PSNR_over_frames + PSNR_func(current_frame, predicted_frame);
if show_difference
figure(3);
imshow(prediction_difference);
end
if show_predicted
figure(4);
subplot(1, 2, 1);
imshow(predicted_frame);
title("Predicted Frame")
subplot(1, 2, 2);
imshow(current_frame);
title("Current Frame")
end
% pause between frames so you can see
if show_difference || show_frame || show_vectors || show_predicted
pause(pause_time);
end
frame = frame + 1;
num_estimate = num_estimate + 1;
end
% saving images
cwd = pwd+"\images\";
imwrite(predicted_frame, cwd+"predicted_frame.png");
imwrite(current_frame, cwd+"current_frame.png");
imwrite(reference_frame, cwd+"reference_frame.png");
imwrite(prediction_difference, cwd+"prediction_difference.png");
figure;
imshow(reference_frame);
hold on;
plot_motion(motion_vec, block_size);
imwrite(getframe(gca).cdata, cwd+"ref_with_vector.png")
figure;
plot_motion(motion_vec, block_size);
imwrite(getframe(gca).cdata, cwd+"just_vector.png")
% reporting
fprintf("The average MAD when comparing blocks is %0.5f. \n" + ...
"The average difference from prediction is %0.5f. \n" + ...
"The average peak to peak SNR is %.5f. \n"+ ...
"The number of operations done per motion estimation is approximately %d. \n", ...
avg_MAD_over_frames/num_estimate*255, ...
mean_difference_MAD_over_frames/num_estimate*255, ...
PSNR_over_frames/num_estimate, ...
num_compare_over_frames*block_size^2/num_estimate);