# -*- coding: utf-8; -*- """ Copyright (c) 2018 Rolf Hempel, rolf6419@gmx.de This file is part of the PlanetarySystemStacker tool (PSS). https://github.com/Rolf-Hempel/PlanetarySystemStacker PSS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PSS. If not, see . """ from glob import glob from os import path, remove from pathlib import Path from time import time import numpy as np from cv2 import imread, VideoCapture, CAP_PROP_FRAME_COUNT, cvtColor, COLOR_BGR2GRAY, \ COLOR_BGR2RGB, GaussianBlur, Laplacian, CV_32F, COLOR_RGB2BGR, imwrite, convertScaleAbs from scipy import misc from configuration import Configuration from exceptions import TypeError, ShapeError, ArgumentError, WrongOrderingError, Error # from frames import Frames class FramesOld(object): """ This object stores the image data of all frames. Four versions of the original frames are used throughout the data processing workflow. They are (re-)used in the folliwing phases: 1. Original (color) frames, type: uint8 / uint16 - Frame stacking ("stack_frames.stack_frames") 2. Monochrome version of 1., type: uint8 - Computing the average frame (only average frame subset, "align_frames.average_frame") 3. Gaussian blur added to 2., type: type: uint8 - Aligning all frames ("align_frames.align_frames") - Frame stacking ("stack_frames.stack_frames") 4. Down-sampled Laplacian of 3., type: uint8 - Overall image ranking ("rank_frames.frame_score") - Ranking frames at alignment points("alignment_points.compute_frame_qualities") Currently all versions of all frames are stored during the entire workflow. As an alternative, a non-buffered version should be added. """ def __init__(self, configuration, names, type='video', progress_signal=None): """ Initialize the Frame object, and read all images. Images can be stored in a video file or as single images in a directory. :param configuration: Configuration object with parameters :param names: In case "video": name of the video file. In case "image": list of names for all images. :param type: Either "video" or "image". :param progress_signal: Either None (no progress signalling), or a signal with the signature (str, int) with the current activity (str) and the progress in percent (int). """ self.configuration = configuration self.progress_signal = progress_signal if type == 'image': # Use scipy.misc to read in image files. self.number = len(names) self.signal_step_size = max(int(self.number / 10), 1) self.frames_original = [] for frame_index, path in enumerate(names): # After every "signal_step_size"th frame, send a progress signal to the main GUI. if self.progress_signal is not None and frame_index%self.signal_step_size == 0: self.progress_signal.emit("Read all frames", int((frame_index / self.number) * 100.)) # Read the next frame. frame = imread(path, -1) self.frames_original.append(frame) if self.progress_signal is not None: self.progress_signal.emit("Read all frames", 100) self.shape = self.frames_original[0].shape dt0 = self.frames_original[0].dtype # Test if all images have the same shape, color type and depth. # If not, raise an exception. for image in self.frames_original: if image.shape != self.shape: raise ShapeError("Images have different size") elif len(self.shape) != len(image.shape): raise ShapeError("Mixing grayscale and color images not supported") if image.dtype != dt0: raise TypeError("Images have different type") elif type == 'video': # In case "video", use OpenCV to capture frames from video file. Revert the implicit # conversion from RGB to BGR in OpenCV input. cap = VideoCapture(names) self.number = int(cap.get(CAP_PROP_FRAME_COUNT)) self.frames_original = [] self.signal_step_size = max(int(self.number / 10), 1) for frame_index in range(self.number): # After every "signal_step_size"th frame, send a progress signal to the main GUI. if self.progress_signal is not None and frame_index%self.signal_step_size == 0: self.progress_signal.emit("Read all frames", int((frame_index/self.number)*100.)) # Read the next frame. ret, frame = cap.read() if ret: self.frames_original.append(cvtColor(frame, COLOR_BGR2RGB)) else: raise IOError("Error in reading video frame") cap.release() if self.progress_signal is not None: self.progress_signal.emit("Read all frames", 100) self.shape = self.frames_original[0].shape dt0 = self.frames_original[0].dtype else: raise TypeError("Image type " + type + " not supported") # Monochrome images are stored as 2D arrays, color images as 3D. if len(self.shape) == 2: self.color = False elif len(self.shape) == 3: self.color = True else: raise ShapeError("Image shape not supported") # Set the depth value of all images to either 16 or 8 bits. if dt0 == 'uint16': self.depth = 16 elif dt0 == 'uint8': self.depth = 8 else: raise TypeError("Frame type " + str(dt0) + " not supported") # Initialize lists of monochrome frames (with and without Gaussian blur) and their # Laplacians. self.frames_monochrome = None self.frames_monochrome_blurred = None self.frames_monochrome_blurred_laplacian = None self.used_alignment_points = None def frames(self, index): """ Look up the original frame object with a given index. :param index: Frame index :return: Frame with index "index". """ # # Code for positioning at arbitrary index. # # Check for valid frame number. # if 0 <= index < self.number: # # Set frame position # cap.set(cv2.CAP_PROP_POS_FRAMES, index) if not 0<=index