-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproject_img.py
234 lines (199 loc) · 9.16 KB
/
project_img.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
from PyQt4 import QtCore, QtGui, uic
import sys
import cv2
import numpy as np
import threading
import time
import Queue
import easygui
import os
from matplotlib import pyplot as plt
running = False
capture_thread = None
form_class = uic.loadUiType("simple.ui")[0]
q = Queue.Queue()
canny = False
laplacian = False
sobel = False
sobel_x = False
sobel_y = False
source = False
contrast = False
image_f = False
dft = False
def grab(file_name, queue, width, height, fps):
global running, image_f
image_f = cv2.imread(file_name)
running = True
class OwnImageWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(OwnImageWidget, self).__init__(parent)
self.image = None
def setImage(self, image):
self.image = image
sz = image.size()
self.setMinimumSize(sz)
self.update()
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
if self.image:
qp.drawImage(QtCore.QPoint(0, 0), self.image)
qp.end()
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.canny.stateChanged.connect(self.update_setting)
self.sobel.stateChanged.connect(self.update_setting)
self.sobel_x.stateChanged.connect(self.update_setting)
self.sobel_y.stateChanged.connect(self.update_setting)
self.laplacian.stateChanged.connect(self.update_setting)
self.inversion.stateChanged.connect(self.update_setting)
self.hist_eq.stateChanged.connect(self.update_setting)
self.gray.stateChanged.connect(self.update_setting)
self.corner.stateChanged.connect(self.update_setting)
self.blob.stateChanged.connect(self.update_setting)
self.canny_max.valueChanged.connect(self.update_setting)
self.canny_min.valueChanged.connect(self.update_setting)
self.brightness_value.valueChanged.connect(self.update_setting)
self.contrast_value.valueChanged.connect(self.update_setting)
self.erosion_value.valueChanged.connect(self.update_setting)
self.dilation_value.valueChanged.connect(self.update_setting)
self.corner_maxc.valueChanged.connect(self.update_setting)
self.corner_mind.valueChanged.connect(self.update_setting)
self.corner_quality.valueChanged.connect(self.update_setting)
self.rotate.valueChanged.connect(self.update_setting)
self.translate_x.valueChanged.connect(self.update_setting)
self.translate_y.valueChanged.connect(self.update_setting)
self.zoom_value.valueChanged.connect(self.update_setting)
self.gaussian_blur.valueChanged.connect(self.update_setting)
self.median_blur.valueChanged.connect(self.update_setting)
self.window_width = self.ImgWidget.frameSize().width()
self.window_height = self.ImgWidget.frameSize().height()
self.ImgWidget = OwnImageWidget(self.ImgWidget)
self.ImgWidget_O = OwnImageWidget(self.ImgWidget_O)
self.update_frame()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(1)
self.startWebcam.setEnabled(False)
self.startVideo.setEnabled(False)
self.startImage.clicked.connect(self.change_image)
self.dft.clicked.connect(self.show_dft)
def show_dft(self):
f = np.fft.fft2(image_f)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('TU Project\n--------------------------\nDiscrete Fourier Transform')
plt.show()
def change_image(self):
global image_f, running
file_name = easygui.fileopenbox(filetypes = ["*.png","*.jpg"])
image_f = cv2.imread(file_name)
running = True
def update_setting(self):
global running
running = True
def update_frame(self):
global running, image, dft
if running:
img = image_f.copy()
img_height, img_width, img_colors = img.shape
scale_w = float(self.window_width) / float(img_width)
scale_h = float(self.window_height) / float(img_height)
scale = min([scale_w, scale_h])
if scale == 0:
scale = 1
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width, bpc = img.shape
bpl = bpc * width
img_o = img.copy()
if self.inversion.isChecked():
img = cv2.bitwise_not(img)
if self.brightness_value.value() > 0:
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(hsv)
v += self.brightness_value.value()
#v = np.where((255 - v) < 255,255,v+self.brightness_value.value())
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2RGB)
if self.zoom_value.value() != 100:
resize_val = int(float(self.zoom_value.value())*0.1)
print resize_val
img = cv2.resize(img,None,fx=resize_val, fy=resize_val)
if self.translate_x.value() != 100:
M = np.float32([[1,0,self.translate_x.value()-100],[0,1,0]])
rows,cols,d = img.shape
img = cv2.warpAffine(img,M,(cols,rows))
if self.translate_y.value() != 100:
M = np.float32([[1,0,0],[0,1,self.translate_y.value()-100]])
rows,cols,d = img.shape
img = cv2.warpAffine(img,M,(cols,rows))
if self.hist_eq.isChecked():
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
if self.gray.isChecked():
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
if self.gaussian_blur.value()>3:
img = cv2.GaussianBlur(img,(self.gaussian_blur.value(),self.gaussian_blur.value()),0)
if self.median_blur.value()>3:
img = cv2.medianBlur(img,self.median_blur.value())
if self.rotate.value() > 0:
cols = img.shape[1]
rows = img.shape[0]
M = cv2.getRotationMatrix2D((cols/2,rows/2),360-self.rotate.value(),1)
img = cv2.warpAffine(img,M,(cols,rows))
kernel = np.ones((5,5),np.uint8)
if self.erosion_value.value() > 0:
img = cv2.erode(img,kernel,iterations = self.erosion_value.value())
if self.dilation_value.value() > 0:
img = cv2.dilate(img,kernel,iterations = self.dilation_value.value())
if self.canny.isChecked():
img = cv2.Canny(img,self.canny_min.value(),self.canny_max.value())
img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
elif self.laplacian.isChecked():
img = cv2.Laplacian(img,cv2.CV_8U)
elif self.sobel.isChecked():
if self.sobel_x.isChecked():
img = cv2.Sobel(img,cv2.CV_8U,1,0,ksize=5)
if self.sobel_y.isChecked():
img = cv2.Sobel(img,cv2.CV_8U,0,1,ksize=5)
if self.corner.isChecked():
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
gray = np.float32(gray)
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
corners=cv2.goodFeaturesToTrack(gray,
self.corner_maxc.value(),
0.0001*float(self.corner_quality.value()),
self.corner_mind.value())
corners=np.int0(corners)
for corner in corners:
x,y = corner.ravel()
cv2.circle(img,(x,y),3,255,-1)
if self.blob.isChecked():
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
img = cv2.bitwise_and(img,img,mask= mask)
image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
image_o = QtGui.QImage(img_o.data, width, height, bpl, QtGui.QImage.Format_RGB888)
self.ImgWidget.setImage(image)
self.ImgWidget_O.setImage(image_o)
running = False
def closeEvent(self, event):
global running
running = False
file_name = easygui.fileopenbox(filetypes = ["*.png","*.jpg"])
capture_thread = threading.Thread(target=grab, args = (file_name, q, 1920, 1080, 15))
capture_thread.start()
app = QtGui.QApplication(sys.argv)
w = MyWindowClass(None)
w.setWindowTitle('TU Image Processing Project')
w.show()
app.exec_()