*
* 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
* 获取图片
read_image (Image, 'C:/Users/11466/Desktop/验证码/timg.jpg')
*
* Matching 01: Build the ROI from basic regions
* 生成一个矩形框(自己画的)ROI (Region of Interest),感兴趣区域。顾名思义,就是你主要想处理的区域
gen_rectangle1 (ModelRegion, 118.291, 460.593, 242.122, 605.931)
*
* Matching 01: Reduce the model template
* 裁剪模型区域
*运算符reduce_domain将给定图像的定义域缩小到指示的区域。
*将新定义域计算为旧定义域与该区域的交集。
*因此,新的定义域可以是该区域的子集。 矩阵的大小不变。
reduce_domain (Image, ModelRegion, TemplateImage)
*
* Matching 01: Create the shape model
*创建形状模型
*准备一个模板,该模板在图像模板中传递,作为用于匹配的形状模型。
create_shape_model (TemplateImage, 4, rad(-20), rad(360), rad(1.5813), ['none','no_pregeneration'], 'use_polarity', [22,43,7], 22, 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)
*根据点对应关系和两个对应角度计算刚性仿射变换,即由旋转和平移组成的变换,
*并将其作为齐次变换矩阵HomMat2D返回。
*仿射变换:线性变换”+“平移”
*仿射变换是单纯对图片进行缩放,倾斜和旋转,因此图片不论如何变化,
*线之间的平行性是不变的
*其实就是由图像平移、旋转、放缩、错切之后得来的(看仿射变换的公式)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 指定的轮廓应用任意仿射2D变换,即缩放,旋转,平移和倾斜(倾斜),
*并在ContoursAffineTrans中返回变换后的轮廓
* 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: Loop over all specified test images
*TestImages := ['C:/Users/11466/Desktop/验证码/timg (5).jpg','C:/Users/11466/Desktop/验证码/timg (6).jpg','C:/Users/11466/Desktop/验证码/timg 5.jpg','C:/Users/11466/Desktop/验证码/timg.jpg','C:/Users/11466/Desktop/验证码/u=2005407448,2931409318&fm=26&gp=0.jpg','C:/Users/11466/Desktop/验证码/25796281_1379406478695_1024x1024.jpg','C:/Users/11466/Desktop/验证码/3.jpg','C:/Users/11466/Desktop/验证码/download.jpg','C:/Users/11466/Desktop/验证码/timg (1).jpg','C:/Users/11466/Desktop/验证码/timg (2).jpg','C:/Users/11466/Desktop/验证码/timg (3).jpg','C:/Users/11466/Desktop/验证码/timg (4).jpg','C:/Users/11466/Desktop/验证码/timg (5).jpg','C:/Users/11466/Desktop/验证码/timg (6).jpg','C:/Users/11466/Desktop/验证码/timg (7).jpg','C:/Users/11466/Desktop/验证码/timg 5.jpg','C:/Users/11466/Desktop/验证码/timg.jpg','C:/Users/11466/Desktop/验证码/u=2005407448,2931409318&fm=26&gp=0.jpg','C:/Users/11466/Desktop/验证码/u=2011195191,3975867501&fm=26&gp=0.jpg','C:/Users/11466/Desktop/验证码/u=2924159685,1127158942&fm=26&gp=0.jpg']
*for T := 0 to 19 by 1
*
* Matching 01: Obtain the test image
*read_image (Image, TestImages[T])
read_image (Image, 'C:/Users/11466/Desktop/验证码/u=2924159685,1127158942&fm=26&gp=0.jpg')
*
* Matching 01: Find the model
*查找模型
*之前要有create_shape_model
find_shape_model (Image, ModelID, rad(-20), rad(360), 0.16, 1, 0.5, 'least_squares', [4,1], 0.5, Row, Column, Angle, Score)
*
* Matching 01: Transform the model contours into the detected positions
*将模型轮廓转换为检测到的位置
dev_display (Image)
for I := 0 to |Score| - 1 by 1
hom_mat2d_identity (HomMat2D)
*生成描述相同2D变换的齐次变换矩阵
hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
*将旋转角度Phi添加到齐次2D转换矩阵
hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
*平移矩阵加到转换矩阵HomMat2D,
*并在HomMat2DTranslate中返回所得矩阵
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
*把轮廓经过二维变换,并在ContoursAffineTrans中返回变换后的轮廓
dev_set_color ('green')
dev_display (TransContours)
*把轮廓用绿色的线描出来
stop ()
endfor
*
* Matching 01: Code for alignment of e.g. measurements
*精准代码
* Matching 01: Calculate a hom_mat2d for each of the matching results
*为每个匹配结果计算hom_mat2d
for I := 0 to |Score| - 1 by 1
hom_mat2d_identity (AlignmentHomMat2D)
hom_mat2d_translate (AlignmentHomMat2D, -RefRow, -RefColumn, AlignmentHomMat2D)
hom_mat2d_rotate (AlignmentHomMat2D, Angle[I], 0, 0, AlignmentHomMat2D)
hom_mat2d_translate (AlignmentHomMat2D, Row[I], Column[I], AlignmentHomMat2D)
* Matching 01: Insert your code using the alignment here, e.g. code generated by
* Matching 01: the measure assistant with the code generation option
* Matching 01: 'Alignment Method' set to 'Affine Transformation'.
endfor
*
* Matching 01: Code for rectification of the image
*图像校正代码
* Matching 01: Calculate an inverse hom_mat2d for each of the matching results
*为每个匹配结果计算一个逆hom_mat2d 逆矩阵
for I := 0 to |Score| - 1 by 1
hom_mat2d_identity (RectificationHomMat2D)
hom_mat2d_translate (RectificationHomMat2D, RefRow-Row[I], RefColumn-Column[I], RectificationHomMat2D)
hom_mat2d_rotate (RectificationHomMat2D, -Angle[I], RefRow, RefColumn, RectificationHomMat2D)
affine_trans_image (Image, RectifiedImage, RectificationHomMat2D, 'constant', 'false')
*
* Matching 01: Insert your code using the rectified image here
*在此处使用校正后的图像插入代码
endfor
* endfor
*
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
*
Halcon匹配助手代码注释
最新推荐文章于 2023-02-01 16:01:15 发布