<Halcon练习>米粒计数(模板匹配)

在这里插入图片描述

read_image (Image001, '001.bmp')
rgb1_to_gray (Image001, GrayImage)
threshold (GrayImage, Region, 100, 255)
paint_region (Region, GrayImage, ImageResult, 255, 'fill')
* 
* Matching 01: ************************************************
* Matching 01: BEGIN of generated code for model initialization
* Matching 01: ************************************************
set_system ('border_shape_models', 'false')
* 
* Matching 01: Obtain the model image
* Matching 01: The image is assumed to be made available in the
* Matching 01: variable last displayed in the graphics window
copy_obj (ImageResult, Image, 1, 1)
* 
* Matching 01: Build the ROI from basic regions
gen_region_runs (ModelRegion, [39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83], [336,332,330,329,328,327,326,326,326,325,325,325,325,325,325,325,325,326,327,327,328,328,329,330,330,332,333,334,336,337,338,340,341,343,344,345,347,348,349,351,354,356,358,360,363], [342,344,348,352,354,359,361,363,365,366,368,369,371,373,374,375,376,377,377,377,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,378,377,377,377,376,374,372])
* 
* Matching 01: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* 
* Matching 01: Create the shape model
create_shape_model (TemplateImage, 1, rad(0), rad(360), 'auto', 'none', 'use_polarity', [9,16,3], 9, ModelID)
* 
* Matching 01: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)
* 
* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 01: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 01: END of generated code for model initialization
* Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 01: BEGIN of generated code for model application
* Matching 01: The following operations are usually moved into
* Matching 01: that loop where the acquired images are processed
* 
* Matching 01: Find the model
find_shape_model (Image, ModelID, rad(0), rad(360), 0.6, 150, 0.7, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
* 
* Matching 01: Transform the model contours into the detected positions
dev_display (Image)
gen_empty_obj (EmptyObject)
for I := 0 to |Score| - 1 by 1
    hom_mat2d_identity (HomMat2D)
    hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
    hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
    affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
    concat_obj (EmptyObject, TransContours, EmptyObject)

*     stop ()
endfor

    dev_set_color ('green')
    dev_display (TransContours)
* 
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
* 
* 
* Matching 02: ************************************************
* Matching 02: BEGIN of generated code for model initialization
* Matching 02: ************************************************
set_system ('border_shape_models', 'false')
* 
* Matching 02: Obtain the model image
* Matching 02: The image is assumed to be made available in the
* Matching 02: variable last displayed in the graphics window
copy_obj (Image, Image, 1, 1)
* 
* Matching 02: Build the ROI from basic regions
gen_region_runs (ModelRegion, [25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51], [271,270,269,268,268,267,267,267,267,267,267,267,267,268,268,269,269,271,273,276,279,281,282,285,289,292,299], [290,298,309,317,321,322,323,324,325,325,326,326,326,326,326,326,326,326,325,325,324,322,321,319,317,312,308])
* 
* Matching 02: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* 
* Matching 02: Create the shape model
create_shape_model (TemplateImage, 3, rad(0), rad(360), 'auto', 'none', 'use_polarity', [10,11,4], 10, ModelID)
* 
* Matching 02: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)
* 
* Matching 02: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 02: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 02: END of generated code for model initialization
* Matching 02:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 02: BEGIN of generated code for model application
* Matching 02: The following operations are usually moved into
* Matching 02: that loop where the acquired images are processed
* 
* Matching 02: Find the model
find_shape_model (Image, ModelID, rad(0), rad(360), 0.6, 0, 0.6, 'least_squares', [3,1], 0.9, Row, Column, Angle, Score)
* 
* Matching 02: Transform the model contours into the detected positions
dev_display (Image)
for I := 0 to |Score| - 1 by 1
    hom_mat2d_identity (HomMat2D)
    hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
    hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
    affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
    concat_obj (EmptyObject, TransContours, EmptyObject)
    dev_set_color ('green')
    dev_display (TransContours)
*     stop ()
endfor
* 
* Matching 02: *******************************************
* Matching 02: END of generated code for model application
* Matching 02: *******************************************
* 
dev_clear_window ()
dev_display (GrayImage)
dev_display (EmptyObject)
count_obj (EmptyObject, Number)

在这里插入图片描述

数量=94

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请将下面的halcon代码转写为python代码:<?xml version="1.0" encoding="UTF-8"?> <hdevelop file_version="1.2" halcon_version="20.11.0.0"> <procedure name="main"> <interface/> <body> <c as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[1,1]" as_ord="1">* Image Acquisition 01: Code generated by Image Acquisition 01</c> <l as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[1,2]" as_ord="1">list_files ('G:/Grasp-Dataset/brick-text', ['files','follow_links'], ImageFiles)</l> <l as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[1,3]" as_ord="1">tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)</l> <l as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[2,2]" as_ord="1">for Index := 0 to |ImageFiles| - 1 by 1</l> <l as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[2,3]" as_ord="1"> read_image (Image, ImageFiles[Index])</l> <c as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[2,4]" as_ord="1"> * Image Acquisition 01: Do something</c> <l> ImgPath:=ImageFiles[Index]</l> <c> * 分割文件名</c> <l> parse_filename(ImageFiles[Index], BaseName, Extension, Directory)</l> <l> minvalue:=20</l> <l> maxvalue:=60</l> <l> Amp:=minvalue+rand(1)*(maxvalue-minvalue)</l> <l> add_noise_white (Image, ImageNoise, Amp)</l> <l> write_image (ImageNoise, 'jpeg', 0, Directory+BaseName+'noise')</l> <c> </c> <l as_id="image_acquisition" as_name="Image Acquisition 01" as_grp="[3,1]" as_ord="1">endfor</l> </body> <docu id="main"> <parameters/> </docu> </procedure> </hdevelop>
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值