-
Notifications
You must be signed in to change notification settings - Fork 1
/
readImage_nosampling.py
executable file
·213 lines (168 loc) · 7.48 KB
/
readImage_nosampling.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
#=================================================================
# 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 no 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
#------------------------------------------------------------------
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':
# deal with zoom in
scale_factor = 1 / self.scale_zoom
elif event.button == 'down':
# deal with zoom out
scale_factor = self.scale_zoom
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.ax0.set_xlim(self.cur_xlim)
self.ax0.set_ylim(self.cur_ylim)
self.canvas.draw()
#------------------------------------------------------------------
def openCall(self):
self.openFileNameDialog()
#------------------------------------------------------------------
def readImage(self):
self.status_bar.showMessage('Reading file')
qApp.processEvents() # Flush events
print('Starting read')
self.image = cv2.imread(self.imageFileName, cv2.IMREAD_GRAYSCALE)
print("Orginal: ", self.image.shape)
print("width: ", self.image.shape[1])
print("height: ", self.image.shape[0])
self.status_bar.clearMessage()
#------------------------------------------------------------------
def displayImage(self):
self.ax0.set_adjustable('datalim')
self.ax0.clear()
self.ax0.set_visible(True)
self.alphaLevel = 1
self.betaLevel = 0
self.adjusted = cv2.convertScaleAbs(self.image, alpha=self.alphaLevel, beta=self.betaLevel)
self.image_object = self.ax0.imshow(self.adjusted, cmap='gray', vmin=0, vmax=255,
aspect='equal')
self.ax0.set_xlim(self.cur_xlim)
self.ax0.set_ylim(self.cur_ylim)
self.canvas.draw()
#------------------------------------------------------------------
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_() )