优秀的编程知识分享平台

网站首页 > 技术文章 正文

Qt上使用OpenCV显示图片(qt多窗口opengl绘图)

nanyue 2024-08-11 20:42:42 技术文章 8 ℃


在QT中使用OpenCV显示图片,主要需要以下步骤:

  1. 配置OpenCV库:在QT项目中添加OpenCV库,并设置相关连接、包含路径。
  2. 创建QT窗口并添加QLabel控件:用于显示OpenCV图像。
  3. 读取OpenCV图像:使用cv::imread函数读取指定路径下的图像文件。
  4. 将OpenCV图像转换为QImage格式:使用cv::Mat::data和QImage::fromData函数将OpenCV图像数据转换为QImage格式。
  5. 将QImage显示在QLabel控件中:使用QPixmap::fromImage函数将QImage转换为QPixmap格式,然后使用QLabel::setPixmap函数将QPixmap显示在QLabel控件中。

以下是一个简单的示例代码:

#include <opencv2/opencv.hpp>
#include <QApplication>
#include <QLabel>

  //【领C++学习资料,进程序员交流君羊:580475042 莬废领取,先码住不迷路~】

  int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 创建QT窗口并添加QLabel控件
    QWidget window;
    QLabel label(&window);
    window.setCentralWidget(&label);
    window.show();

    // 读取OpenCV图像
    cv::Mat image = cv::imread("path/to/image.jpg");

    // 将OpenCV图像转换为QImage格式
    QImage qimage(image.data, image.cols, image.rows, QImage::Format_RGB888);

    // 将QImage显示在QLabel控件中
    QPixmap pixmap = QPixmap::fromImage(qimage);
    label.setPixmap(pixmap);

    return a.exec();
}

Tags:

最近发表
标签列表