本篇记录halcon图像image、区域region和轮廓xld的相互转换
一、xld转region
方法1:
gen_region_contour_xld (SelectedXLD, RegionXLD, 'filled')
方法2:
二、region转xld
方法1:gen_contour_region_xld (SelectedRegions, Contours, 'border')
拟合部分边缘提取和轮廓分割之间会用到,因为轮廓分割需输入xld轮廓,而用boundary提取区域边缘输出的是区域(region),所以需要转换。
方法2:先将区域转换骨架,然后再提取骨架轮廓
skeleton (Region2, Skeleton2)
gen_contours_skeleton_xld (Skeleton2, Contours, 1, 'filter')
三、xld/region转换成image
方法1:
*Halcon感兴趣区域填充特定颜色
color24 := [255,0,0]
color8 := 255
gen_region_contour_xld (UnionContoursCircles, Region1, 'filled')
*区域转换为图片
region_to_bin(Region, Binary, 0, 255, Width, Height)
overpaint_region (Binary, Region1, color8, 'fill')
write_image (Binary, 'bmp', 0, 'E:/Org.bmp')
或者
threshold (Image1, Region, 128, 255)
region_to_bin(Region, Binary, 0, 255, Width, Height)
write_image (Binary, 'bmp', 0, 'E:/Org.bmp')
或者
binary_threshold (Image1, BrightRegion, 'max_separability', 'dark', UsedThreshold)
region_to_bin(BrightRegion, SaveBinary, 0, 255, Width, Height)
write_image (SaveBinary, 'jpeg 100', 0, 'd:/Org.jpg')
方法2:
*创建空白图像,将得到的区域贴到上面 get_image_size (ImageReduced, Width1, Height1) gen_image_proto (ImageReduced, ImageCleared, 128) paint_region (Region, ImageCleared, ImageResult1, 255, 'fill')
方法3:(推荐)xld->region->image
*无效set_system ('init_new_image', 'false') gen_region_contour_xld (ObjectSelected, Region, 'filled') gen_image_const (NewImage, 'byte', Width, Height) *Create an image with a specified constant gray value gen_image_proto (NewImage, ImageCleared1, 255) *Paint regions into an image paint_region (Region, ImageCleared1, ImageResult, 0, 'fill') write_image (ImageResult, 'jpeg', 0, 'D:/1111.jpg') *Overpaint regions in an image gen_image_proto (NewImage, ImageCleared2, 255) overpaint_region(ImageCleared2, Region, 0, 'fill')
结论:paint_region 和overpaint_region最终的输出结果是一样的
四、从image裁剪需要的区域,成为新的image
read_image(Image,'monkey') gen_rectangle1 (ROI_0, 588.03, 468.95, 2328.43, 3212.37) reduce_domain (Image, ROI_0, ImageReduced) crop_domain(ImageReduced, ImagePart) write_image(ImagePart, 'bmp', 0, 'e:/1.bmp')
五、从image获得region
binary_threshold (Image, Region, 'smooth_histo', 'dark', UsedThreshold) mean_image (Image, ImageMean, 12, 12) dyn_threshold (Image, ImageMean, Region, 30, 'dark') read_image (Image2, 'D:/1.jpg') draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) reduce_domain (Image2, Rectangle, ImageReduced) threshold (ImageReduced, Region, 100, 255) area_center (Region, Area3, Row3, Column3) for I := 0 to |Row3| - 1 by 1 gen_cross_contour_xld (Cross, Row3[I], Column3[I], 20, 0) endfor stop()
六、综合
*动态二值化
D:=31 mean_image(Image1, Mean, D*2+1, D*2+1) dyn_threshold(Image1, Mean, Seg, 5,'dark') R:=2.5 erosion_circle (Seg, RegionErosion, R) dilation_circle (RegionErosion, RegionDilation, R) fill_up (RegionDilation, RegionFillUp) connection(RegionFillUp, Regions) select_shape (Regions, SelectedRegions, 'area', 'and', 100, 30000) region_to_bin(SelectedRegions, Binary, 0, 255, Width, Height) area_center (SelectedRegions, Area, Row, Column) for I := 0 to |Row| - 1 by 1 gen_cross_contour_xld (Cross, Row[I], Column[I], 20, 0) endfor stop()