-
Notifications
You must be signed in to change notification settings - Fork 1
/
readImage_sampling.py
executable file
·253 lines (204 loc) · 9.57 KB
/
readImage_sampling.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
#=================================================================
# Author: Patrick Brockmann CEA/DRF/LSCE - May 2022
#=================================================================
import sys, os
from PyQt5.Qt import Qt
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib.transforms import Bbox
from matplotlib.collections import LineCollection
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import cv2
#======================================================
class MainWindow(QMainWindow):
#------------------------------------------------------------------
def __init__(self):
QMainWindow.__init__(self)
#-------------------------------
self.setMinimumSize(QSize(1400, 900))
self.setWindowTitle("StripesCounter read sampling")
# Create new action
openAction = QAction(QIcon('open.png'), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open document')
openAction.triggered.connect(self.openCall)
# Create exit action
exitAction = QAction(QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.exitCall)
# Create menu bar and add action
self.menuBar = self.menuBar()
fileMenu = self.menuBar.addMenu('&File')
fileMenu.addAction(openAction)
fileMenu.addAction(exitAction)
#-----------------------
self.fig = plt.figure(figsize=(8,8))
self.ax0 = self.fig.add_axes([0.08, 0.05, 0.87, 0.90]) # [left, bottom, width, height]
self.canvas = FigureCanvas(self.fig)
# https://github.com/matplotlib/matplotlib/issues/707/
self.canvas.setFocusPolicy(Qt.ClickFocus)
self.canvas.setFocus()
self.canvas.mpl_connect('button_press_event', self.on_press)
self.canvas.mpl_connect('button_release_event', self.on_release)
self.canvas.mpl_connect('motion_notify_event', self.on_motion)
self.canvas.mpl_connect('scroll_event', self.zoom)
#-----------------------
layoutH = QHBoxLayout()
layoutV1 = QVBoxLayout()
layoutV1.addWidget(self.canvas)
layoutH.addLayout(layoutV1)
# Create a placeholder widget to hold our toolbar and canvas.
widget = QWidget()
widget.setLayout(layoutH)
self.setCentralWidget(widget)
# Status bar
self.status_bar = self.statusBar()
self.status_bar.showMessage('Ready', 5000)
self.ax0.set_visible(False)
#-----------------------
self.press = False
self.xpress = 0
self.ypress = 0
self.cur_xlim = None
self.cur_ylim = None
self.scale_zoom = 1.2
self.imageDisplayedWidthLimit = 2000
self.subSampling = 1
self.imageDisplayedWidth = 0
#------------------------------------------------------------------
def zoom(self, event):
try:
# https://stackoverflow.com/questions/11551049/matplotlib-plot-zooming-with-scroll-wheel
cur_xlim = event.inaxes.get_xlim()
cur_ylim = event.inaxes.get_ylim()
xdata = event.xdata # get event x location
ydata = event.ydata # get event y location
if event.button == 'up': # zoom in
scale_factor = 1. / self.scale_zoom
print('--- up')
if (self.imageDisplayedWidth < self.imageDisplayedWidthLimit):
if (self.subSampling > 1) :
self.subSampling -= 1
self.sampleImage()
elif event.button == 'down': # zoom out
scale_factor = self.scale_zoom
print('--- down')
if (self.imageDisplayedWidth > self.imageDisplayedWidthLimit):
self.subSampling += 1
self.sampleImage()
new_width = (cur_xlim[1] - cur_xlim[0]) * scale_factor
new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
relx = (cur_xlim[1] - xdata)/(cur_xlim[1] - cur_xlim[0])
rely = (cur_ylim[1] - ydata)/(cur_ylim[1] - cur_ylim[0])
self.cur_xlim = [xdata - new_width * (1-relx), xdata + new_width * (relx)]
self.cur_ylim = [ydata - new_height * (1-rely), ydata + new_height * (rely)]
self.displayImage()
except:
return
#------------------------------------------------------------------
def on_release(self, event):
#----------------------------------------------
self.press = False
self.canvas.draw()
#------------------------------------------------------------------
def on_press(self, event):
#----------------------------------------------
self.press = True
if event.inaxes == self.ax0:
self.cur_xlim = self.ax0.get_xlim()
self.cur_ylim = self.ax0.get_ylim()
self.xpress = event.xdata
self.ypress = event.ydata
#------------------------------------------------------------------
def on_motion(self, event):
#----------------------------------------------
if not self.press: return
if event.xdata == None or event.ydata == None: return
#----------------------------------------------
if event.inaxes == self.ax0:
dx = event.xdata - self.xpress
dy = event.ydata - self.ypress
self.cur_xlim -= dx
self.cur_ylim -= dy
self.displayImage()
#------------------------------------------------------------------
def openCall(self):
self.openFileNameDialog()
#------------------------------------------------------------------
def readImage(self):
self.status_bar.showMessage('Reading file')
qApp.processEvents() # Flush events
self.ax0.set_visible(True)
print('Reading start')
self.image = cv2.imread(self.imageFileName, cv2.IMREAD_GRAYSCALE)
print('Readind end')
print("Shape: ", self.image.shape)
#print("Width: ", self.image.shape[1])
#print("Height: ", self.image.shape[0])
self.subSampling = np.ceil(np.max(self.image.shape) / self.imageDisplayedWidthLimit).astype(int)
self.cur_xlim = [0, self.image.shape[1]]
self.cur_ylim = [self.image.shape[0], 0]
self.sampleImage()
self.status_bar.clearMessage()
#------------------------------------------------------------------
def sampleImage(self):
print("Sampling: ", self.subSampling)
if self.subSampling == 1:
self.imageResized = self.image
else:
self.imageResized = cv2.resize(self.image, None, fx=1./self.subSampling, fy=1./self.subSampling, interpolation = cv2.INTER_NEAREST)
print("Resized: ", self.imageResized.shape)
#------------------------------------------------------------------
def adjustImage(self):
self.alphaLevel = self.mySliderAlpha.value()/10.
self.betaLevel = self.mySliderBeta.value()
#self.imageAdjusted = cv2.convertScaleAbs(self.image, alpha=self.alphaLevel, beta=self.betaLevel)
self.imageResizedAdjusted = cv2.convertScaleAbs(self.imageResized, alpha=self.alphaLevel, beta=self.betaLevel)
#------------------------------------------------------------------
def displayImage(self):
ex1 = np.maximum(0, np.min(self.cur_xlim))
ex2 = np.minimum(self.image.shape[1], np.max(self.cur_xlim))
ey1 = np.maximum(0, np.min(self.cur_ylim))
ey2 = np.minimum(self.image.shape[0], np.max(self.cur_ylim))
x1 = int(ex1 / self.subSampling)
x2 = int(ex2 / self.subSampling)
y1 = int(ey1 / self.subSampling)
y2 = int(ey2 / self.subSampling)
self.ax0.clear()
self.ax0.set_adjustable('datalim')
self.image_object = self.ax0.imshow(self.imageResized[y1:y2, x1:x2], cmap='gray', vmin=0, vmax=255,
aspect='equal', extent=[ex1,ex2,ey2,ey1])
self.ax0.set_xlim(self.cur_xlim)
self.ax0.set_ylim(self.cur_ylim)
self.ax0.text(0.75, 0.99, 'Sampling: 1:%d'%self.subSampling, horizontalalignment='left', verticalalignment='top',
transform=self.fig.transFigure)
self.canvas.draw()
self.imageDisplayedWidth = (x2 - x1)
print("Image displayed width: ", self.imageDisplayedWidth)
#------------------------------------------------------------------
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
self.imageFileName, _ = QFileDialog.getOpenFileName(self, "Load image",
"","PNG, JPG, TIF Files (*.png *.jpg *.tif);;PNG Files (*.png);;JPG Files (*.jpg);;TIF Files (*.tif);;All Files (*)", options=options)
if self.imageFileName:
self.readImage()
self.displayImage()
#------------------------------------------------------------------
def exitCall(self):
self.close()
#======================================================
if __name__ == "__main__":
qApp = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( qApp.exec_() )