-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageProcessor.py
executable file
·255 lines (212 loc) · 7.82 KB
/
imageProcessor.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
import cv2 as cv
import scipy
import scipy.ndimage
import scipy.ndimage.morphology
import skimage
import skimage.morphology
from PyQt5.QtCore import pyqtSignal, QObject
from imageAlgos import thresholdOTSU, thresholdMaxEntropy
class basicImage(QObject):
imageChanged = pyqtSignal()
def __init__(self, parent=None):
super(basicImage, self).__init__(parent)
self.__image = None
# reset only variables define in current class
def __reset(self):
self.__image = None
# reset all
def reset(self):
self.__reset()
@staticmethod
def __checkImage(image):
assert image is not None, 'empty image'
assert isinstance(image, np.ndarray), 'image should be numpy array'
assert image.ndim == 2, 'greyscale image only'
assert image.dtype == np.uint8, 'uint8 pixel type only'
# set current image
def setImage(self, image):
print('image set')
if (self.__image is not None) and np.array_equal(self.__image, image):
return
self.__checkImage(image)
self.__reset()
self.__image = image
print('image emited!')
self.imageChanged.emit()
# get current image
def getImage(self):
assert self.__image is not None, 'image unset'
return self.__image.copy()
# read image from file
def loadImage(self, imgPath):
image = cv.imread(imgPath, cv.IMREAD_GRAYSCALE) # greyscale, uint8 img
self.setImage(image)
# save image to file
def saveImage(self, imgPath):
image = self.getImage()
cv.imwrite(imgPath, image)
class binaryImage(basicImage):
thresholdChanged = pyqtSignal(float)
def __init__(self):
super(binaryImage, self).__init__()
self.__threshold = None
self.__binaryImage = None
def __reset(self):
self.__threshold = None
self.__binaryImage = None
def reset(self):
super(binaryImage, self).reset()
self.__reset()
# get current thresholding
def setThreshold(self, threshold):
print('set threshold to '+str(threshold))
eps = 1e-5
threshold = float(threshold)
if (self.__threshold is not None) and \
(abs(threshold - self.__threshold) < eps):
return
self.__reset()
self.__threshold = threshold
print('threshold emited!')
self.thresholdChanged.emit(threshold)
# get current thresholding
def getThreshold(self):
assert self.__threshold is not None, 'threshold unset'
return self.__threshold
# get binary image
def getBinImage(self):
if self.__binaryImage is not None:
return self.__binaryImage
else:
image = self.getImage()
threshold = self.getThreshold()
maxval = 255 #uint8 max
_, self.__binaryImage = \
cv.threshold(image, threshold, maxval, cv.THRESH_BINARY)
return self.__binaryImage.copy()
def getOtsuThreshold(self):
image = self.getImage()
threshold = thresholdOTSU(image)
return threshold
def getEntropyThreshold(self):
image = self.getImage()
threshold = thresholdMaxEntropy(image)
return threshold
class imageProcessor(binaryImage):
def __init__(self):
super(imageProcessor, self).__init__()
self.__skeleton = None
self.__distance = None
def __reset(self):
self.__skeleton = None
self.__distance = None
def setThreshold(self, threshold):
super(imageProcessor, self).setThreshold(threshold)
self.__reset()
def reset(self):
super(imageProcessorImage, self).reset()
self.__reset()
def binary_dilation(self, *args, **kwargs):
image = self.getBinImage()
out = scipy.ndimage.morphology.binary_dilation(\
image, *args, **kwargs)
return (out*255).astype(np.uint8)
def binary_erosion(self, *args, **kwargs):
image = self.getBinImage()
out = scipy.ndimage.morphology.binary_erosion(\
image, *args, **kwargs)
return (out*255).astype(np.uint8)
def binary_opening(self, *args, **kwargs):
image = self.getBinImage()
out = scipy.ndimage.morphology.binary_opening(\
image, *args, **kwargs)
return (out*255).astype(np.uint8)
def binary_closing(self, *args, **kwargs):
image = self.getBinImage()
out = scipy.ndimage.morphology.binary_closing(\
image, *args, **kwargs)
return (out*255).astype(np.uint8)
def grey_dilation(self, *args, **kwargs):
image = self.getImage()
out = scipy.ndimage.morphology.grey_dilation(\
image, *args, **kwargs)
return out
def grey_erosion(self, *args, **kwargs):
image = self.getImage()
return scipy.ndimage.morphology.grey_erosion(\
image, *args, **kwargs)
def grey_opening(self, *args, **kwargs):
image = self.getImage()
return scipy.ndimage.morphology.grey_opening(\
image, *args, **kwargs)
def grey_closing(self, *args, **kwargs):
image = self.getImage()
return scipy.ndimage.morphology.grey_closing(\
image, *args, **kwargs)
def perform_medial_axis(self):
image = self.getBinImage()
skeleton, dist = skimage.morphology.medial_axis(\
image, return_distance=True)
self.__distance = dist
self.__skeleton = skeleton
def distance(self):
if self.__distance is None:
self.perform_medial_axis()
if self.__distance.max() > 255:
return (self.__distance/ \
float(self.__distance.max())).astype(np.uint8)
else:
return self.__distance.astype(np.uint8)
def skeletonize(self):
if self.__skeleton is None:
self.perform_medial_axis()
return (255*self.__skeleton).astype(np.uint8)
def restoration(self):
assert self.__skeleton is not None
assert self.__distance is not None
return (255*skimage.morphology.reconstruction( \
self.__skeleton, self.__distance)).astype(np.uint8)
def morphological_edge(self):
return self.binary_dilation() - self.binary_erosion()
def morphological_gradient(self):
return self.grey_dilation(size=(3,3)) - self.grey_erosion(size=(3,3))
def conditional_dilation(self):
mask = self.getBinImage()
marker = self.binary_opening()
while True:
print('conditonal dialtion loop')
T = marker.copy()
marker = scipy.ndimage.morphology.binary_dilation(marker)
marker = marker*mask
marker = (255*(marker > 0)).astype(np.uint8)
if np.array_equal(T, marker):
print('conditonal dialtion done')
return marker
def greyscale_reconstruction(self):
mask = self.getImage()
marker = self.grey_opening(size=(5,5))
eps = 1e-4
while True:
print('greyscale conditonal dialtion loop')
T = marker.copy()
marker = scipy.ndimage.morphology.grey_dilation(marker, size=(3,3))
marker = np.minimum(marker, mask)
if np.max(np.abs(T - marker)) < eps:
print('greyscale conditonal dialtion done')
return marker
if __name__ == '__main__':
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget
imgPath = 'pics/Lenna.png'
if len(sys.argv) > 1:
imgPath = sys.argv[1]
processor = imageProcessor()
processor.loadImage(imgPath)
app = QApplication(sys.argv)
ex = QWidget()
ex.setGeometry(300, 300, 600, 600)
ex.show()
sys.exit(app.exec_())