问题
I have a 2D numpy array of type np.float64, and I want to show it as an image in a QLabel (or any other valid way):
self.img = np.rot90(get_my_data()) # this line returns a 2D numpy array of type np.float64
self.qimg = QtGui.QImage(self.img, self.img.shape[0], self.img.shape[1], QtGui.QImage.Format_Grayscale8)
self.myLabel.setPixmap(QtGui.QPixmap(self.qimg))
My code above returning the following error:
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(bytes, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(bytes, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(List[str]): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, format: str = None): argument 1 has unexpected type 'numpy.ndarray'
QImage(QImage): argument 1 has unexpected type 'numpy.ndarray'
QImage(Any): too many arguments
But, if I add .copy()
at the end of the first line, then it works! but it doesn't display the data correctly.
self.img = np.rot90(get_my_data()).copy()
self.qimg = QtGui.QImage(self.img, self.img.shape[0], self.img.shape[1], QtGui.QImage.Format_Grayscale8)
self.myLabel.setPixmap(QtGui.QPixmap(self.qimg))
Here is what the label displays compared with pyplot.imshow()
:
self.img = 20 * np.log10(np.rot90(get_my_data()).copy())
self.qimg = QtGui.QImage(self.img, self.img.shape[0], self.img.shape[1], QtGui.QImage.Format_Grayscale8)
self.myLabel.setPixmap(QtGui.QPixmap(self.qimg))
pyplot.imshow(self.img)
pyplot.show()
The result of pyplot.imshow()
is:
While myLabel
displays the following result:
So, what is wrong with my code?
Is there a more elegant way to display my 2D numpy array as an image?
回答1:
From what I read the OP has an XY problem, that is, its objective is to show the output of imshow() in a Qt window, but ask about the attempt to display the data in a QImage.
The imshow() method does not show raw data but processes the information based on the parameters as indicated by the docs:
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=, filternorm=1, filterrad=4.0, imlim=, resample=None, url=None, *, data=None, **kwargs)
So if you want to obtain an image with that data you must implement that algorithm (you can check the source code of matplotlib or similar SW to analyze the logic)
If we focus on the real objective then the simplest solution is to use the Qt backend of matplotlib to obtain the appropriate canvas as shown below:
import numpy as np
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.figure = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.subplots()
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2
self.ax.imshow(Z)
self.ax.set_axis_off()
self.setCentralWidget(self.canvas)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Update:
If you want to display the data from time to time then you can use a QTimer that updates the information as I show below:
import random
import numpy as np
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.figure = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.subplots()
self.ax.set_axis_off()
self.setCentralWidget(self.canvas)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.on_timeout)
timer.start(100)
def on_timeout(self):
x0, y0 = random.uniform(-2, 2), random.uniform(-2, 2)
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - x0) ** 2) - (Y - y0) ** 2)
Z = (Z1 - Z2) * 2
self.ax.imshow(Z)
self.canvas.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
On the other hand, if you want to have a SW in real time then the GUI will limit that objective. It is advisable to show the data every N samples so that the GUI is not blocked and the user can view and analyze the information. The human eye is very slow, so even if the technology exists to display images every microsecond, our vision would not appreciate it, our vision requires 60ms to process the image, therefore the devices are designed to work at 30Hz since if the frequency were superior improvement would not be observed.
来源:https://stackoverflow.com/questions/60358003/show-matplotlib-imshow-output-in-qt