-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaSelect.py
288 lines (221 loc) · 9.3 KB
/
AreaSelect.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 09:33:12 2020
@author: ungersebastian
"""
#%%
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5.QtGui import QImage,QPixmap, QTransform, QPainter, QPen, QPolygon, QBrush, QColor
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem, QFrame, QApplication
from PyQt5.QtCore import Qt, QPoint
import sys
import numpy as np
from shapely.geometry import Polygon, Point, LinearRing, LineString
class AreaSelect(QGraphicsView):
'''
In the image, the area inside the polygonal countour line is selected.
New points are added by either drag and drop or by a double click in the image
Existing points are deleted by a double click near the point
'''
DELTA = 10 #for the minimum distance
def __init__(self, image, parent = None, points = None):
super(AreaSelect, self).__init__(parent)
self.draggin_idx = -1
self._scene = QGraphicsScene(self)
self._image = QGraphicsPixmapItem()
self._scene.addItem(self._image)
self.setScene(self._scene)
self.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setFrameShape(QFrame.NoFrame)
self.image = image
self.w, self.h = self.image.shape
array = image.__copy__()
"""
if len(array.shape) == 3:
array = imProc.nip_to_color_image(array)
"""
imgShow = Image.fromarray(array)
if imgShow.mode == 'F':
imgShow = self.image_to_uint8(array)
elif imgShow.mode == 'I;16':
imgShow = self.image_to_uint8(array)
qtImage1 = ImageQt(imgShow)
qtImage2 = QImage(qtImage1)
self.pixmap = QPixmap.fromImage(qtImage2)
self.pixmap.detach()
self.pixmapOrig = self.pixmap.copy()
self.pixmapOrig.detach()
self._image.setPixmap(self.pixmap)
if points == None:
self.points = [
[self.h//2, self.w//3],
[self.h//3, 2*self.w//3],
[2*self.h//3, 2*self.w//3]
]
else:
self.points = points
self.__create_new_plotmap__()
self.win_scale = [1,1]
self.height = self.w
self.width = self.h
self.show()
self.win_scale = [1,1]
self.area = np.zeros([self.w, self.h]).astype(int)
self.__create_region__()
self.__resize__()
def __resize__(self):
h = self.mapToScene(self.viewport().rect()).boundingRect().height()
r = self.sceneRect()
r.setHeight(h)
self.setSceneRect(r)
height = self.viewport().height()
width = self.viewport().width()
self.win_scale = [self.width / width, self.height / height]
for item in self.items():
item_height = item.boundingRect().height()
item_width = item.boundingRect().width()
tr = QTransform()
tr.scale(width / item_width, height / item_height)
item.setTransform(tr)
def image_to_uint8(self, my_image):
temp = (my_image - my_image.min())
temp = temp / temp.max()
temp = np.asarray(temp * 255, dtype=np.uint8)
return( Image.fromarray(temp.astype(np.uint8), mode='L') )
def __plot_points__(self):
newMap = QPixmap(self.h, self.w)
newMap.fill(Qt.transparent)
painter = QPainter(self)
painter.begin(newMap)
pen = QPen(Qt.red, 1)
painter.setPen(pen)
for p in self.points:
painter.drawPoint(p[0] , p[1])
pen = QPen(Qt.red,0.5)
painter.setPen(pen)
brush = QBrush(QColor(255,0,0,20))
painter.setBrush(brush)
poly = QPolygon([QPoint(p[0], p[1]) for p in self.points])
painter.drawPolygon(poly)
painter.end()
return(newMap)
def __create_new_plotmap__(self):
newMap = self.__plot_points__()
newMapItem = QGraphicsPixmapItem()
newMapItem.setPixmap(newMap)
r = self.sceneRect()
self._scene.addItem(newMapItem)
self.setSceneRect(r)
def __update_plotmap__(self):
newMap = self.__plot_points__()
newMapItem = QGraphicsPixmapItem()
newMapItem.setPixmap(newMap)
items = self.items()
self._scene.removeItem(items[0])
del(items[0])
self._scene.addItem(newMapItem)
h = self.mapToScene(self.viewport().rect()).boundingRect().height()
r = self.sceneRect()
r.setHeight(h)
self.setSceneRect(r)
height = self.viewport().height()
width = self.viewport().width()
item_height = newMapItem.boundingRect().height()
item_width = newMapItem.boundingRect().width()
tr = QTransform()
tr.scale(width / item_width, height / item_height)
newMapItem.setTransform(tr)
def __create_region__(self):
pixmaps = [item.pixmap() for item in self.items()]
ipm = 0
image = pixmaps[ipm].toImage()
b = image.bits()
b.setsize(self.h * self.w * 4)
arr = np.frombuffer(b, np.uint8).reshape((self.w, self.h, 4))
arr = np.sum(arr, axis = 2)
arr[arr>0]=1
self.area = arr
def resizeEvent(self, event):
self.__resize__()
super(AreaSelect, self).resizeEvent(event)
def _get_point(self, evt):
point = np.array([evt.pos().x(),evt.pos().y()])*np.array(self.win_scale)
if point[1] > self.height - 1:
point[1] = self.height - 1
if point[1] < 0:
point[1] = 0
if point[0] > self.width - 1:
point[0] = self.width - 1
if point[0] < 0:
point[0] = 0
return point
def mouseDoubleClickEvent(self, evt):
if evt.button() == Qt.LeftButton and self.draggin_idx == -1:
point = self._get_point(evt)
dist = self.points - point
dist = np.sqrt(dist[:,0]**2 + dist[:,1]**2)
if dist.min() > self.DELTA:
poly = Polygon(self.points)
gpoint = Point(point[0], point[1])
pol_ext = LinearRing(poly.exterior.coords)
d = pol_ext.project(gpoint)
p = pol_ext.interpolate(d)
closest_point_coords = list(p.coords)[0]
gpoint = Point(closest_point_coords)
n = len(self.points)
p = [(k, k+1) for k in np.arange(n-1)]
p.append((n-1,0))
k = np.argmin([LineString([self.points[line[0]], self.points[line[1]]]).distance(gpoint) for line in p])
coords = p[k]
if max(coords) == n-1 and min(coords) == 0:
insert = 0
else:
insert = max(coords)
self.points.insert(insert, point)
self.__update_plotmap__()
self.__create_region__()
elif dist.min() <= self.DELTA and len(self.points)>3:
self.points.pop(dist.argmin())
self.__update_plotmap__()
self.__create_region__()
def mousePressEvent(self, evt):
if evt.button() == Qt.LeftButton and self.draggin_idx == -1:
point = self._get_point(evt)
dist = self.points - point
dist = np.sqrt(dist[:,0]**2 + dist[:,1]**2)
dist[dist>self.DELTA] = np.inf
if dist.min() < np.inf:
self.draggin_idx = dist.argmin()
def mouseMoveEvent(self, evt):
if self.draggin_idx != -1:
point = self._get_point(evt)
self.points[self.draggin_idx] = point
self.__update_plotmap__()
def mouseReleaseEvent(self, evt):
if evt.button() == Qt.LeftButton and self.draggin_idx != -1:
point = self._get_point(evt)
self.points[self.draggin_idx] = point
self.draggin_idx = -1
self.__update_plotmap__()
self.__create_region__()
def closeEvent(self,evt):
QApplication.quit()
def getArea(image, points = None, delta = 10):
app = QApplication(sys.argv)
ex = AreaSelect(image = image, points = points)
ex.DELTA = delta
app.exec_()
my_p=ex.points
my_a=ex.area
return my_p, my_a
#%% Example 1: select Area on example image
if __name__ == '__main__':
import NanoImagingPack as nip
my_im = nip.readim('lena')[:,50:]
my_p, my_a = getArea(my_im)
#%% Example 2: use previously obtained points
if __name__ == '__main__':
my_p, my_a = getArea(my_im, my_p)