在下面的代码中,当我使用函数get_core_data加载GridscanCanvas的pixmap时,它可以正常工作,没有崩溃,然后图像显示在我的应用程序中。
然而,当我想在函数get_square_data中更新相同的像素图时,它每次都会崩溃。
两次,原始图像都是np.array,它们之间的唯一区别是数组的大小。我已经评论了它崩溃的代码。
我很失落,因为两个功能几乎相同。任何帮助将不胜感激!
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os
import numpy as np
from mdoc_class import Gridscan_mdoc
from mrc_class import Gridscan_mrc
from em_ss_find_gridsquares import find_gridsquares
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setGeometry(100,100,1000,1000)
self.main_widget = QWidget(self)
self.setCentralWidget(self.main_widget)
self.gridscan_canvas = GridscanCanvas(self)
self.init_menu()
self.pixel_size = 92.7
self.gridsquare_length = 85000
self.spacing_length = 40000
self.show()
def init_menu(self):
bar = self.menuBar()
self.file = bar.addMenu('File')
load_mdoc_action = QAction('Load mdoc', self)
self.file.addAction(load_mdoc_action)
self.file.triggered.connect(self.file_respond)
find_squares_action = QAction('Find squares', self)
self.file.addAction(find_squares_action)
def file_respond(self, q):
signal = q.text()
if signal == 'Load mdoc':
mdoc_file = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('Home'),
"MDOC(*.mdoc);;AllFiles(*.*)")[0]
if mdoc_file:
try:
self.load_core_data = LoadCoreData(mdoc_file)
except Exception as e:
print(e)
return
try:
self.load_core_data.new_output.connect(self.get_core_data)
except Exception as e:
print(e)
return
try:
self.load_core_data.start()
except Exception as e:
print(e)
return
elif signal == 'Find squares':
try:
self.find_squares = FindGridsquares(self.mrc.gridscan, self.pixel_size, self.gridsquare_length,
self.spacing_length)
except Exception as e:
print(e)
return
try:
self.find_squares.new_output.connect(self.get_square_data)
except Exception as e:
print(e)
return
try:
self.find_squares.start()
except Exception as e:
print(e)
return
else:
return
def get_core_data(self, core_data):
self.mrc = core_data['mrc']
self.mdoc = core_data['mdoc']
# This here works fine
self.gridscan_canvas.pixmap = QPixmap(QImage(self.mrc.gridscan, self.mrc.gridscan.shape[1],
self.mrc.gridscan.shape[0], QImage.Format_Grayscale8))
self.gridscan_canvas.update()
def get_square_data(self, square_data):
self.centres = square_data['centres']
self.aligned_centres = square_data['aligned_centres']
self.aligned_gridscan = square_data['aligned_gridscan']
self.rot_angle = square_data['rot_angle']
self.sqrd_centres = square_data['sqrd_centres']
# This here crashes is where it crashes
self.gridscan_canvas.pixmap = QPixmap(QImage(self.aligned_gridscan, self.aligned_gridscan.shape[1],
self.aligned_gridscan.shape[0], QImage.Format_Grayscale8))
self.gridscan_canvas.update()
class GridscanCanvas(QWidget):
DELTA = 10
def __init__(self, *args, **kwargs):
super(GridscanCanvas, self).__init__(*args, **kwargs)
self.draggin_idx = -1
self.points_old = np.array([[v * 5, v * 5] for v in range(75)], dtype=np.float)
self.width = 700
self.height = 700
self.setFixedSize(self.width, self.height)
self.pixmap = None
self.points = None
self.scaling_factor = [1, 1]
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
if self.pixmap is not None:
self.pixmap = self.pixmap.scaled(self.width, self.height, transformMode=Qt.SmoothTransformation)
qp.drawPixmap(0, 0, self.width, self.height, self.pixmap)
else:
self.scaling_factor = [1, 1]
if self.points is not None:
self.drawPoints(qp)
qp.end()
def drawPoints(self, qp):
pen = QPen()
pen.setWidth(5)
pen.setColor(Qt.red)
qp.setPen(pen)
for x,y in self.points:
qp.drawPoint(x,y)
class LoadCoreData(QThread):
new_output = pyqtSignal(object)
def __init__(self, mdoc_filepath):
QThread.__init__(self)
self.isRunning = True
self.mdoc_filepath = mdoc_filepath
def __del__(self):
self.wait()
def run(self):
mdoc = Gridscan_mdoc(self.mdoc_filepath)
mrc = Gridscan_mrc(mdoc)
output = {'mdoc': mdoc, 'mrc': mrc}
self.new_output.emit(output)
class FindGridsquares(QThread):
new_output = pyqtSignal(object)
def __init__(self, gridscan, pixel_size, gridsquare_length, spacing_length):
QThread.__init__(self)
self.isRunning = True
self.gridscan = gridscan
self.pixel_size = pixel_size
self.gridsquare_length = gridsquare_length
self.spacing_length = spacing_length
def __del__(self):
self.wait()
def run(self):
centres, aligned_centres, aligned_gridscan, rot_angle, sqrd_centres =find_gridsquares(self.gridscan, self.pixel_size, self.gridsquare_length, self.spacing_length)
output = {'centres': centres, 'aligned_centres': aligned_centres, 'aligned_gridscan': aligned_gridscan, 'rot_angle': rot_angle, 'sqrd_centres': sqrd_centres}
self.new_output.emit(output)
app = QApplication([])
window = MainWindow()
app.exec_()