-
Notifications
You must be signed in to change notification settings - Fork 7
/
frame_difference.py
135 lines (102 loc) · 3.65 KB
/
frame_difference.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Joerg Hermann Mueller
#
# This file is part of pynephoscope.
#
# pynephoscope 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.
#
# pynephoscope 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 pynephoscope. If not, see <http://www.gnu.org/licenses/>.
import cv2
import sys
import numpy as np
import os.path
import glob
from skycamera import SkyCamera
class FrameDifference:
def __init__(self):
self.mask = cv2.cvtColor(SkyCamera.getBitMask(), cv2.COLOR_GRAY2BGR)
def difference(self, image1, image2):
image1 = np.int32(image1 * self.mask)
image2 = np.int32(image2 * self.mask)
self.diff = image1 - image2
def getValue(self):
return np.mean(np.abs(self.diff))
def getImage(self):
return cv2.cvtColor(np.uint8(np.abs(self.diff)), cv2.COLOR_BGR2GRAY)
class FrameComparison:
def __init__(self, files):
if len(files) < 2:
raise Exception('Need at least two files to compare.')
self.image_window = 'Image'
self.threshold_window = 'Threshold'
self.difference_window = 'Difference'
self.files = files
self.tb_threshold = 'Threshold'
self.tb_image = 'Image'
self.current_image = 0
self.image1 = None
self.image2 = None
self.difference = None
self.threshold = 25
self.gray = None
cv2.namedWindow(self.image_window, cv2.WINDOW_AUTOSIZE)
cv2.namedWindow(self.difference_window, cv2.WINDOW_AUTOSIZE)
cv2.namedWindow(self.threshold_window, cv2.WINDOW_AUTOSIZE)
cv2.createTrackbar(self.tb_image, self.difference_window, 0, len(self.files) - 2, self.selectImage)
cv2.createTrackbar(self.tb_threshold, self.threshold_window, self.threshold, 255, self.renderThreshold)
self.render()
def selectImage(self, number):
if number >= len(self.files) - 1 or number < 0:
return
self.current_image = number
self.render()
def render(self):
self.image1 = np.int32(cv2.imread(self.files[self.current_image], 1))
self.image2 = np.int32(cv2.imread(self.files[self.current_image + 1], 1))
self.difference = self.image1 - self.image2
self.gray = cv2.cvtColor(np.uint8(np.abs(self.difference)), cv2.COLOR_BGR2GRAY)
self.difference = np.uint8(self.difference * 2 + 128)
cv2.imshow(self.image_window, np.uint8(self.image1))
#cv2.imshow(self.difference_window, self.difference)
cv2.imshow(self.difference_window, self.gray)
self.renderThreshold(self.threshold)
def renderThreshold(self, threshold):
self.threshold = threshold
_, thresh = cv2.threshold(self.gray, threshold, 255, cv2.THRESH_BINARY)
cv2.imshow(self.threshold_window, thresh)
def run(self):
while(True):
k = cv2.waitKey(30) & 0xFF
if k == 27:
break
if k == ord('s'):
filename = 'out.png'
print('Saving ' + filename)
cv2.imwrite(filename, self.difference)
cv2.destroyAllWindows()
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: frame_difference <path>...')
exit(1)
files = []
for path in sys.argv[1:]:
if os.path.isfile(path):
files.append(path)
elif os.path.isdir(path):
files += sorted(glob.glob(os.path.join(path, "*.jpg")))
else:
print('Invalid parameter: %s' % path)
frame_comparison = FrameComparison(files)
frame_comparison.run()
#__import__("code").interact(local=locals())