-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.py
253 lines (192 loc) · 9.51 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
import sys
import os
from PyQt5 import uic
from PyQt5 import QtGui
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QColor, QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QCheckBox, QComboBox, QFileDialog, QMainWindow, QLabel, QPushButton, QSlider
import cv2
import numpy as np
import os
def generateSolidColorPixmap(w, h, color):
canvas = QImage(QSize(w, h), QImage.Format_RGB30)
for baris in range(0, h):
for kolom in range(0, w):
canvas.setPixel(kolom, baris, color.rgb())
return canvas
class MainWindow(QMainWindow):
selectedHue = 0
selectedSaturation = 255
selectedValue = 255
lowerHSV = (0, 0, 0)
upperHSV = (179, 255, 255)
imgRaw = None
imgMask = None
imgMasked = None
imgHsvSpace = None
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi(os.path.join(os.path.dirname(
__file__), "./assets/main_window.ui"), self)
self.sliderH = self.findChild(QSlider, "sliderH")
self.sliderS = self.findChild(QSlider, "sliderS")
self.sliderV = self.findChild(QSlider, "sliderV")
self.lblH = self.findChild(QLabel, "lblH")
self.lblS = self.findChild(QLabel, "lblS")
self.lblV = self.findChild(QLabel, "lblV")
self.lblLower = self.findChild(QLabel, "lblLower")
self.lblUpper = self.findChild(QLabel, "lblUpper")
self.previewH = self.findChild(QLabel, "previewH")
self.previewS = self.findChild(QLabel, "previewS")
self.previewV = self.findChild(QLabel, "previewV")
self.previewRaw = self.findChild(QLabel, "previewRaw")
self.previewMask = self.findChild(QLabel, "previewMask")
self.previewMaskedRaw = self.findChild(QLabel, "previewMaskedRaw")
self.previewHsvSpace = self.findChild(QLabel, "previewHsvSpace")
self.cboxSetMode = self.findChild(QComboBox, "cboxSetMode")
self.cboxErode = self.findChild(QCheckBox, "cboxErode")
self.sliderErotion = self.findChild(QSlider, "sliderErotion")
self.cboxDilate = self.findChild(QCheckBox, "cboxDilate")
self.sliderDilation = self.findChild(QSlider, "sliderDilation")
self.btnOpen = self.findChild(QPushButton, "btnOpen")
self.btnCopy = self.findChild(QPushButton, "btnCopy")
self.init_handler()
self.loadHsvSpace()
self.updateHSVPreview()
def loadHsvSpace(self):
self.imgHsvSpace = cv2.imread(os.path.join(os.path.dirname(__file__), "assets", "hsv_color.png"))
def init_handler(self):
self.sliderH.valueChanged.connect(self.onHChanged)
self.sliderS.valueChanged.connect(self.onSChanged)
self.sliderV.valueChanged.connect(self.onVChanged)
self.cboxSetMode.currentTextChanged.connect(self.onCBoxModeChanged)
self.btnOpen.clicked.connect(self.onBtnOpenClicked)
self.btnCopy.clicked.connect(self.onBtnCopyClicked)
self.cboxDilate.stateChanged.connect(self.updateMask)
self.cboxErode.stateChanged.connect(self.updateMask)
self.sliderErotion.valueChanged.connect(self.onSliderErodeChanged)
self.sliderDilation.valueChanged.connect(self.onSliderDilateChanged)
def onBtnCopyClicked(self):
print("Upper HSV: ", self.upperHSV)
print("Lower HSV: ", self.lowerHSV)
# =========== Helper ===========
def updatePreviewHsvSpace(self):
if self.imgHsvSpace is None:
return
frame_HSV = cv2.cvtColor(self.imgHsvSpace, cv2.COLOR_BGR2HSV)
lower_orange = np.array(self.lowerHSV)
upper_orange = np.array(self.upperHSV)
frame_threshold = cv2.inRange(
frame_HSV, lower_orange, upper_orange)
frame_threshold = cv2.bitwise_and(self.imgHsvSpace, self.imgHsvSpace, mask=frame_threshold)
_asQImage = QImage(
frame_threshold.data, frame_threshold.shape[1], frame_threshold.shape[0], frame_threshold.shape[1]*3, QtGui.QImage.Format_RGB888)
_asQImage = _asQImage.rgbSwapped()
self.previewHsvSpace.setPixmap(QPixmap.fromImage(_asQImage).scaledToWidth(self.previewMask.size().width()))
def updateHSVPreview(self):
prevH = generateSolidColorPixmap(
200, 300, QColor.fromHsv(self.selectedHue, 255, 255))
self.previewH.setPixmap(QPixmap.fromImage(prevH))
prevS = generateSolidColorPixmap(
200, 300, QColor.fromHsv(self.selectedHue, self.selectedSaturation, 255))
self.previewS.setPixmap(QPixmap.fromImage(prevS))
prevV = generateSolidColorPixmap(
200, 300, QColor.fromHsv(self.selectedHue, self.selectedSaturation, self.selectedValue))
self.previewV.setPixmap(QPixmap.fromImage(prevV))
if self.cboxSetMode.currentText() == "UPPER":
self.upperHSV = (self.selectedHue // 2,
self.selectedSaturation, self.selectedValue)
self.lblUpper.setText(
f"H {self.upperHSV[0]}; S {self.upperHSV[1]}; V {self.upperHSV[2]}")
elif self.cboxSetMode.currentText() == "LOWER":
self.lowerHSV = (self.selectedHue // 2,
self.selectedSaturation, self.selectedValue)
self.lblLower.setText(
f"H {self.lowerHSV[0]}; S {self.lowerHSV[1]}; V {self.lowerHSV[2]}")
self.updateMask()
self.updatePreviewHsvSpace()
def updateRawImg(self, img):
# _dsize = (self.previewRaw.size().height(),
# self.previewRaw.size().width())
self.imgRaw = img
_imgAsQImg = QImage(
self.imgRaw.data, self.imgRaw.shape[1], self.imgRaw.shape[0], QImage.Format_RGB888).rgbSwapped()
# self.imgRaw = img.scaled(200,100, QtCore.KeepAspectRatio)
# self.imgRaw = img.scaledToHeight(self.previewMask.size().height())
self.previewRaw.setPixmap(QPixmap.fromImage(
_imgAsQImg).scaledToWidth(self.previewRaw.size().width()))
def updateMask(self):
if self.imgRaw is None:
return
frame_HSV = cv2.cvtColor(self.imgRaw, cv2.COLOR_BGR2HSV)
lower_orange = np.array(self.lowerHSV)
upper_orange = np.array(self.upperHSV)
frame_threshold = cv2.inRange(
frame_HSV, lower_orange, upper_orange)
if self.cboxErode.isChecked():
_kernel = self.sliderErotion.value()
frame_threshold = cv2.erode(frame_threshold, np.ones((_kernel, _kernel), dtype=np.uint8))
if self.cboxDilate.isChecked():
_kernel = self.sliderDilation.value()
frame_threshold = cv2.dilate(frame_threshold, np.ones((_kernel, _kernel), dtype=np.uint8))
self.updateMaskedRaw(frame_threshold)
frame_threshold = cv2.cvtColor(frame_threshold, cv2.COLOR_GRAY2RGB)
_asQImage = QImage(
frame_threshold.data, frame_threshold.shape[1], frame_threshold.shape[0], frame_threshold.shape[1]*3, QtGui.QImage.Format_RGB888)
_asQImage = _asQImage.rgbSwapped()
self.previewMask.setPixmap(QPixmap.fromImage(_asQImage).scaledToHeight(self.previewMask.size().height()))
def updateMaskedRaw(self, masking):
if self.imgRaw is None:
return
frame_threshold = cv2.bitwise_and(self.imgRaw, self.imgRaw, mask=masking)
_asQImage = QImage(
frame_threshold.data, frame_threshold.shape[1], frame_threshold.shape[0], frame_threshold.shape[1]*3, QtGui.QImage.Format_RGB888)
_asQImage = _asQImage.rgbSwapped()
self.previewMaskedRaw.setPixmap(QPixmap.fromImage(_asQImage).scaledToHeight(self.previewMaskedRaw.size().height()))
# =========== EVENT HANDLER ===========
def onCBoxModeChanged(self, text):
if text == "UPPER":
self.selectedHue = self.upperHSV[0] * 2
self.selectedSaturation = self.upperHSV[1]
self.selectedValue = self.upperHSV[2]
elif text == "LOWER":
self.selectedHue = self.lowerHSV[0] * 2
self.selectedSaturation = self.lowerHSV[1]
self.selectedValue = self.lowerHSV[2]
self.sliderH.setValue(self.selectedHue)
self.sliderS.setValue(self.selectedSaturation)
self.sliderV.setValue(self.selectedValue)
self.updateHSVPreview()
def onHChanged(self):
_v = self.selectedHue = self.sliderH.value()
self.lblH.setText(str(f"QT5 ({_v}) | cv2 ({_v//2})"))
self.updateHSVPreview()
def onSChanged(self):
_v = self.selectedSaturation = self.sliderS.value()
self.lblS.setText(str(_v))
self.updateHSVPreview()
def onVChanged(self):
_v = self.selectedValue = self.sliderV.value()
self.lblV.setText(str(_v))
self.updateHSVPreview()
def onSliderErodeChanged(self):
self.cboxErode.setText(f"Erode {self.sliderErotion.value()}")
self.updateMask()
def onSliderDilateChanged(self):
self.cboxDilate.setText(f"Dilate {self.sliderDilation.value()}")
self.updateMask()
def onBtnOpenClicked(self):
options = QFileDialog.Options()
fileName, _ = QFileDialog.getOpenFileName(
self, "QFileDialog.getOpenFileName()", "", "All Files (*);;Jpeg (*.jpeg);;BMP (*.bmp)", options=options)
if not fileName:
return
# self.srcQimg = QImage(fileName=fileName, format=QImage.Format_RGB32)
self.updateRawImg(cv2.imread(fileName))
# with open(fileName, 'rb') as f:
# self.updateRawImg(QImage.fromData(f.read()))
if __name__ == "__main__":
app = QApplication([])
widget = MainWindow()
widget.show()
sys.exit(app.exec_())