-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
306 lines (231 loc) · 9.75 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import cv2
from tkinter import *
from tkinter import filedialog
from PIL import Image
from PIL import ImageTk
from math import cos, sin, pi
import random
from time import *
import sys
sys.setrecursionlimit(10**7)
from near_algorithm import *
from mst_algorithm import *
from classes import *
############################ tkinter window ############################
window = Tk()
window.title("image fourier transformation")
window.geometry("1400x400+100+100")
window.resizable(False, False)
############################### functions ##############################
connectList = []
pointList = []
def segment_function(p1, p2, t): # 0 <= t <= 1
return p1 + (p2-p1)*t
def connectlist_to_func(t): # 0 <= t <= 1
global connectList
idx = int(t*len(connectList)-1)
para = t*len(connectList)-idx
return segment_function(connectList[idx], connectList[idx+1], para)
def totalDistance():
N = len(connectList)
d = 0
for i in range(N):
d += connectList[i].getDistance(connectList[(i+1)%N])
return d
def outline_action():
global src
global connectList
# color img -> gray sclae -> outline
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
outline = cv2.Canny(gray, 100, 255)
# color inverse
for i in range(len(outline)):
for j in range(len(outline[i])):
outline[i][j] = 255-outline[i][j]
# points -> connect list
connectList, cluster = connect_points(outline)
print("total distance : ", totalDistance())
# draw_by_cluster(canvas_outline, cluster)
draw_by_list(canvas_outline, connectList, (0,0,0))
# transform img form
img = Image.fromarray(outline)
imgtk = ImageTk.PhotoImage(image=img)
label.config(image=imgtk)
label.image = imgtk
print("Outline Detection Finished")
def connect_points(outline):
# detect black point
for i in range(img_width):
for j in range(img_height):
if outline[j][i] == 0: # 0 : white, 255 : black
pointList.append(Point(i, j))
# connect near point
connectList = [pointList[0]]
cluster = [[]]
print("clustet 시작")
a = time()
while len(connectList) != len(pointList):
point = connectList[-1]
near, cluster = nearest_point_cluster(outline, point, cluster)
connectList.append(near)
connectList.append(connectList[0])
# return connectList, cluster
cluster[-1].append(cluster[0][0])
print('cluster 수 : ', len(cluster))
b = time()
print("cluster 걸리는 시간 : ", b-a)
# edge 전체로 kruskal을 돌리는 경우
# pointGraph = pointToGraph(pointList)
# mst = kruskal(pointGraph, len(pointList))
# mst_path = mst_dfs(mst, pointList)
# print(cluster)
# print(mst)
# cluster 나눠서 kruskal을 돌리는 경우
mst_apro = cluster_kruskal(cluster)
mst_aproIdx = [(pointFind(mst_apro[i][0], pointList), pointFind(mst_apro[i][1], pointList)) for i in range(len(mst_apro))]
mst_path = mst_dfs(mst_aproIdx, pointList)
# TSP 2-approximation algoirthm
# tsp_approx_path = TSP_approx(mst_path)
connectList = [pointList[i] for i in mst_path]
print("kruskal 걸리는 시간 : ", time() - b)
return connectList, cluster
########################### fourier functions ##########################
def integrate(g):
# integrate g(t) from 0 to 1
h = 0.001
N = int(1/h)
return h*sum(g(h*(i+0.5)) for i in range(N))
def complex_fourier_transform(x, y, N):
# input x(t), y(t)
# returns dictionary of complex fourier constants c_i, where -N <= i <= N
c = dict()
for i in range(-N, N+1):
real_func = lambda t: cos(2*pi*i*t) * x(t) + sin(2*pi*i*t) * y(t)
imag_func = lambda t: -sin(2*pi*i*t) * x(t) + cos(2*pi*i*t) * y(t)
c[i] = integrate(real_func) + integrate(imag_func) * 1j
return c
def fourier_action():
# fourier transformation
connectList.append(connectList[0])
x = lambda t: connectlist_to_func(t).x
y = lambda t: connectlist_to_func(t).y
N = slider_N.get()
c = complex_fourier_transform(x, y, N)
window.update()
centers = dict()
circles = dict()
arrows = dict()
centers[0] = 0
canvas_fourier.delete('all')
for i in range(1, N+1):
centers[i] = centers[1-i] + c[1-i]
tmp = convert(centers[i].real, centers[i].imag, abs(c[i]))
circles[i] = canvas_fourier.create_oval(tmp[0], tmp[1], tmp[2], tmp[3], fill="", outline = "#ff948c")
arrows[i] = canvas_fourier.create_line(centers[1-i].real, centers[1-i].imag, centers[i].real, centers[i].imag, width=2)
centers[-i] = centers[i] + c[i]
tmp = convert(centers[-i].real, centers[-i].imag, abs(c[-i]))
circles[-i] = canvas_fourier.create_oval(tmp[0], tmp[1], tmp[2], tmp[3], fill="", outline = "#ff948c")
arrows[-i] = canvas_fourier.create_line(centers[i].real, centers[i].imag, centers[-i].real, centers[-i].imag, width=2)
canvas_fourier.delete(arrows[1])
position = centers[-N] + c[-N]
arrows[N+1] = canvas_fourier.create_line(centers[-N].real, centers[-N].imag, position.real, position.imag)
M = slider_M.get()
for k in range(M):
t = k / M
for i in range(1, N+1):
centers[i] = centers[1-i] + c[1-i] * (cos(2*pi*(1-i)*t) + 1j * sin(2*pi*(1-i)*t))
tmp = convert(centers[i].real, centers[i].imag, abs(c[i]))
canvas_fourier.coords(circles[i], tmp[0], tmp[1], tmp[2], tmp[3])
canvas_fourier.coords(arrows[i], centers[1-i].real, centers[1-i].imag, centers[i].real, centers[i].imag)
centers[-i] = centers[i] + c[i] * (cos(2*pi*i*t) + 1j * sin(2*pi*i*t))
tmp = convert(centers[-i].real, centers[-i].imag, abs(c[-i]))
canvas_fourier.coords(circles[-i], tmp[0], tmp[1], tmp[2], tmp[3])
canvas_fourier.coords(arrows[-i], centers[i].real, centers[i].imag, centers[-i].real, centers[-i].imag)
position = centers[-N] + c[-N] * (cos(2*pi*(-N)*t) + 1j * sin(2*pi*(-N)*t))
canvas_fourier.coords(arrows[N+1], centers[-N].real, centers[-N].imag, position.real, position.imag)
canvas_fourier.create_oval(position.real, position.imag, position.real+1, position.imag+1, fill="blue")
window.update()
print("Fourier Transform Finished")
for i in range(1,N+1):
canvas_fourier.delete(circles[i])
canvas_fourier.delete(circles[-i])
canvas_fourier.delete(arrows[i])
canvas_fourier.delete(arrows[-i])
def convert(centerx, centery, radius):
return centerx-radius, centery-radius, centerx+radius, centery+radius
############################## GUI functions #############################
def draw_by_list(canvas, l, color):
color = "#%02x%02x%02x" % (color[0], color[1], color[2])
for i in range(len(l)-1):
canvas.create_line(l[i].x, l[i].y, l[i+1].x, l[i+1].y, fill=color, width=1)
def draw_by_cluster(canvas, cluster):
canvas.delete("all")
color = (0, 255, 255)
print(len(cluster))
random.seed(100)
for l in cluster:
print(len(l))
color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
# color = (0,0,0)
draw_by_list(canvas, l, color)
def load():
global src, img, imgtk
window.filename = filedialog.askopenfilename(initialdir = "./", title = "Select file")
src = cv2.imread(window.filename)
src = cv2.resize(src, (img_width, img_height))
# transform opencv(BGR) to tkinter(RGB)
img = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
# transform numpy array to img
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
label.configure(image=imgtk)
label.image = imgtk
############################## load img ###############################
img_width, img_height = 400, 400
src = cv2.imread("./img/lion.jpg")
src = cv2.resize(src, (img_width, img_height))
# transform opencv(BGR) to tkinter(RGB)
img = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
# transform numpy array to img
img = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=img)
label = Label(window, image=imgtk)
label.place(x=800, y=0)
label.pack
############################# Draw Line ################################
pastx, pasty = 0, 0
startbool = False
def drawing(event):
global pastx, pasty, startbool
if startbool:
canvas_outline.create_line(pastx, pasty, event.x, event.y)
pastx, pasty = event.x, event.y
connectList.append(Point(event.x,event.y))
startbool = True
############################## Clearing ################################
def clear():
global startbool, connectList
canvas_outline.delete('all')
connectList = []
startbool = False
################################ GUI ###################################
canvas_fourier = Canvas(window, width=img_width, height=img_height, bg="white", bd=2)
canvas_fourier.place(x=0, y=0)
canvas_outline = Canvas(window, width=img_width, height=img_height, bg="white", bd=2)
canvas_outline.place(x=400, y=0)
canvas_outline.bind("<B1-Motion>", drawing)
button_outline = Button(window, text="outline detection", command=outline_action)
button_outline.place(x=1200,y=0, width=200, height=75)
button_fourier = Button(window, text="fourier transform", command=fourier_action)
button_fourier.place(x=1200,y=75, width=200, height=75)
button_clear = Button(window, text="Clear Line", command=clear)
button_clear.place(x=1200,y=150, width=200, height=75)
button_load = Button(window, text="load img", command=load)
button_load.place(x=1200, y=225, width=200, height=75)
slider_N = Scale(window, from_=0, to=500, orient=HORIZONTAL, sliderlength=15, length=150)
slider_N.place(x=1225,y=300)
slider_N.set(10)
slider_M = Scale(window, from_=0, to=3000, orient=HORIZONTAL, sliderlength=15, length=150)
slider_M.place(x=1225,y=350)
slider_M.set(300)
window.mainloop()