QPixmap 使用 Realsense 图像调整大小
QPixmap resizing with Realsense image
我试着感谢 Qlabel 让它自动调整大小,它包括一个 QPixmap,它显示一个在线程上工作的 Realsense 相机 vid_thread
。我有一个计时器,每 20 毫秒刷新一次图像,以避免负载过重。除了调整大小不是流畅的,它不会像界面的其他元素那样立即跟随鼠标的移动。
由于调整大小事件,我尝试不让它依赖于这个计时器,因为它更流畅,但仍然不是瞬时的
任何聪明的提示如何完成这个?
class UI_main(QMainWindow):
def __init__(self):
[...]
self.timer = QTimer()
self.timer.setInterval(20)
self.timer.timeout.connect(self.setImage)
self.timer.start()
[...]
def setImage(self):
self.camLabel.setP(self.server_thread.vid_thread.p)
[...]
def setupUi(self):
[...]
self.camLabel = camLabel(self.detectionContainer)
self.camLabel.setStyleSheet("border-color: rgb(112, 112, 112); border-width : 1px; border-radius:5px; border-style:inset;")
self.hCamContainer.addWidget(self.camLabel)
[...]
class camLabel(QLabel):
mouseSignal = pyqtSignal(tuple)
def __init__(self, parent=None):
super(camLabel, self).__init__(parent)
self.p = QPixmap()
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
self.setMinimumSize(640, 480)
self.setScaledContents(True)
def setP(self, p):
self.p = QPixmap.fromImage(p)
self.setPixmap(self.p.scaled(self.width(), self.height(),
Qt.KeepAspectRatio, Qt.FastTransformation))
def mousePressEvent(self, event):
self.x=int(event.x()*(640/self.width()))
self.y=int(event.y()*(480/self.height()))
print("point x: ", self.x, ", point y: ", self.y)
print("point x (ancien): ",event.x(), ", point y(ancien): ", event.y())
print("Width : ", self.width(), ", Height: ", self.height())
self.mouseSignal.emit((self.x,self.y))
def resizeEvent(self, event):
self.setPixmap(self.p.scaled(self.width(), self.height(),
Qt.KeepAspectRatio,Qt.FastTransformation))
这个解决方案可能会有所帮助。关键区别在于图像是在 paintEvent 上更新的,而不是在 resizeEvent 上更新的。代码是 C++ 而不是 Python,但我相信你明白了。
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
class ImageWidget : public QWidget
{
Q_OBJECT
public:
explicit ImageWidget(QWidget *parent = nullptr) : QWidget(parent) {}
void setImage(const QString &path) {
m_image.load(path);
}
protected:
void paintEvent(QPaintEvent *) override {
QPainter painter(this);
auto scaled = m_image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter.drawImage(0,0, scaled);
painter.end();
}
private:
QImage m_image;
};
#endif // IMAGEWIDGET_H
我试着感谢 Qlabel 让它自动调整大小,它包括一个 QPixmap,它显示一个在线程上工作的 Realsense 相机 vid_thread
。我有一个计时器,每 20 毫秒刷新一次图像,以避免负载过重。除了调整大小不是流畅的,它不会像界面的其他元素那样立即跟随鼠标的移动。
由于调整大小事件,我尝试不让它依赖于这个计时器,因为它更流畅,但仍然不是瞬时的
任何聪明的提示如何完成这个?
class UI_main(QMainWindow):
def __init__(self):
[...]
self.timer = QTimer()
self.timer.setInterval(20)
self.timer.timeout.connect(self.setImage)
self.timer.start()
[...]
def setImage(self):
self.camLabel.setP(self.server_thread.vid_thread.p)
[...]
def setupUi(self):
[...]
self.camLabel = camLabel(self.detectionContainer)
self.camLabel.setStyleSheet("border-color: rgb(112, 112, 112); border-width : 1px; border-radius:5px; border-style:inset;")
self.hCamContainer.addWidget(self.camLabel)
[...]
class camLabel(QLabel):
mouseSignal = pyqtSignal(tuple)
def __init__(self, parent=None):
super(camLabel, self).__init__(parent)
self.p = QPixmap()
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
self.setMinimumSize(640, 480)
self.setScaledContents(True)
def setP(self, p):
self.p = QPixmap.fromImage(p)
self.setPixmap(self.p.scaled(self.width(), self.height(),
Qt.KeepAspectRatio, Qt.FastTransformation))
def mousePressEvent(self, event):
self.x=int(event.x()*(640/self.width()))
self.y=int(event.y()*(480/self.height()))
print("point x: ", self.x, ", point y: ", self.y)
print("point x (ancien): ",event.x(), ", point y(ancien): ", event.y())
print("Width : ", self.width(), ", Height: ", self.height())
self.mouseSignal.emit((self.x,self.y))
def resizeEvent(self, event):
self.setPixmap(self.p.scaled(self.width(), self.height(),
Qt.KeepAspectRatio,Qt.FastTransformation))
这个解决方案可能会有所帮助。关键区别在于图像是在 paintEvent 上更新的,而不是在 resizeEvent 上更新的。代码是 C++ 而不是 Python,但我相信你明白了。
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
class ImageWidget : public QWidget
{
Q_OBJECT
public:
explicit ImageWidget(QWidget *parent = nullptr) : QWidget(parent) {}
void setImage(const QString &path) {
m_image.load(path);
}
protected:
void paintEvent(QPaintEvent *) override {
QPainter painter(this);
auto scaled = m_image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter.drawImage(0,0, scaled);
painter.end();
}
private:
QImage m_image;
};
#endif // IMAGEWIDGET_H