-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
194 lines (170 loc) · 7.7 KB
/
operations.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
import imutils
import numpy as np
from PyQt5.QtWidgets import QMessageBox
from skimage.io import imread
from skimage import filters
from PyQt5 import QtGui, QtWidgets
import qimage2ndarray
import cv2 as opencv
from skimage.util import random_noise
def QImageToCvMat(incomingImage):
incomingImage.save('temp.png', 'png')
mat = opencv.imread('temp.png')
mat = mat[:, :, ::-1]
return mat
def warnMessage(message):
msg = QMessageBox()
msg.setWindowTitle("WARNING!")
msg.setText(message)
msg.exec_()
def checkInputPhotoExist(stateManager):
if stateManager.inputSource == '':
return False
else:
return True
def checkOutputPhotoExist(stateManager):
if stateManager.outputSource != '':
return True
else:
return False
class Operations():
def openSource(imageLabel, stateManager, outputImageLabel):
dialogMenu = QtWidgets.QDialog()
sourcePath, _ = QtWidgets.QFileDialog.getOpenFileName(dialogMenu,
"Open Image", "", "Image (*.jpg *.png)")
if sourcePath != '':
imageLabel.setPixmap(QtGui.QPixmap(sourcePath))
stateManager.importImage(sourcePath)
stateManager.outputSource = ''
outputImageLabel.setPixmap(QtGui.QPixmap())
def saveImageAs(stateManager):
if checkOutputPhotoExist(stateManager):
dialogMenu = QtWidgets.QDialog()
Image = stateManager.outputSource
fileName = QtWidgets.QFileDialog.getSaveFileName(dialogMenu, "Save File",
"",
"Images (*.png *.jpg)")
Image.save(fileName[0])
else:
warnMessage("Output Image can not be null!!")
def Roberts(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
grayImage = opencv.cvtColor(inputImage, opencv.COLOR_BGR2GRAY)
edgeRoberts = filters.roberts(grayImage)
outputQImage = qimage2ndarray.array2qimage(
edgeRoberts, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def rgbToGrayscale(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = opencv.cvtColor(inputImage, opencv.COLOR_BGR2GRAY)
outputQImage = qimage2ndarray.gray2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def blurImage(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = opencv.blur(inputImage, (5, 5))
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def flipImage(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = opencv.flip(inputImage, 0)
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def mirrorImage(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = opencv.flip(inputImage, 1)
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def rotateImage(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = imutils.rotate(inputImage, 90)
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def reverseColor(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = opencv.bitwise_not(inputImage)
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def addNoise(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
outputData = random_noise(inputImage, mode='s&p', amount=0.1)
outputData = np.array(255 * outputData, dtype='uint8')
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")
def deBlur(outputImageLabel, stateManager):
if checkInputPhotoExist(stateManager):
if stateManager.outputSource != '':
inputImage = QImageToCvMat(stateManager.outputSource)
else:
inputImage = imread(stateManager.inputSource)
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
outputData = opencv.filter2D(src=inputImage, ddepth=-1, kernel=kernel)
outputQImage = qimage2ndarray.array2qimage(outputData, normalize=True)
outputImageLabel.setPixmap(QtGui.QPixmap(outputQImage))
stateManager.imageOperation(
outputQImage)
else:
warnMessage("Please open a image!")