设置首页收藏本站
开启左侧

有没有大哥知道QT引入halcon这个问题要如何处理“terminate called after throwi

[复制链接]
index_xue 发表于 2025-3-25 16:12:00 | 显示全部楼层 |阅读模式
有没有大哥知道QT引入halcon这个问题要如何处理“terminate called after throwing an instance of 'HalconCpp::HOperatorException'”
我使用的版本是这个HALCON-24.11-Progress-Steady
b3837ebd-57b6-41f3-8eef-f1cb88a062ff.png
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
Criss 发表于 2025-3-25 16:39:16 | 显示全部楼层
报错了,用try...catch捕获处理下,你这个是直接抛出异常了。
  1. try
  2. {
  3.     //HALCON代码
  4. }
  5. catch (HException &ex)
  6. {
  7.     qDebug() << ex.ErrorMessage().Text();
  8. }
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| index_xue 发表于 2025-3-25 17:53:19 | 显示全部楼层
  1. #include "widget.h"
  2. #include <QVBoxLayout>
  3. #include <QImage>
  4. #include <QPixmap>
  5. #include <QDebug>
  6. #include <QFile>

  7. Widget::Widget(QWidget *parent) : QWidget(parent) {
  8.     // 创建布局和标签
  9.     QVBoxLayout *layout = new QVBoxLayout(this);
  10.     imageLabel = new QLabel(this);
  11.     layout->addWidget(imageLabel);

  12.     // 加载并显示图像
  13.     loadAndDisplayImage("/home/xue/HalconQt/halcon_qt/logo.bmp");
  14. }

  15. Widget::~Widget() {
  16.     // 资源清理(如果需要)
  17. }

  18. void Widget::loadAndDisplayImage(const QString &imagePath) {
  19.     // 检查图像文件是否存在
  20.     if (!QFile::exists(imagePath)) {
  21.         qDebug() << "Image file does not exist:" << imagePath;
  22.         return;
  23.     }

  24.     HObject ho_Image;
  25.     try {
  26.         ReadImage(&ho_Image, imagePath.toStdString().c_str());
  27.         qDebug() << "Image loaded successfully.";
  28.     } catch (HOperatorException &ex) {
  29.         qDebug() << "Error reading image:" << ex.ErrorMessage();
  30.         return;
  31.     }

  32.     // 检查图像是否有效
  33.     HTuple hv_Num;
  34.     CountObj(ho_Image, &hv_Num);
  35.     if (hv_Num == 0) {
  36.         qDebug() << "Loaded image is empty.";
  37.         return;
  38.     }

  39.     // 处理图像
  40.     HObject ho_ProcessedImage;
  41.     try {
  42.         processImage(ho_Image, ho_ProcessedImage);
  43.     } catch (HOperatorException &ex) {
  44.         qDebug() << "Error processing image:" << ex.ErrorMessage();
  45.         return; // 处理失败,返回
  46.     }

  47.     // 检查处理后图像是否有效
  48.     if (!ho_ProcessedImage.IsInitialized()) {
  49.         qDebug() << "Processed image is empty, skipping display.";
  50.         return; // 或者可以选择显示原始图像
  51.     }

  52.     // 获取处理后图像的尺寸
  53.     HTuple hv_Width, hv_Height;
  54.     GetImageSize(ho_ProcessedImage, &hv_Width, &hv_Height);

  55.     // 创建QImage
  56.     QImage qImage(hv_Width[0].I(), hv_Height[0].I(), QImage::Format_RGB888);
  57.     for (int y = 0; y < hv_Height[0].I(); y++) {
  58.         for (int x = 0; x < hv_Width[0].I(); x++) {
  59.             HTuple hv_Pixel;
  60.             GetGrayval(ho_ProcessedImage, x, y, &hv_Pixel);
  61.             qImage.setPixel(x, y, qRgb(hv_Pixel[0].I(), hv_Pixel[0].I(), hv_Pixel[0].I()));
  62.         }
  63.     }

  64.     // 显示图像
  65.     imageLabel->setPixmap(QPixmap::fromImage(qImage));
  66. }

  67. void Widget::processImage(HObject &image, HObject &processedImage) {
  68.     // 获取图像尺寸
  69.     HTuple hv_Width, hv_Height;
  70.     GetImageSize(image, &hv_Width, &hv_Height);

  71.     // 输出图像尺寸
  72.     qDebug() << "Processing image of size:" << hv_Width[0].I() << "x" << hv_Height[0].I();

  73.     // 使用边缘检测
  74.     try {
  75.         EdgesSubPix(image, &processedImage, "canny", 3, 20, 40);
  76.     } catch (HOperatorException &ex) {
  77.         qDebug() << "Error in edge detection:" << ex.ErrorMessage();
  78.         // 处理异常后返回一个默认图像或空图像
  79.         processedImage = HObject(); // 或者设置为某个默认图像
  80.         return;
  81.     }
  82. }
复制代码


我写了一个这个,然后输出结果仍然是这样的
  1. Processing image of size: 640 x 640
  2. terminate called after throwing an instance of 'HalconCpp::HOperatorException'
复制代码


奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| index_xue 发表于 2025-3-25 17:56:28 | 显示全部楼层
我想会不会和我的这个pro的配置有关系呀
  1. # HALCON 路径
  2. HALCON_ROOT = /opt/MVTec/HALCON-24.11-Progress-Steady

  3. # 添加 HALCON 的头文件路径
  4. INCLUDEPATH += $$HALCON_ROOT/include
  5. INCLUDEPATH += $$HALCON_ROOT/include/halconcpp

  6. # 添加 HALCON 的库文件路径
  7. LIBS += -L$$HALCON_ROOT/lib/x64-linux

  8. # 链接 HALCON 核心库和 C++ 接口库
  9. LIBS += -lhalcon
  10. LIBS += -lhalconcpp
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表