Debian Codegraph @

planetary_system_stacker/Test_programs/Test_Center_of_Gravity_v1.0.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 @author: Michal Powalko [email protected]
4
5 Version 1.0 [2019-08-01]
6 ------------------------
7 INF: Inititial release
8
9 """
10
11 import cv2
12 import time
13 import numpy as np
14 from scipy import ndimage
15 import matplotlib
16 matplotlib.use('Agg')
17 import matplotlib.pyplot as plt
18
19 # https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html
20 # https://www.learnopencv.com/find-center-of-blob-centroid-using-opencv-cpp-python/
21 # https://docs.opencv.org/3.4.0/d7/d4d/tutorial_py_thresholding.html
22 # https://en.wikipedia.org/wiki/Image_moment#Examples
23
24 LOOPS = int(1)
25
26 file_path = r'D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter\2019-05-26-0115_4-L-Jupiter_ZWO ASI290MM Mini_pipp.avi'
27 cap = cv2.VideoCapture(file_path)
28 frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
29
30 tic = time.time()
31 frames = [cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY) for idx in range(frame_count)]
32 print('Importing the video file took:\t\t{0:.2f} [s]'.format(time.time() - tic))
33 cap.release()
34
35 tic = time.time()
36 frames = [cv2.GaussianBlur(frame, (3, 3), 0) for frame in frames]
37 print('GaussianBlur took:\t\t\t{0:.2f} [s]'.format(time.time() - tic))
38
39 tic = time.time()
40 for idx in range(LOOPS):
41 cog_rolf = [ndimage.measurements.center_of_mass(np.uint8(1)*(frame >= (np.max(frame)/2).astype(frame.dtype)))
42 for frame in frames]
43 cog_rolf = [(int(round(aaa[0])), int(round(aaa[1]))) for aaa in cog_rolf]
44 print('Rolf/Jens CoG estimation took:\t\t{0:.2f} [s]'.format(time.time() - tic))
45
46 tic = time.time()
47 for idx in range(LOOPS):
48 MM = [cv2.moments(cv2.threshold(frame, frame.max()/2, 1, cv2.THRESH_BINARY)[1])
49 for frame in frames]
50 cog_michal = [(round(M["m01"] / M["m00"]), round(M['m10'] / M["m00"])) for M in MM]
51 print('Michals CoG estimation took:\t\t{0:.2f} [s]'.format(time.time() - tic))
52
53 dev_x = [a[1] - b[1] for a,b in zip(cog_rolf, cog_michal)]
54 dev_y = [a[0] - b[0] for a,b in zip(cog_rolf, cog_michal)]
55
56 plt.subplot(2, 1, 1)
57 plt.title('Center of gravity estimation')
58 plt.plot(dev_x, '-b')
59 plt.xlabel('Frame chronological number')
60 plt.ylabel('Error in x axis [pixel]')
61 plt.grid(True)
62 plt.subplot(2, 1, 2)
63 plt.title('Center of gravity estimation')
64 plt.plot(dev_y, '-r')
65 plt.xlabel('Frame chronological number')
66 plt.ylabel('Error in y axis [pixel]')
67 plt.grid(True)
68 pass
69