Convert Halcon's Region and XLD to each other

1. Type introduction

Graphical variables in Halcon mainly include Image type, Region type, and XLD type.

1.1 Image type:

read_image (Image, 'printer_chip/printer_chip_01')

 1.2 Region type:

draw_region (Region, 3600)

 1.3 XLD type

        XLD is divided into two types: edge contour (xld contour) and polygon (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's outline is closer to the actual image edge

The outline of Polygon is an outline composed of polygons

Here is an example:

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)

The Contour outline is as follows

 The Polygon outline is as follows:

 2. Conversion rules

        The mutual conversion between Halcon objects can generally be converted into the form of "gen_target object_original object", such as gen_region_contour_xld (create a region region according to xld); another example is gen_contour_region_xld (create an xld contour according to region)

2.1 gen_region_contour_xld(Contour : Region : Mode : )
    creates a region region based on the contour xld

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 : )
    Function: Create a region region according to the outline of the polygon

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 : )
    Function: Create an xld according to the region

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

4. gen_polygons_xld(Contours : Polygons : Type, Alpha : )
    function: use polygons to approximate XLD contours

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

5. gen_contours_skeleton_xld(Skeleton : Contours : Length, Mode : )
    Function: Convert the skeleton to the xld outline of the layer

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. Convert xld/region to image

Example 1: Output the pixels contained in the area as an image

* Halcon region of interest is filled with a specific color

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')

Example 2: Image clipping, pasting one image on top of another.

*Create a blank image and paste the resulting area onto it

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

Example 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')

Note: The final output of paint_region and overpaint_region is the same
 

Guess you like

Origin blog.csdn.net/qq_40732350/article/details/129585707