* This example program shows how to use HALCON's shape-based matching
* to find SMD capacitors that exhibit independent size changes in the row and
* column direction in images with a depth of 10 bits (i.e., in images of type uint2).
dev_update_off ()
* For visualization purposes, we specify the number of significant bits in the uint2
* images. We do this because this information cannot be stored in the image
* files themselves.
set_system ('int2_bits', 10)
read_image (Image, 'smd/smd_capacitors_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('green')
dev_set_line_width (3)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* Create a synthetic model for the SMD capacitors. This is just a rectangle
* with rounded corners.
gen_contour_polygon_rounded_xld (Contour, [50,100,100,50,50], [50,50,150,150,50], [6,6,6,6,6], 0.01)
*根据一个多边形(polygon)(以元组形式给出)的圆形角点创建一个XLD轮廓(contour)
*((输出)结果轮廓,多边形的行列坐标,多边形的圆角半径,采样距离)
gen_image_const (Image, 'byte', 200, 150)
*创建一个固定灰度值的图像(输出图像,像素类型,图像宽,高)
paint_xld (Contour, Image, ImageModel, 128)
*把XLD目标画在一个图像中(要绘制到图像去的XLD轮廓,图像,结果图像,xld轮廓期望的灰度值)
create_aniso_shape_model (ImageModel, 'auto', -rad(10), rad(20), 'auto', 0.9, 1.7, 'auto', 0.9, 1.1, 'auto', 'none', 'use_polarity', 'auto', 20, ModelID)
*
get_shape_model_contours (ModelContours, ModelID, 1)
*
Button := 0
ImgNo := 1
while (Button # 1)
read_image (Image, 'smd/smd_capacitors_'+ImgNo$'02d')
dev_display (Image)
count_seconds (S1)
find_aniso_shape_model (Image, ModelID, -rad(10), rad(20), 0.9, 1.7, 0.9, 1.1, 0.7, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, ScaleR, ScaleC, Score)
count_seconds (S2)
Time := (S2-S1)*1000
Num := |Score|
* Display the number of found models and the recognition time.
set_tposition (WindowHandle, 10, 10)
write_string (WindowHandle, Num$'d'+' models found in '+Time$'5.2f'+' ms')
MeanColumn := mean(Column)
*找出工件,并计算出工件与模板的关系
for I := 0 to Num-1 by 1
* Display the found model.
hom_mat2d_identity (HomMat2D)
*二维描述矩阵,就是一个三维单位矩阵。
hom_mat2d_scale (HomMat2D, ScaleR[I], ScaleC[I], 0, 0, HomMat2D)
*为一个同质二维变换矩阵添加一个缩放(输入矩阵,缩放因子,固定点,结果矩阵)
hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
*为一个同质二维变换矩阵添加一个循环(输入矩阵,旋转角度,固定点,输出矩阵)
hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
*为一个同质二维变换矩阵添加一个转换
affine_trans_contour_xld (ModelContours, ContoursTrans, HomMat2D)
*仿射转换。
dev_display (ContoursTrans)
* Display the model's scale next to the found model.
ScaleRowStr := 'ScaleRow='+ScaleR[I]$'5.3f'
ScaleColStr := 'ScaleCol='+ScaleC[I]$'5.3f'
get_string_extents (WindowHandle, ScaleRowStr, AscentStr, DescentStr, WidthStr, HeightStr)
*获取一个字符串的空间大小(窗口句柄,要考虑的变量,往上的最大高度,往下的最大延伸,文本宽,文本高)
if (Column[I] <= MeanColumn)
disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I]-20, Column[I]-60-WidthStr, 'green', 'false')
else
disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I]-20, Column[I]+60, 'green', 'false')
endif
endfor
ImgNo := ImgNo+1
if (ImgNo > 4)
ImgNo := 1
endif
dev_error_var (Error, 1)
dev_set_check ('~give_error')
get_mposition (WindowHandle, R, C, Button)
dev_error_var (Error, 0)
dev_set_check ('give_error')
if (Error#H_MSG_TRUE)
Button := 0
endif
endwhile
clear_shape_model (ModelID)
* Reset the number of significant bits of uint2 images to unknown.
set_system ('int2_bits', -1)
* to find SMD capacitors that exhibit independent size changes in the row and
* column direction in images with a depth of 10 bits (i.e., in images of type uint2).
dev_update_off ()
* For visualization purposes, we specify the number of significant bits in the uint2
* images. We do this because this information cannot be stored in the image
* files themselves.
set_system ('int2_bits', 10)
read_image (Image, 'smd/smd_capacitors_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('green')
dev_set_line_width (3)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* Create a synthetic model for the SMD capacitors. This is just a rectangle
* with rounded corners.
gen_contour_polygon_rounded_xld (Contour, [50,100,100,50,50], [50,50,150,150,50], [6,6,6,6,6], 0.01)
*根据一个多边形(polygon)(以元组形式给出)的圆形角点创建一个XLD轮廓(contour)
*((输出)结果轮廓,多边形的行列坐标,多边形的圆角半径,采样距离)
gen_image_const (Image, 'byte', 200, 150)
*创建一个固定灰度值的图像(输出图像,像素类型,图像宽,高)
paint_xld (Contour, Image, ImageModel, 128)
*把XLD目标画在一个图像中(要绘制到图像去的XLD轮廓,图像,结果图像,xld轮廓期望的灰度值)
create_aniso_shape_model (ImageModel, 'auto', -rad(10), rad(20), 'auto', 0.9, 1.7, 'auto', 0.9, 1.1, 'auto', 'none', 'use_polarity', 'auto', 20, ModelID)
*
get_shape_model_contours (ModelContours, ModelID, 1)
*
Button := 0
ImgNo := 1
while (Button # 1)
read_image (Image, 'smd/smd_capacitors_'+ImgNo$'02d')
dev_display (Image)
count_seconds (S1)
find_aniso_shape_model (Image, ModelID, -rad(10), rad(20), 0.9, 1.7, 0.9, 1.1, 0.7, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, ScaleR, ScaleC, Score)
count_seconds (S2)
Time := (S2-S1)*1000
Num := |Score|
* Display the number of found models and the recognition time.
set_tposition (WindowHandle, 10, 10)
write_string (WindowHandle, Num$'d'+' models found in '+Time$'5.2f'+' ms')
MeanColumn := mean(Column)
*找出工件,并计算出工件与模板的关系
for I := 0 to Num-1 by 1
* Display the found model.
hom_mat2d_identity (HomMat2D)
*二维描述矩阵,就是一个三维单位矩阵。
hom_mat2d_scale (HomMat2D, ScaleR[I], ScaleC[I], 0, 0, HomMat2D)
*为一个同质二维变换矩阵添加一个缩放(输入矩阵,缩放因子,固定点,结果矩阵)
hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
*为一个同质二维变换矩阵添加一个循环(输入矩阵,旋转角度,固定点,输出矩阵)
hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
*为一个同质二维变换矩阵添加一个转换
affine_trans_contour_xld (ModelContours, ContoursTrans, HomMat2D)
*仿射转换。
dev_display (ContoursTrans)
* Display the model's scale next to the found model.
ScaleRowStr := 'ScaleRow='+ScaleR[I]$'5.3f'
ScaleColStr := 'ScaleCol='+ScaleC[I]$'5.3f'
get_string_extents (WindowHandle, ScaleRowStr, AscentStr, DescentStr, WidthStr, HeightStr)
*获取一个字符串的空间大小(窗口句柄,要考虑的变量,往上的最大高度,往下的最大延伸,文本宽,文本高)
if (Column[I] <= MeanColumn)
disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I]-20, Column[I]-60-WidthStr, 'green', 'false')
else
disp_message (WindowHandle, [ScaleRowStr,ScaleColStr], 'image', Row[I]-20, Column[I]+60, 'green', 'false')
endif
endfor
ImgNo := ImgNo+1
if (ImgNo > 4)
ImgNo := 1
endif
dev_error_var (Error, 1)
dev_set_check ('~give_error')
get_mposition (WindowHandle, R, C, Button)
dev_error_var (Error, 0)
dev_set_check ('give_error')
if (Error#H_MSG_TRUE)
Button := 0
endif
endwhile
clear_shape_model (ModelID)
* Reset the number of significant bits of uint2 images to unknown.
set_system ('int2_bits', -1)