Last active
February 8, 2024 15:43
-
-
Save mcarletti/76d132dd955050962ef5ae71eb098692 to your computer and use it in GitHub Desktop.
Set of Qt / OpenCV snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Convert a cv::Mat frame to a QPixmap and show it in a Qt object. | |
* | |
* Parameters | |
* ---------- | |
* frame : cv::Mat | |
* Input image in BGR format. | |
*/ | |
void updateImageFrameToQLabel(cv::Mat& frame) | |
{ | |
// prepare frame data | |
cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB); | |
// convert to pixmap | |
QImage image = QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); | |
QPixmap pixmap = QPixmap::fromImage(image); | |
// update label content | |
ui->image_frame_label->setPixmap(pixmap); // no resize | |
} | |
void updateImageFrameToQGraphicsView(cv::Mat& frame) | |
{ | |
// prepare frame data | |
cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB); | |
// convert to pixmap | |
QImage image = QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); | |
QPixmap pixmap = QPixmap::fromImage(image); | |
// update graphics view content | |
// parent : QWidget* | |
// m_p_scene = new QGraphicsScene(parent) | |
// m_p_scene_pixmap = m_p_scene->addPixmap(QPixmap()); | |
// ui->graphicsView->setScene(m_p_scene); | |
m_p_scene_pixmap->setPixmap(pixmap); | |
m_p_scene->setSceneRect(0, 0, pixmap.width(), pixmap.height()); | |
ui->graphicsView->fitInView(m_p_scene->sceneRect(), Qt::KeepAspectRatio); // auto-resize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment