-
Notifications
You must be signed in to change notification settings - Fork 3
/
coarse_refining.py
89 lines (68 loc) · 2.51 KB
/
coarse_refining.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
from sklearn.metrics import mutual_info_score
import cv2
import numpy as np
detector = cv2.ORB_create()
video_number = 'v12'
video_path = video_number + '.mp4'
print ('Coarse Refining: ', video_path)
capture = cv2.VideoCapture(video_path)
total_frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
detector = cv2.ORB_create()
def coarse_refine(frame1,frame2):
kp1 = detector.detect(frame1, None)
kp1 , des1 = detector.compute(frame1, kp1)
des1 = np.array(des1)
kp2 = detector.detect(frame2, None)
kp2 , des2 = detector.compute(frame2, kp2)
des2 = np.array(des2)
if not des1.any():
print ('des1 empty')
return 0
elif not des2.any():
print ('des2 empty')
return 0
else:
#print ('frame1 = ', frame1.shape, ', shape = ', frame2.shape, ',des1 = ', des1)
#print ('des1 type = ', type(des1), ', shape = ', des1.shape)
#print ('=', des1)
r1,c1 = des1.shape
#print ('des1 rows = ', r1 , ', des1 cols = ', c1)
des1 = np.reshape(des1, (r1*c1))
des1 = des1[0:(r1*c1)]
#print ('des1 shape = ', des1.shape)
r2,c2 = des2.shape
#print ('des2 rows = ', r2 , ', des2 cols = ', c2)
des2 = np.reshape(des2, (r2*c2))
#print ('des2 shape = **', des2.shape)
shape_des1 = r1*c1
shape_des2 = r2*c2
if shape_des1 > shape_des2:
des1 = des1[0:(r2*c2)]
des2 = des2[0:(r2*c2)]
elif shape_des2 > shape_des1:
des1 = des1[0:(r1*c1)]
des2 = des2[0:(r1*c1)]
else:
des1 = des1[0:(r1*c1)]
des2 = des2[0:(r2*c2)]
#print ('des2 shape = ', des2.shape)
mi = mutual_info_score(des1,des2)
return mi
def video_processing():
frames_counter = 0
while(frames_counter < total_frames-1):
frames_counter = frames_counter + 1
print ('Processing ', str(frames_counter), ' out of ', total_frames)
ret, frame = capture.read()
cv2.imshow('f', frame)
cv2.waitKey(5)
if (ret):
previous_frame = frame
status, frame = capture.read()
frames_distance = coarse_refine(frame,previous_frame)
if frames_distance > 1.8:
name = video_number + '\\Coarse-refine\\'+str(frames_counter)+'.jpg'
cv2.imwrite(name,frame)
print ('Frame written ', name, ',distance = ',frames_distance)
print ('m i = ' , frames_distance)
video_processing()