You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Qt+OpenCV的图像全景拼接程序强制终止问题求助

全景拼接模块程序崩溃问题排查求助

软件环境

  • QT 5.12.10
  • OPENCV 4.2.0
  • CMAKE 3.17.1

问题现象

开发的图像处理系统中,全景拼接模块出现程序强制终止问题,报错截图如下:
报错截图

问题代码详情

void MainWindow::on_pushButton_2_clicked()
{
    srcDirPath = QFileDialog::getExistingDirectory(this, tr("Choose folder"), "/", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    qDebug() << srcDirPath;
    //FileInfo
    QFileInfo OpenFileInfo;
    OpenFileInfo = QFileInfo(srcDirPath);
    OpenFilePath = OpenFileInfo.filePath();
    ui->lineEdit->setText(OpenFilePath);
}

void MainWindow::on_btn_imgstit_clicked()
{
    // read images from folder
    vector<Mat> images;
    QDir dir(srcDirPath);
    QStringList filters;
    filters << "*.jpg" << "*.png" << "*.bmp";
    QFileInfoList fileList = dir.entryInfoList(filters, QDir::Files|QDir::NoDotAndDotDot);
    foreach(QFileInfo fileInfo, fileList) {
          Mat img = imread(fileInfo.absoluteFilePath().toStdString());
          images.push_back(img);
      }
    // stitch 
    Ptr<Stitcher> stitcher = nullptr;
    stitcher = Stitcher::create();
    cout << " stitcher successfully" << endl;

    Mat result;
    Stitcher::Status status = stitcher->stitch(images,result);

    if (status != Stitcher::OK)
    {
        QMessageBox::warning(nullptr,"提示", "Can't stitch images!",QMessageBox::Yes |  QMessageBox::Yes);
        cout << "Can't stitch images, error code = " << int(status) << endl;
    }
    waitKey();

    QString resultPath = srcDirPath + "/result.jpg";
    imwrite(resultPath.toStdString(), result);
    QImage img = cvMat2QImage(result);
    QImage Image=ImageCenter(img,ui->label_stitcher);
    ui->label_stitcher->setPixmap(QPixmap::fromImage(Image));
    ui->label_stitcher->setAlignment(Qt::AlignCenter);
    statusBar()->showMessage(tr("stitching successfully"));

    for(unsigned int i = 0; i < images.size(); ++i)
    {
        images[i].release();
    }
    cout << "images[i] release successfully " << endl;

    // clear images
    images.clear();
    cout << "images clear successfully " << endl;

    // release stitcher
    stitcher.release();
    stitcher = nullptr;
    cout << "stitcher release successfully " << endl;
}

程序功能

  • 用户点击pushButton_2选择目标文件夹
  • 点击btn_imgstit将文件夹内所有jpg/png/bmp格式图像拼接为全景图,保存至原文件夹并在界面显示

调试情况

断点调试确认错误发生在Stitcher::Status status = stitcher->stitch(images,result);行,Debug模式下报错截图:
Debug报错截图1
Debug报错截图2

项目配置文件(.pro)

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = picture 
TEMPLATE = app 

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += mainwindow.ui

LIBS += E:\Qt\Qt5.12.10\5.12.10\mingw73_64\lib\libopencv_*.a
INCLUDEPATH += E:\Qt\Qt5.12.10\5.12.10\mingw73_64\include\OpenCV

RESOURCES += \
    myimage.qrc

RC_ICONS = picture.ico

TRANSLATIONS += en_tr.ts zh_tr.ts

错误分析与解决方案

可能的错误原因

  1. 图像读取异常:Qt字符串转std::string时,中文路径可能出现编码问题,导致imread读取失败得到空Mat;或存在损坏的图像文件,空Mat会触发Stitcher内部崩溃。
  2. 环境兼容性问题:OpenCV静态库的编译MinGW版本与项目使用的MinGW 7.3.0不匹配,导致Stitcher初始化后调用出现指针异常。
  3. 有效图像数量不足:全景拼接至少需要2张有效图像,若文件夹内图像数量不够或全部读取失败,会触发未定义行为。
  4. 内存溢出:高分辨率图像拼接时内存占用过大,导致程序强制终止。

针对性解决方案

  1. 修复图像读取逻辑:
    • 解决中文路径编码问题,添加图像有效性校验:
      foreach(QFileInfo fileInfo, fileList) {
          string imgPath = fileInfo.absoluteFilePath().toLocal8Bit().constData();
          Mat img = imread(imgPath);
          if (!img.empty()) {
              images.push_back(img);
          } else {
              qDebug() << "读取失败:" << fileInfo.absoluteFilePath();
          }
      }
      // 校验图像数量
      if (images.size() < 2) {
          QMessageBox::warning(nullptr,"提示", "至少需要2张有效图像!",QMessageBox::Ok);
          return;
      }
      
  2. 匹配OpenCV编译环境:
    • 确认OpenCV静态库是用MinGW 7.3.0编译的,若使用预编译包,选择对应版本的资源。
  3. 完善错误分支处理:
    • 拼接失败后直接返回,避免后续访问空的result Mat:
      Stitcher::Status status = stitcher->stitch(images,result);
      if (status != Stitcher::OK)
      {
          QMessageBox::warning(nullptr,"提示", "拼接失败!",QMessageBox::Ok);
          cout << "错误码 = " << int(status) << endl;
          // 提前释放资源
          for(auto& img : images) img.release();
          images.clear();
          stitcher.release();
          return;
      }
      
  4. 优化内存占用:
    • 对高分辨率图像进行缩放后再拼接:
      Mat scaledImg;
      resize(img, scaledImg, Size(img.cols/2, img.rows/2));
      images.push_back(scaledImg);
      
  5. 修正.pro文件库链接:
    • 明确指定需要的OpenCV库,避免通配符链接冲突:
      LIBS += -LE:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib \
              -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_stitching -lopencv_imgcodecs
      

内容的提问来源于stack exchange,提问作者ho ko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:00:45