Halcon的Region和XLD相互转换
创始人
2025-05-29 06:10:47

1.类型介绍

Halcon中的图形变量主要有Image类型、Region类型、XLD类型。

1.1 Image类型:

read_image (Image, 'printer_chip/printer_chip_01')

 1.2 Region类型:

draw_region (Region, 3600)

 1.3 XLD类型

        XLD分为两种:边缘轮廓(xld contour)和多边形(xld polygon)

        XLD Contour

dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
draw_xld (ContOut, WindowHandle, 'true', 'true', 'true', 'true')

         XLD Polygon

dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
draw_xld (ContOut, WindowHandle, 'true', 'true', 'true', 'true')
gen_polygons_xld (ContOut, Polygons, 'ramer', 2)

Contour的轮廓更贴近实际的图像边缘

Polygon的轮廓是用多边形组合而成的轮廓

下面举个例子:

read_image (Image, 'printer_chip/printer_chip_01')
gen_rectangle1 (Rectangle, 0, 0, 900, 900)
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, RegionTh, 128, 255)
gen_contour_region_xld (RegionTh, RegionCo, 'border')
gen_polygons_xld (RegionCo, RegionPo, 'ramer', 2)

Contour轮廓如下

 Polygon轮廓如下:

 2.转换规律

        Halcon对象之间的相互转换,一般可以转化为"gen_目标对象_原对象"形式,如gen_region_contour_xld(根据xld创建一个区域region);再如gen_contour_region_xld(根据region创建一个xld轮廓)

2.1 gen_region_contour_xld(Contour : Region : Mode : )
    根据轮廓xld创建一个区域region

read_image(Image,'lena')
draw_xld (XLD1, 3600, 'true', 'true', 'true', 'true')
gen_region_contour_xld (XLD1, Region, 'filled')

2、gen_region_polygon_xld(Polygon : Region : Mode : )
    功能:根据多边形轮廓创建一个区域region

read_image(Image,'lena')
draw_xld (XLD1, 3600, 'true', 'true', 'true', 'true')
gen_polygons_xld (XLD1, Polygons, 'ramer', 2)//用多边形逼近XLD轮廓, Polygons为XLD Polygon类型
gen_region_polygon_xld (Polygons, Region, 'filled')

3、gen_contour_region_xld(Regions : Contours : Mode : )
    功能:根据region创建一个xld

read_image(Image,'lena')
draw_region (Region, 3600)
gen_contour_region_xld (Region, XLD1, 'border')

4、gen_polygons_xld(Contours : Polygons : Type, Alpha : )
    功能:用多边形逼近XLD轮廓

draw_xld (ContOut, 3600, 'true', 'true', 'true', 'true')
gen_polygons_xld (ContOut, Polygons, 'ramer', 2)

5、gen_contours_skeleton_xld(Skeleton : Contours : Length, Mode : )
    功能:把骨架转换层xld轮廓

read_image (Bk45, 'bk45')
//bandpass_image — 使用带通滤波器的边缘提取
bandpass_image (Bk45, Lines, 'lines')
//threshold — Segment an image using global threshold.
threshold (Lines, Region, 128, 255)
//skeleton — Compute the skeleton of a region.
skeleton (Region, Skeleton)
dev_set_colored (12)
//gen_contours_skeleton_xld — 将skeleton 转换为 XLD 轮廓
gen_contours_skeleton_xld (Skeleton, Contours, 5, 'filter')
dev_display (Bk45)
dev_display (Contours)

3.xld/region转换成image

例1:将区域所包含的像素做为图像输出

*Halcon感兴趣区域填充特定颜色

read_image (Image, 'printer_chip/printer_chip_01')
gen_rectangle1 (Rectangle, 0, 0, 900, 900)
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, RegionTh, 128, 255)
*将区域直接生成二值图像
region_to_bin(Region, Binary, 0, 255, Width, Height)
*将Image中的RegionTh区域填充255
overpaint_region (Image, RegionTh, 255, 'fill')
write_image (Image, 'bmp', 0, 'E:/Org.bmp')

例2:图像剪贴,将一个图贴在另一个之上。

*创建空白图像,将得到的区域贴到上面

get_image_size (ImageReduced, Width1, Height1)
gen_image_proto (ImageReduced, ImageCleared, 128)
paint_region (Region, ImageCleared, ImageResult1, 255, 'fill')

例3:xld->region->image

read_image (Image, 'printer_chip/printer_chip_01')
gen_rectangle1 (Rectangle, 0, 0, 900, 900)
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, RegionTh, 128, 255)
gen_contour_region_xld (RegionTh, RegionCo, 'border')
gen_region_contour_xld (RegionCo, RegionTh2, 'filled')
*创建一个具有恒定灰度值的图像。
gen_image_const (ImageCo, 'byte', Width1, Height1)
*创建一个具有指定的恒定灰度值的图像。
gen_image_proto (ImageCo, ImagePr, 0)
*将区域绘制到图像中。
paint_region (RegionTh, ImagePr, ImageResult, 255, 'fill')

注:paint_region 和overpaint_region最终的输出结果是一样的
 

相关内容

热门资讯

V观财报|上汽集团:2027年... 【V观财报|上汽集团:2027年正式实现全固态电池量产交付】上汽集团11月21日在投资者互动平台回复...
啥情况?一字跌停!7板“大妖股... 突然暴涨,又突然暴跌。11月21日,“男裤龙头”九牧王(601566.SH)一字跌停。而在此之前,1...
年赚10亿的AI单品,喂不饱大... 出品|虎嗅黄青春频道作者|商业消费主笔黄青春题图|视觉中国当一众媒体还在为 Gemini 3 Pro...
V观财报|吉林化纤:独董徐樑华... 【V观财报|吉林化纤:独董徐樑华辞职】吉林化纤公告,近日收到公司独立董事徐樑华提交的书面辞职报告。徐...
英伟达内部会,黄仁勋坦言:太难... 全球市场陷入“AI泡沫”争论之际,即便是最成功的英伟达,也正面临着前所未有的审视压力。作为AI基础设...