-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
302 lines (244 loc) · 11.4 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
import os
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QPoint, QRect, QSize, Qt
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2
from matplotlib import pyplot as plt
import numpy as np
import qdarkstyle
import scipy.misc
import colorsys
from PIL import Image
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
self.bg = QFrame(self)
self.bg.setGeometry(0, 0, 800, 600)
self.pic = QLabel(self.bg)
self.pic.setBackgroundRole(QPalette.Base)
self.pic.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.pic.setScaledContents(True)
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addWidget(okButton)
vbox.addWidget(cancelButton)
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.bg)
hbox.addLayout(vbox)
self.setLayout(hbox)
self.setGeometry(0, 0, 800, 600)
# self.pic.setGeometry(10, 10,800,600)
# use full ABSOLUTE path to the image, not relative
self.current_image_path = os.getcwd() +"/1.bmp"
self.image = QtGui.QImage()
self.cv2Image = cv2.imread(self.current_image_path, 0)
self.selectedImage = None #The image, selected via rubberband
self.image = self.convertCv2ToQimage(self.cv2Image)
self.pic.setPixmap(QtGui.QPixmap.fromImage(self.image))
# If you pass a parent to PySide.QtGui.QRubberBand ¡®s constructor,
# the rubber band will display only inside its parent, but stays on top of other child widgets.
# If no parent is passed, PySide.QtGui.QRubberBand will act as a top-level widget.
# self.rubberband = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle,)
self.rubberband = QRubberBand(QRubberBand.Rectangle, self.pic)
self.setMouseTracking(True)
# Create new action
openAction = QAction(QIcon('open.png'), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open document')
openAction.triggered.connect(self.openCall)
# Create new action
saveAction = QAction(QIcon('open.png'), '&Save', self)
saveAction.setShortcut('Ctrl+O')
saveAction.setStatusTip('Save document')
saveAction.triggered.connect(self.saveCall)
# Show the histogram
showHistogram = QAction(QIcon('open.png'), '&Show histogram', self)
showHistogram.setStatusTip('Shows the histogram')
showHistogram.triggered.connect(self.show_histogram)
# Image Processing
Action = QAction(QIcon('open.png'), '&Example image processing', self)
Action.setShortcut('Ctrl+P')
Action.setStatusTip('Changes the image')
Action.triggered.connect(self.example_image_processing_method)
# Create new action
showDefaultImage = QAction(QIcon('open.png'), '&Show Default Image', self) # показує дефолтне(відкрите) зображення
showDefaultImage.setShortcut('Ctrl+D')
showDefaultImage.setStatusTip('Shows default image')
showDefaultImage.triggered.connect(self.ShowDefaultImage)
# Create new action
self.ShowSelectedImage = QAction(QIcon('open.png'), '&Show', self)
self.ShowSelectedImage.setShortcut('Ctrl+S')
self.ShowSelectedImage.setStatusTip('Shows the selected area')
self.ShowSelectedImage.triggered.connect(self.showSelectedImage)
# Create new action
self.SaveSelectedImage = QAction(QIcon('open.png'), '&SaveSelected', self)
self.SaveSelectedImage.setStatusTip('Saves selected image')
self.SaveSelectedImage.triggered.connect(self.saveSelectedImage)
# Create new action
self.ContrastSelectedImage = QAction(QIcon('open.png'), '&Raise Contract', self)
self.ContrastSelectedImage.setShortcut('Ctrl+C')
self.ContrastSelectedImage.setStatusTip('Raises the contrast')
self.ContrastSelectedImage.triggered.connect(self.raiseContrastSelectedImage)
# Create new action
self.HslSelectedImage = QAction(QIcon('open.png'), '&Colorize', self)
self.HslSelectedImage.setShortcut('Ctrl+H')
self.HslSelectedImage.setStatusTip('Converting to HSL')
self.HslSelectedImage.triggered.connect(self.selectedToHsl)
self.SaveSelectedImage.setEnabled(False)
self.ContrastSelectedImage.setEnabled(False)
self.HslSelectedImage.setEnabled(False)
self.ShowSelectedImage.setEnabled(False)
# Create menu bar and add action
menuBar = self.menuBar()
MenuFile = menuBar.addMenu('&File')
MenuFile.addAction(openAction)
MenuFile.addAction(saveAction)
MenuImage = menuBar.addMenu('&Image')
MenuImage.addAction(Action)
MenuImage.addAction(showHistogram)
MenuImage.addAction(showDefaultImage)
MenuSelected = menuBar.addMenu('&Selected Image')
MenuSelected.addAction(self.ShowSelectedImage)
MenuSelected.addAction(self.SaveSelectedImage)
MenuSelected.addAction(self.ContrastSelectedImage)
MenuSelected.addAction(self.HslSelectedImage)
# def contextMenuEvent(self, event):
# x = event.pos().x()
# y = event.pos().y()
# rect = self.rubberband.geometry()
# if x>=rect.x() and x<= (rect.x()+rect.width()) and y>=rect.y() and y<=(rect.y() + rect.height())
# menu = QMenu(self)
# quitAction = menu.addAction("Quit")
# action = menu.exec_(self.mapToGlobal(event.pos()))
# if action == quitAction:
# qApp.quit()
def raiseContrastSelectedImage(self):
self.selectedImage = self.HighContrast(self.selectedImage.copy())
def selectedToHsl(self):
self.selectedImage = self.grayScale2BGR(self.selectedImage.copy())
def saveSelectedImage(self):
rgb = Image.fromarray(self.selectedImage, mode=None)
path = QFileDialog.getSaveFileName(self, 'Save File')
if path:
print(path)
rgb.save(path[0])
def showSelectedImage(self):
cv2.imshow('Selected Image', self.selectedImage)
def show_histogram(self):
plt.hist(self.selectedImage.ravel(), 256, [1, 255])
plt.show()
def mousePressEvent(self, event):
self.rubberband.hide()
self.ShowSelectedImage.setEnabled(False)
self.SaveSelectedImage.setEnabled(False)
self.ContrastSelectedImage.setEnabled(False)
self.HslSelectedImage.setEnabled(False)
self.origin = self.pic.mapFromParent(event .pos())
self.rubberband.setGeometry(QtCore.QRect(self.origin, QtCore.QSize()))
self.rubberband.show()
# QtGui.QWidget.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
rect = self.rubberband.geometry()
if rect.width() > 10 and rect.height() > 10:
self.selectedImage = self.cropImage(rect)
print(type(self.selectedImage))
self.ShowSelectedImage.setEnabled(True)
self.SaveSelectedImage.setEnabled(True)
self.ContrastSelectedImage.setEnabled(True)
self.HslSelectedImage.setEnabled(True)
def mouseMoveEvent(self, event):
if self.rubberband.isVisible():
# Control the Rubber within the imageViewer!!!
self.rubberband.setGeometry(QtCore.QRect(self.origin, event .pos()) & self.image.rect())
# QtGui.QWidget.mouseMoveEvent(self, event)
def convertQImageToMat(self, incomingImage):
''' Converts a QImage into an opencv MAT format '''
incomingImage = incomingImage.convertToFormat(4)
width = incomingImage.width()
height = incomingImage.height()
ptr = incomingImage.bits()
ptr.setsize(incomingImage.byteCount())
arr = np.array(ptr).reshape(height, width, 4) # Copies the data
return cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
def convertCv2ToQimage(self, im):
qim = QImage()
if im is None:
return qim
if im.dtype == np.uint8:
if len(im.shape) == 2:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8)
qim.setColorTable([qRgb(i, i, i) for i in range(256)])
elif len(im.shape) == 3:
if im.shape[2] == 3:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888)
elif im.shape[2] == 4:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32)
return qim
def cropImage(self, rect):
croppedImage = self.image.copy(rect)
return self.convertQImageToMat(croppedImage)
def changeLabelPic(self,cv2Image):
self.image = self.convertCv2ToQimage(cv2Image)
self.pic.setPixmap(QtGui.QPixmap.fromImage(self.image))
def saveCall(self):
rgb = Image.fromarray(self.cv2Image, mode=None)
path = QFileDialog.getSaveFileName(self, 'Save File')
if path:
print(path)
rgb.save(path[0])
def openCall(self):
dlg = QFileDialog()
# dlg.setFileMode(QFileDialog.AnyFile)
if dlg.exec_():
self.current_image_path = dlg.selectedFiles()[0]
self.setWindowTitle(self.current_image_path)
self.cv2Image = cv2.imread(self.current_image_path, 0)
self.changeLabelPic(self.cv2Image)
def cannyEdges(self):
edges = cv2.Canny(self.cv2Image, 1, 1)
self.cv2Image = edges
cv2.imshow('kek', self.cv2Image)
# self.cv2Image = cv2.Sobel(self.cv2Image, cv2.CV_8U, 1, 0, ksize=3)
self.changeLabelPic(self.cv2Image)
numpyArray = self.cv2Image
print(numpyArray)
def grayScale2BGR(self, grayScaleImage):
hslImage = np.zeros((grayScaleImage.shape[0], grayScaleImage.shape[1], 3), np.uint8)
for i in range(1, grayScaleImage.shape[0]):
for j in range(1, grayScaleImage.shape[1] - 2):
# hslColor = ((int(self.cv2Image[i][j] + abs(int(self.cv2Image[i][j]) - int(self.cv2Image[i][j + 1]))) * 361 / 256), 100, 100)
hslColor = (int(grayScaleImage[i][j]) * 361 / 256, 100, 100)
hslImage[i][j] = hslColor
print(hslColor)
return cv2.cvtColor(hslImage, cv2.COLOR_HLS2BGR)
def HighContrast(self, grayImage):
min = grayImage.min()
max= grayImage.max()
Range = max - min
for i in range(int(grayImage.shape[0])):
for j in range(int(grayImage.shape[1])):
grayImage[i][j] = (grayImage[i][j] - min) * (255/Range)
return grayImage
def example_image_processing_method(self): #обробка зображення
colorized = self.grayScale2BGR(self.cv2Image)
self.cv2Image = colorized
# self.cv2Image = self.grayScale2BGR(self.cv2Image.copy())
# self.cv2Image = cv2.Sobel(self.cv2Image, cv2.CV_8U, 1, 0, ksize=3)
self.changeLabelPic(self.cv2Image)
numpyArray = self.cv2Image
print(numpyArray)
def ShowDefaultImage(self):
self.cv2Image = cv2.imread(self.current_image_path,0)
self.changeLabelPic(self.cv2Image)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())