读取halcon中DXF文件并创建模板

halcon中的DXF文件

在HDevelop的帮助窗口里,选择"算子"标签页,输入dxf,一共出现四个算子:

  • read_contour_xld_dxf:Read XLD contours from a DXF file.
  • read_polygon_xld_dxf:Read XLD polygons from a DXF file.
  • write_contour_xld_dxf:Write XLD contours to a file in DXF format.
  • write_polygon_xld_dxf:Write XLD polygons to a file in DXF format.

XLDs(eXtended Line Descriptions,扩展线描述)是HALCON中特定的图标对象,用于表示亚像素精度的轮廓和多边形(Hcont, Hpoly)。

从上述描述可知,XLD主要包含轮廓和多边形两种类型,四个算子的功能分别为从DXF文件读取XLD对象 、将XLD对象写入DXF文件 。

在DeepSeek 中输入C++读取DXF文件并解析得到如下代码:

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

// 定义一个结构体来存储DXF文件中的实体
struct DxfEntity {
    std::string type;
    std::map<int, std::string> properties;
};

// 读取DXF文件并解析
std::vector<DxfEntity> readDxfFile(const std::string& filePath) {
    std::ifstream file(filePath);
    std::vector<DxfEntity> entities;
    DxfEntity currentEntity;
    bool inEntity = false;

    if (!file.is_open()) {
        std::cerr << "无法打开文件: " << filePath << std::endl;
        return entities;
    }

    std::string line;
    while (std::getline(file, line)) {
        // 去除行首尾的空白字符
        line.erase(0, line.find_first_not_of(" \t\r\n"));
        line.erase(line.find_last_not_of(" \t\r\n") + 1);

        if (line.empty()) continue;

        // 解析组码和值
        int groupCode = std::stoi(line);
        std::string value;
        if (std::getline(file, value)) {
            value.erase(0, value.find_first_not_of(" \t\r\n"));
            value.erase(value.find_last_not_of(" \t\r\n") + 1);

            if (groupCode == 0) {
                // 新的实体开始
                if (inEntity) {
                    entities.push_back(currentEntity);
                    currentEntity = DxfEntity();
                }
                currentEntity.type = value;
                inEntity = true;
            } else {
                // 添加属性到当前实体
                currentEntity.properties[groupCode] = value;
            }
        }
    }

    // 添加最后一个实体
    if (inEntity) {
        entities.push_back(currentEntity);
    }

    file.close();
    return entities;
}

// 打印解析的实体
void printEntities(const std::vector<DxfEntity>& entities) {
    for (const auto& entity : entities) {
        std::cout << "实体类型: " << entity.type << std::endl;
        for (const auto& prop : entity.properties) {
            std::cout << "  组码: " << prop.first << ", 值: " << prop.second << std::endl;
        }
        std::cout << std::endl;
    }
}

int main() {
    std::string filePath = "example.dxf";
    std::vector<DxfEntity> entities = readDxfFile(filePath);
    printEntities(entities);
    return 0;
}

利用上述代码,分别读取由write_contour_xld_dxf 和write_polygon_xld_dxf 保存的DXF文件,输出结果如下:

不难看出,POLYLINE 标示每一个XLD对象的开始,其后的VERTEX 即为XLD中的顶点,10,20,30分别对应X,Y,Z 坐标,1040对应edge_direction ,对于轮廓可以提取到(X,Y,edge_direction),对于多边形可以提取到(X,Y),基于这些信息,即可创建模板。

使用DXF文件创建模板

mwwz 图像库支持从轮廓或者多边形创建模板,读取halcon的DXF文件后,解析出位置、方向等信息,即可创建适用于mwwz 的模板。此项功能已加入测试软件。需要注意的是,轮廓本身自带方向,而多边形仅包含顶点位置信息,其方向需要额外指定。在测试软件中加载模板时,多边形模板+ 表示方向在轮廓的右侧,多边形模板- 表示方向在轮廓的左侧,此外形状模板(*.shm) 与halcon并不兼容。

测试软件下载地址

相关推荐
硅谷秋水16 小时前
MorphMimic 方法分析
人工智能·机器学习·计算机视觉·机器人
jay神16 小时前
基于 YOLOv8 的传送带异物检测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机视觉·计算机毕业设计
Rattenking3 天前
OpenCV 轨迹栏操作和键盘响应操作
人工智能·opencv·计算机外设
jay神3 天前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
wangxiaoxuan2174 天前
TF2 的原理与使用:把坐标、运动和观测时间一起算清楚
计算机视觉·机器人·科沃斯八界
硅谷秋水4 天前
EgoHumanoid‑V2 方法分析
人工智能·机器学习·计算机视觉·语言模型·机器人
BYSJMG4 天前
计算机毕业设计选题推荐:【基于深度学习的车辆损坏严重程度评估方法研究】ResNet50 图像分类
人工智能·深度学习·目标检测·机器学习·计算机视觉·分类·课程设计
BYSJMG4 天前
计算机毕业设计选题推荐|【基于深度学习的电梯电动车违规进入检测研究】YOLO26m+可视化大屏
人工智能·深度学习·神经网络·目标检测·机器学习·计算机视觉·课程设计
硅谷秋水5 天前
将闲科技(LiberAI)提出世界动作模型Liber-0
机器学习·计算机视觉·语言模型·机器人
ahccqw5 天前
OpenCV 车辆数量统计
人工智能·opencv·计算机视觉