halcon定位匹配圆

* 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进行阈值分割







模板匹配,检测缺陷

read_image (Image1, 'C:/Users/40143/Desktop/小圆_co_h_w_inspect.bmp')
rgb1_to_gray(Image1, Image)
get_image_size(Image, Width, Height)

gen_image_const(Image2, 'byte', Width, Height)
gen_circle(Circle, Height/2, Width/2, 60)
paint_region(Circle, Image2, RegionDilation, 255, 'fill')

create_scaled_shape_model (RegionDilation, 'auto', 0, 6.29, 'auto', 0.5, 1.4, 'auto', 'none', 'use_polarity', 100, 20, ModelID)

get_shape_model_contours (ShapeModel, ModelID, 1)
find_scaled_shape_model (Image, ModelID, 0, 0, 0.8, 1.2, 0.6, 1000, 0, 'least_squares', 1, 0.8, RowCheck, ColumnCheck, AngleCheck, ScaleCheck, Score)

dev_display(Image)
gen_empty_obj(EmptyObject)
for i := 0 to |Score| - 1 by 1
      *用映射矩阵显示
      vector_angle_to_rigid (0, 0, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject)
      hom_mat2d_scale (MovementOfObject, ScaleCheck[i], ScaleCheck[i], RowCheck[i], ColumnCheck[i], MoveAndScalingOfObject)
      copy_obj (ShapeModel,ObjectsSelected, 1, 1)
      affine_trans_contour_xld (ObjectsSelected, ModelAtNewPosition, MoveAndScalingOfObject)
*       dev_display (ModelAtNewPosition)
      gen_region_contour_xld(ModelAtNewPosition, Region, 'filled')
      change_domain(Image,Region, ImageNew)
      threshold(ImageNew, Region1, 160, 255)
      erosion_circle(Region,RegionErosion, 3.5)
      difference(RegionErosion,Region1,RegionDifference)
      concat_obj (EmptyObject, RegionDifference, EmptyObject)
      dev_clear_window()
      dev_display(Image)
      dev_display(EmptyObject)      
      stop()
endfor

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值