HVL 1.12 · C ABI

工业视觉算法库

HVL(Halcon-style Vision Library)以 C++17 实现、C ABI 导出, Region 采用行程编码(RLE),算子语义对齐 MVTec HALCON 的使用习惯,可供 C / C++ / C# / Python 调用。

01 // DESIGN

与 Halcon 对齐的关键设计

Region = RLE

行程 {row, col_begin, col_end},形态学 / connection / 集合运算在行程上完成,复杂度随区域规模增长。

threshold → region

分割直接输出 region,再 connection / select_shape / 形态学,而不是长期持有整图掩膜。

多语言

稳定 C ABI + CMake find_package(hvl);绑定:C++ RAII、C# P/Invoke、Python ctypes。

02 // PIPELINE

推荐检测流程

01

滤波

mean / gauss

02

阈值

threshold / domain

03

形态学

open / close

04

连通

connection 4/8

05

筛选·测量·匹配

select / measure / NCC·shape

03 // OPERATORS

算子模块(1.12)

模块代表算子说明
Segmentthreshold, dyn_threshold, threshold_domain图像→RLE region
I/Oread_png/write_png, read_pgm, image_read灰度 PGM/PNG(libpng)
Filtermean, gauss, median(OpenMP)平滑与椒盐抑制
Morphologyopen/close, fill_up, boundary, skeletonRLE 形态学 / 骨架
Blobconnection, select/sort_region, circles连通、排序、外接/内切圆
Grayintensity, min_max_gray, select_gray区域内灰度特征
EdgesCanny edges_image, edges_sub_pix, sobel_dirNMS + 亚像素边缘点
XLDedges_to_xld, length_xld, select_contours_xld边缘点连成折线轮廓
Contourget_region_convex, contlength轮廓与凸包
Measuremeasure_diameter/radial, fit_line/circle/ellipse多卡尺直径 + 几何拟合
Matchncc_levels/roi, shape_*_roi, find_ncc_shape金字塔 + ROI + 混合级联
Geometryaffine / projective / radial_undistort标定与矫正

04 // CODE

最小示例

#include "hvl.h"

HvlImage* img = NULL;
hvl_image_read_pgm("part.pgm", &img);

HvlRegion* dark = NULL;
hvl_threshold(img, 0, 120, &dark);

HvlRegion* opened = NULL;
hvl_opening_rectangle1(dark, 3, 3, &opened);

HvlRegion** blobs = NULL; int n = 0;
hvl_set_system("neighborhood", 8);
hvl_connection(opened, &blobs, &n);

HvlRegion** sel = NULL; int ns = 0;
hvl_select_shape(blobs, n, "area", "and", 100, 1e9, &sel, &ns);

HvlRegion** sorted = NULL; int nsort = 0;
hvl_sort_region(sel, ns, "area", "decreasing", &sorted, &nsort);

HvlRegion* solid = NULL;
hvl_fill_up(sorted[0], &solid);

double mean, dev;
hvl_intensity(img, solid, &mean, &dev);

double row, col, ra, rb, phi, rms;
hvl_fit_ellipse_contour(solid, &row, &col, &ra, &rb, &phi, &rms);

真实图:hvl_monkey_pipeline / hvl_monkey_match / hvl_monkey_hybrid(NCC→shape)· 图:examples/monkey.png

05 // DEMO · monkey.png

真实图结果画廊

对 512×512 灰度 monkey.png 运行中值 / Canny / 分割 / 匹配。 点击图片可看原图。

本地复现: ./build/bin/hvl_monkey_pipeline && ./build/bin/hvl_monkey_match && ./build/bin/hvl_monkey_hybrid · 性能: ./build/bin/hvl_bench_match (1.12:产线配方见库内 docs/RECIPES.md, 发布包 make package

06 // RECIPE

产线默认匹配配方

// 启动时:模板 + shape model
hvl_create_shape_model(templ, 25.0, 1500, &model);

// 在线:NCC 金字塔粗定 → 局部 shape 精修(~30–40ms @512/96)
hvl_find_ncc_shape(img, templ, model,
    3, 0.55, 12,          // levels, ncc_min, pad
    -0.10, 0.20, 0.05,    // angle search
    0.22, 0.05, 0.02, 3,  // shape_min, ang_refine, pos_refine
    4, rows, cols, angs, scores, ncc_r, ncc_c, &n);

// 跟踪帧:只搜上一帧 TL 附近
hvl_find_ncc_levels_roi(img, templ, 3, 0.6, 2, 15, 1,
    tr-40, tc-40, tr+40, tc+40, rows, cols, scores, &n);

完整说明(blob 检测、卡尺、坐标系):源码树 docs/RECIPES.md · 性能 docs/PERF.md

07 // NOTE

定位说明

HVL 是教学与工程骨架级实现,算法与数据结构参考 HALCON 文档及本地 ref/ 行程码实现, 并非 MVTec HALCON 的兼容或替代品。商业产线请以完整验证与合规为准。