Halcon算子抠图截图调用模型文件--模板匹配

Halcon算子抠图截图调用模型文件--模板匹配--基于形状


* Matching 15: BEGIN of generated code for model initialization
*set_system ('border_shape_models', 'true')
* Matching 15: Obtain the model image
read_image (Image, 'D:/ats_Work/Work_Project/IQ项目/pics/1b445864-9d45-4ed5-b39c-0a2b23034a71.jpg')
* Matching 15: build the ROI from basic regions
*gen_rectangle1 (ModelRegion, 623.053, 569.565, 782.423, 724.185)
* Matching 15: reduce the model template
*reduce_domain (Image, ModelRegion, TemplateImage)
* Matching 15: create the shape model
*create_shape_model (TemplateImage, 3, rad(0), rad(30), rad(1.0592), ['point_reduction_high','no_pregeneration'], 'use_polarity', [35,47,125], 4, ModelId)//创建模型
* Matching 15: get the model contour for transforming it later into the image



*get_shape_model_contours (ModelContours, ModelId, 1) //获取形状


*write_shape_model (ModelId, 'D:/halcon_project/mod1.shm') //保存模型


read_shape_model('D:/halcon_project/mod1.shm',ModelId) //读取模型
get_shape_model_contours (ModelContours, ModelId, 1)//获取形状
* Matching 15: END of generated code for model initialization
* Matching 15: BEGIN of generated code for model application
* Matching 15: the following operations are usually moved into
* Matching 15: that loop where the aquired images are processed
* Matching 15: Find the model


find_shape_model (Image, ModelId, rad(0), rad(30), 0.3, 27, 0.5, 'least_squares', [3,1], 0.9, ModelRow, ModelColumn, ModelAngle, ModelScore) //通过模型再图中找匹配信息
* Matching 15: transform the model contours into the detected positions
for MatchingObjIdx := 0 to |ModelScore| - 1 by 1
hom_mat2d_identity (HomMat)
hom_mat2d_rotate (HomMat, ModelAngle[MatchingObjIdx], 0, 0, HomMat)
hom_mat2d_translate (HomMat, ModelRow[MatchingObjIdx], ModelColumn[MatchingObjIdx], HomMat)
affine_trans_contour_xld (ModelContours, TransContours, HomMat)
dev_display (TransContours)

gen_rectangle1 (ModelRegion, ModelRow[MatchingObjIdx]-75, ModelColumn[MatchingObjIdx]-75, ModelRow[MatchingObjIdx]+75, ModelColumn[MatchingObjIdx]+75)
* Matching 15: reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage) //截取图片
crop_domain (TemplateImage, ImagePart) //去除黑色区域
write_image (ImagePart, 'jpg', 0, MatchingObjIdx +'.jpg') //写入文件

endfor
* Matching 15: Clear model when done
* dev_open_window(0,0,300,300,'black',WindowHandle1)
clear_shape_model (ModelId)
* Matching 15: END of generated code for model application


 

posted @ 2025-07-09 16:50  txwtech  阅读(38)  评论(4)    收藏  举报