* 1.读取图像
read_image (Image, 'C:/Users/29939/Desktop/123456/小圆_co_h_w_inspect.bmp')
rgb1_to_gray (Image, GrayImage)

* 2.指定模板图像区域
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (GrayImage)
draw_circle (WindowHandle, Row, Column, Radius)
gen_circle (Circle, Row, Column, Radius)
reduce_domain (GrayImage, Circle, ImageROI)

* 3.创建模板
*亚像素轮廓
edges_sub_pix (ImageROI, Edges, 'canny', 1, 40, 70)
*创建轮廓模板
create_shape_model_xld (Edges, 'auto', rad(-180), rad(360), 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)

* 4.查找匹配模板
find_scaled_shape_model (GrayImage, ModelID, -0.39, 0.78, 0.5, 2.1, 0.5, 5, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Scale, Score1)
dev_display_shape_matching_results (ModelID, 'red', Row1, Column1, Angle, 1, 1, 0)


read_image(Image, 'D:/31199-Others/Learning  App/Halcon/circle')
rgb1_to_gray(Image, GrayImage)
*创建模板轮廓
Radius:=55
gen_ellipse_contour_xld(ContEllipse, 200, 200, 0, 55, 55, 0, rad(360), 'positive', 1.5)
gen_image_const (Image1, 'byte', 512, 512)
paint_xld (ContEllipse, Image1, ImageResult, 150)
create_scaled_shape_model(ImageResult, 'auto', -0.39, 0.79, 'auto', 0.8, 1.2, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
* create_shape_model_xld(ContEllipse, 'auto', -0.39, 0.79, 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)
*获得模板轮廓
get_shape_model_contours(ModelContours, ModelID, 1)
*根据模板进行匹配
find_scaled_shape_model(GrayImage, ModelID, -0.39, 0.78, 0.8, 1.2, 0.5, 7, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Scale, Score)
dev_display(GrayImage)

for Index := 0 to |Score|-1 by 1
*求模板与匹配结果的映射关系   
    vector_angle_to_rigid(0, 0, 0, Row[Index], Column[Index], Angle[Index], HomMat2D)
*根据映射关系得匹配后的轮廓   
    affine_trans_contour_xld(ModelContours, ContoursAffineTrans, HomMat2D)  
*显示匹配后的轮廓
    dev_display(ContoursAffineTrans)
endfor

*dev_display_shape_matching_results(ModelID, 'red', Row, Column, Angle, 1, 1, 0)
* 1.读取图像
read_image (Image, 'C:/Users/29939/Desktop/123456/小圆_co_h_w_inspect.bmp')
rgb1_to_gray (Image, GrayImage)

* 2.指定模板图像区域
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (GrayImage)
draw_circle (WindowHandle, Row, Column, Radius)
gen_circle (Circle, Row, Column, Radius)
reduce_domain (GrayImage, Circle, ImageROI)

* 3.创建模板
*亚像素轮廓
edges_sub_pix (ImageROI, Edges, 'canny', 1, 40, 70)
gen_region_contour_xld(Edges, Region2, 'filled')
union1(Region2, RegionUnion)
gen_contour_region_xld(RegionUnion, Contours1, 'border')

*创建轮廓模板
create_shape_model_xld (Contours1, 'auto', rad(-180), rad(360), 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)

* 4.查找匹配模板
find_scaled_shape_model (GrayImage, ModelID, -0.39, 0.78, 0.5, 2.1, 0.5, 0, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Scale, Score1)

* 5.显示结果
dev_display_shape_matching_results (ModelID, 'red', Row1, Column1, Angle, 1, 1, 0)
get_shape_model_contours(ModelContours, ModelID, 1)

count_obj(ModelContours, Number)
*dev_display(GrayImage)
select_obj(ModelContours, ObjectSelected, 1)
for Index := 0 to |Row1|-1 by 1
    vector_angle_to_rigid(0, 0, 0, Row1[Index], Column1[Index], Angle[Index], HomMat2D)
    affine_trans_contour_xld(ModelContours, ContoursAffineTrans, HomMat2D)
    gen_region_contour_xld (ContoursAffineTrans, Region, 'filled')
   * erosion1 (Region, Region, RegionErosion, 1)
    reduce_domain(GrayImage, Region, ImageReduced)
    
    dev_display(Region)
endfor

* 找出contours


* contours转region

* region腐蚀去除边缘

* region进行阈值分割