Skip to content

Instantly share code, notes, and snippets.

@mcarletti
Last active February 8, 2024 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcarletti/76d132dd955050962ef5ae71eb098692 to your computer and use it in GitHub Desktop.
Save mcarletti/76d132dd955050962ef5ae71eb098692 to your computer and use it in GitHub Desktop.
Set of Qt / OpenCV snippets
/*
* 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