-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
141 lines (113 loc) · 4.11 KB
/
utils.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
135
136
137
138
139
140
# Edge detection algorithm : Canny, sobel
# source file
import cv2
import os
import imageio
import numpy as np
def load_img(img_dir):
'''
input : your image file directory
output : opencv image
'''
# image load
image = cv2.imread(img_dir,cv2.IMREAD_GRAYSCALE)
if image is None: raise Exception("Image not found error, please check image path")
return image
def sobel_make(image, growth_rate = 0.01, back_ground = "white"):
'''
- sobel has one threshold that is a scale parameter
- we set scale parameter's range 0~1
- growth_rate 0.01
- we have x-direction , y-direction images so we have 200 images to make gif
'''
# ------------- setting -----------
cnt = 0
weight_x = 0
weight_y = 0
vid_frames = []
# ------------- sobel -----------
while weight_x < 1 or weight_y < 1:
# OpenCV 제공 소벨 에지 계산
# x-direction differentiation - vertical mask
dst1 = cv2.Sobel(np.float32(image), cv2.CV_32F, 1, 0, scale=weight_x, ksize = 3)
# y-direction differentiation - horizontal mask
dst2 = cv2.Sobel(np.float32(image), cv2.CV_32F, 0, 1, scale=weight_y, ksize = 3)
# --> Convert absolute value and uint8
dst1 = cv2.convertScaleAbs(dst1)
dst2 = cv2.convertScaleAbs(dst2)
# combine x-direction, y-direction mask iamge
merged_image = cv2.addWeighted(dst1, 0.5, dst2, 0.5, 0)
# count image
cnt += 1
# check back ground color and change
if back_ground == 'white':
sobel_inverted = cv2.bitwise_not(merged_image)
vid_frames.append(sobel_inverted)
elif back_ground == 'black':
vid_frames.append(merged_image)
else:
raise ValueError("Wrong input value, there only two input black, white")
# threshold change
if cnt % 2 == 1:
weight_x += growth_rate
elif cnt % 2 == 0:
weight_y += growth_rate
return vid_frames
def canny_make(image, reduction_rate = 10, back_ground = "white"):
'''
- using canny algorithm, edge detection
- input : your image
- output : detected frames list with multiple thresholds
--
- canny
- threshold1 : range(1000 -> 200)
- threshold2 : range(1000 -> 200)
- decrease (1000,1000) -> (900,1000) -> (900,900) -> (800,900) ...
- reduction_rate : control the decrease rate of threshold
'''
# setting
cnt = 0
min_th = 1000
max_th = 1000
# set output list
vid_frames = []
while min_th > 200 or max_th > 200:
# make image with canny
canny1 = cv2.Canny(image, threshold1=min_th, threshold2=max_th) # OpenCV canny
if back_ground == 'white':
canny1_inverted = cv2.bitwise_not(canny1)
vid_frames.append(canny1_inverted)
elif back_ground == 'black':
vid_frames.append(canny1)
else:
raise ValueError("Wrong input value, there only two input black, white")
cnt += 1
if cnt % 2 == 1: # 홀수번째, th1 감소
min_th -= reduction_rate
elif cnt % 2 == 0:
max_th -= reduction_rate
return vid_frames
def make_frames(image, method = "canny"):
'''
input : openCV image (image = cv2.imread())
output : frame list to make gif
'''
if method == 'canny':
frames = canny_make(image, reduction_rate = 10, back_ground='white')
elif method == 'sobel':
frames = sobel_make(image, growth_rate = 0.01, back_ground = 'white')
else:
print("NOT IMPLEMENT")
return frames
def save_gif_file(frames, save_dir = './output.gif'):
'''
- Save frames to gif file in your save_dir
'''
with imageio.get_writer(save_dir, mode="I") as writer: # mode : A string containing the modes that this format can handle ('iIvV'),“i” for an image, “I”
for f in frames:
writer.append_data(f) # save frame to gif
if os.path.exists(save_dir):
return True
else:
print("saving fail")
return False