| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2018 Rolf Hempel, [email protected] |
| 4 |
|
| 5 |
This file is part of the PlanetarySystemStacker tool (PSS). |
| 6 |
https://github.com/Rolf-Hempel/PlanetarySystemStacker |
| 7 |
|
| 8 |
PSS is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with PSS. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
from glob import glob |
| 24 |
from statistics import mean |
| 25 |
from time import time |
| 26 |
|
| 27 |
import matplotlib |
| 28 |
matplotlib.use('Agg') |
| 29 |
import matplotlib.pyplot as plt |
| 30 |
from cv2 import meanStdDev |
| 31 |
from numpy import array, full |
| 32 |
|
| 33 |
from configuration import Configuration |
| 34 |
from exceptions import ArgumentError, NotSupportedError, Error |
| 35 |
from frames import Frames |
| 36 |
from miscellaneous import Miscellaneous |
| 37 |
|
| 38 |
|
| 39 |
class RankFrames(object): |
| 40 |
""" |
| 41 |
Rank frames according to their overall sharpness. Experiments with different algorithms |
| 42 |
have been made. The classical "Sobel" algorithm is good but slow. An alternative is |
| 43 |
implemented in method "local_contrast" in module "miscellaneous". |
| 44 |
|
| 45 |
""" |
| 46 |
|
| 47 |
def __init__(self, frames, configuration, progress_signal=None): |
| 48 |
""" |
| 49 |
Initialize the object and instance variables. |
| 50 |
|
| 51 |
:param frames: Frames object with all video frames |
| 52 |
:param configuration: Configuration object with parameters |
| 53 |
:param progress_signal: Either None (no progress signalling), or a signal with the signature |
| 54 |
(str, int) with the current activity (str) and the progress in |
| 55 |
percent (int). |
| 56 |
""" |
| 57 |
|
| 58 |
|
| 59 |
self.shape = frames.shape |
| 60 |
self.configuration = configuration |
| 61 |
self.frames = frames |
| 62 |
|
| 63 |
self.number_original = frames.number |
| 64 |
self.frame_ranks_original = [] |
| 65 |
self.quality_sorted_indices_original = None |
| 66 |
self.rank_indices_original = None |
| 67 |
self.frame_ranks_max_index_original = None |
| 68 |
self.frame_ranks_max_value_original = None |
| 69 |
|
| 70 |
self.number = None |
| 71 |
self.frame_ranks = None |
| 72 |
self.quality_sorted_indices = None |
| 73 |
self.rank_indices = None |
| 74 |
self.frame_ranks_max_index = None |
| 75 |
self.frame_ranks_max_value = None |
| 76 |
|
| 77 |
self.progress_signal = progress_signal |
| 78 |
self.signal_step_size = max(int(self.number_original / 10), 1) |
| 79 |
|
| 80 |
def frame_score(self): |
| 81 |
""" |
| 82 |
Compute the frame quality values and normalize them such that the best value is 1. |
| 83 |
|
| 84 |
:return: - |
| 85 |
""" |
| 86 |
|
| 87 |
if self.configuration.rank_frames_method == "xy gradient": |
| 88 |
method = Miscellaneous.local_contrast |
| 89 |
elif self.configuration.rank_frames_method == "Laplace": |
| 90 |
method = Miscellaneous.local_contrast_laplace |
| 91 |
elif self.configuration.rank_frames_method == "Sobel": |
| 92 |
method = Miscellaneous.local_contrast_sobel |
| 93 |
else: |
| 94 |
raise NotSupportedError("Ranking method " + self.configuration.rank_frames_method + |
| 95 |
" not supported") |
| 96 |
|
| 97 |
|
| 98 |
if self.frames.index_translation_active: |
| 99 |
self.frames.reset_index_translation() |
| 100 |
|
| 101 |
|
| 102 |
if method != Miscellaneous.local_contrast_laplace: |
| 103 |
for frame_index in range(self.number_original): |
| 104 |
frame = self.frames.frames_mono_blurred(frame_index) |
| 105 |
if self.progress_signal is not None and frame_index % self.signal_step_size == 1: |
| 106 |
self.progress_signal.emit("Rank all frames", |
| 107 |
int(round(10*frame_index / self.number_original) * 10)) |
| 108 |
if self.configuration.frames_normalization: |
| 109 |
self.frame_ranks_original.append( |
| 110 |
method(frame, self.configuration.rank_frames_pixel_stride) / |
| 111 |
self.frames.average_brightness(frame_index)) |
| 112 |
else: |
| 113 |
self.frame_ranks_original.append( |
| 114 |
method(frame, self.configuration.rank_frames_pixel_stride)) |
| 115 |
else: |
| 116 |
for frame_index in range(self.number_original): |
| 117 |
frame = self.frames.frames_mono_blurred_laplacian(frame_index) |
| 118 |
|
| 119 |
if self.progress_signal is not None and frame_index % self.signal_step_size == 1: |
| 120 |
self.progress_signal.emit("Rank all frames", |
| 121 |
int(round(10*frame_index / self.number_original) * 10)) |
| 122 |
if self.configuration.frames_normalization: |
| 123 |
self.frame_ranks_original.append(meanStdDev(frame)[1][0][0] / |
| 124 |
self.frames.average_brightness(frame_index)) |
| 125 |
else: |
| 126 |
self.frame_ranks_original.append(meanStdDev(frame)[1][0][0]) |
| 127 |
|
| 128 |
|
| 129 |
self.quality_sorted_indices_original = sorted(range(self.number_original), |
| 130 |
key=self.frame_ranks_original.__getitem__, reverse=True) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
self.rank_indices_original = [self.quality_sorted_indices_original.index(index) for index in |
| 135 |
range(self.number_original)] |
| 136 |
|
| 137 |
if self.progress_signal is not None: |
| 138 |
self.progress_signal.emit("Rank all frames", 100) |
| 139 |
|
| 140 |
|
| 141 |
self.frame_ranks_max_index_original = self.quality_sorted_indices_original[0] |
| 142 |
self.frame_ranks_max_value_original = self.frame_ranks_original[self.frame_ranks_max_index_original] |
| 143 |
self.frame_ranks_original /= self.frame_ranks_max_value_original |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
self.number = self.number_original |
| 148 |
self.frame_ranks = self.frame_ranks_original |
| 149 |
self.quality_sorted_indices = self.quality_sorted_indices_original |
| 150 |
self.rank_indices = self.rank_indices_original |
| 151 |
self.frame_ranks_max_index = self.frame_ranks_max_index_original |
| 152 |
self.frame_ranks_max_value = self.frame_ranks_max_value_original |
| 153 |
|
| 154 |
def set_index_translation(self, index_translation): |
| 155 |
""" |
| 156 |
After frames have been marked to be excluded from the further workflow, update the ranking |
| 157 |
tables, based on the index translation list from the frames module. |
| 158 |
|
| 159 |
:param index_translation: List with indices. For each index in the reduced list of frames |
| 160 |
it gives the corresponding index in the original frame list. |
| 161 |
:return: - |
| 162 |
""" |
| 163 |
|
| 164 |
|
| 165 |
self.number = len(index_translation) |
| 166 |
|
| 167 |
self.frame_ranks = [self.frame_ranks_original[index] for index in index_translation] |
| 168 |
|
| 169 |
|
| 170 |
self.quality_sorted_indices = sorted(range(self.number), |
| 171 |
key=self.frame_ranks.__getitem__, reverse=True) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
self.rank_indices = [self.quality_sorted_indices.index(index) for index in |
| 176 |
range(self.number)] |
| 177 |
|
| 178 |
if self.progress_signal is not None: |
| 179 |
self.progress_signal.emit("Rank all frames", 100) |
| 180 |
|
| 181 |
|
| 182 |
self.frame_ranks_max_index = self.quality_sorted_indices[0] |
| 183 |
self.frame_ranks_max_value = self.frame_ranks[self.frame_ranks_max_index] |
| 184 |
self.frame_ranks /= self.frame_ranks_max_value |
| 185 |
|
| 186 |
def reset_index_translation(self): |
| 187 |
""" |
| 188 |
De-activate index translation and re-establish the original frame ranking data. |
| 189 |
|
| 190 |
:return: - |
| 191 |
""" |
| 192 |
|
| 193 |
self.number = self.number_original |
| 194 |
self.frame_ranks = self.frame_ranks_original |
| 195 |
self.quality_sorted_indices = self.quality_sorted_indices_original |
| 196 |
self.rank_indices = self.rank_indices_original |
| 197 |
self.frame_ranks_max_index = self.frame_ranks_max_index_original |
| 198 |
self.frame_ranks_max_value = self.frame_ranks_max_value_original |
| 199 |
|
| 200 |
def find_best_frames(self, number_frames, region_size): |
| 201 |
""" |
| 202 |
Find the indices of the best "number_frames" frames under the condition that all indices |
| 203 |
are within an interval of size "region_size". |
| 204 |
|
| 205 |
:param number_frames: Number of best frames the indices of which are to be found. |
| 206 |
:param region_size: Maximal width of index interval. |
| 207 |
:return: (List of frame indices, quality loss, time line position) with: |
| 208 |
List of frame indices: Indices of frames participating in mean frame computation. |
| 209 |
quality loss: Loss in average frame quality due to range restriction (%). |
| 210 |
time line position: Position of the average frame index relative to the total |
| 211 |
duration of the video. |
| 212 |
""" |
| 213 |
|
| 214 |
|
| 215 |
if number_frames > region_size: |
| 216 |
raise ArgumentError("Attempt to find " + str(number_frames) + " good frames in " |
| 217 |
"an index interval of size " + str(region_size)) |
| 218 |
elif region_size > self.number: |
| 219 |
raise ArgumentError("Size of best frames region " + str(region_size) + " larger " |
| 220 |
"than the total number of frames " + str(self.number)) |
| 221 |
|
| 222 |
best_indices = [] |
| 223 |
rank_sum_opt = 0. |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
for start_index in range(self.number - region_size + 1): |
| 229 |
end_index = start_index + region_size |
| 230 |
best_indices_in_range = sorted(range(start_index, end_index), |
| 231 |
key=self.frame_ranks.__getitem__, reverse=True)[ |
| 232 |
:number_frames] |
| 233 |
rank_sum = sum([self.frame_ranks[i] for i in best_indices_in_range]) |
| 234 |
if rank_sum > rank_sum_opt: |
| 235 |
rank_sum_opt = rank_sum |
| 236 |
best_indices = best_indices_in_range |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
rank_sum_global = sum( |
| 241 |
[self.frame_ranks[i] for i in self.quality_sorted_indices[:number_frames]]) |
| 242 |
quality_loss_percent = round(100. * (rank_sum_global - rank_sum_opt) / rank_sum_global, 1) |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
cog_mean_frame = round(100 * mean(best_indices) / self.number, 1) |
| 247 |
|
| 248 |
return best_indices, quality_loss_percent, cog_mean_frame |
| 249 |
|
| 250 |
|
| 251 |
if __name__ == "__main__": |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
type = 'video' |
| 256 |
if type == 'image': |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
names = glob('Images/Mond_*.jpg') |
| 261 |
else: |
| 262 |
names = 'Videos/another_short_video.avi' |
| 263 |
|
| 264 |
|
| 265 |
print(names) |
| 266 |
|
| 267 |
|
| 268 |
configuration = Configuration() |
| 269 |
configuration.initialize_configuration() |
| 270 |
try: |
| 271 |
frames = Frames(configuration, names, type=type) |
| 272 |
print("Number of images read: " + str(frames.number)) |
| 273 |
print("Image shape: " + str(frames.shape)) |
| 274 |
except Error as e: |
| 275 |
print("Error: " + e.message) |
| 276 |
exit() |
| 277 |
|
| 278 |
|
| 279 |
start = time() |
| 280 |
rank_frames = RankFrames(frames, configuration) |
| 281 |
rank_frames.frame_score() |
| 282 |
end = time() |
| 283 |
print('Elapsed time in ranking all frames: {}'.format(end - start)) |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
print("") |
| 294 |
num_frames = len(rank_frames.frame_ranks) |
| 295 |
frame_percent = 10 |
| 296 |
num_frames_stacked = max(1, round(num_frames*frame_percent/100.)) |
| 297 |
print("Percent of frames to be stacked: ", str(frame_percent), ", numnber: " |
| 298 |
+ str(num_frames_stacked)) |
| 299 |
quality_cutoff = rank_frames.frame_ranks[rank_frames.quality_sorted_indices[num_frames_stacked]] |
| 300 |
print("Quality cutoff: ", str(quality_cutoff)) |
| 301 |
|
| 302 |
|
| 303 |
ax1 = plt.subplot(211) |
| 304 |
|
| 305 |
x = array(rank_frames.frame_ranks) |
| 306 |
plt.ylabel('Frame number') |
| 307 |
plt.gca().invert_yaxis() |
| 308 |
y = array(range(num_frames)) |
| 309 |
x_cutoff = full((num_frames,), quality_cutoff) |
| 310 |
plt.xlabel('Quality') |
| 311 |
line1, = plt.plot(x, y, lw=1) |
| 312 |
line2, = plt.plot(x_cutoff, y, lw=1) |
| 313 |
index = 37 |
| 314 |
plt.scatter(x[index], y[index], s=20) |
| 315 |
plt.grid(True) |
| 316 |
|
| 317 |
|
| 318 |
ax2 = plt.subplot(212) |
| 319 |
|
| 320 |
x = array([rank_frames.frame_ranks[i] for i in rank_frames.quality_sorted_indices]) |
| 321 |
plt.ylabel('Frame rank') |
| 322 |
plt.gca().invert_yaxis() |
| 323 |
y = array(range(num_frames)) |
| 324 |
y_cutoff = full((num_frames,), num_frames_stacked) |
| 325 |
plt.xlabel('Quality') |
| 326 |
line3, = plt.plot(x, y, lw=1) |
| 327 |
line4, = plt.plot(x, y_cutoff, lw=1) |
| 328 |
index = 37 |
| 329 |
plt.scatter(x[index], y[index], s=20) |
| 330 |
plt.grid(True) |
| 331 |
|
| 332 |
plt.show() |
| 333 |
|
| 334 |
number = 3 |
| 335 |
window = 5 |
| 336 |
start = time() |
| 337 |
best_indices, quality_loss_percent, cog_mean_frame = rank_frames.find_best_frames(number, window) |
| 338 |
end = time() |
| 339 |
print ("\nIndices of best frames in window of size " + str(window) + " found in " + |
| 340 |
str(end - start) + " seconds: " + str(best_indices) + |
| 341 |
"\nQuality loss as compared to unrestricted selection: " + |
| 342 |
str(quality_loss_percent) + "%\nPosition of mean frame in video time line: " + |
| 343 |
str(cog_mean_frame) + "%") |