http://write.blog.youkuaiyun.com/postedit
* 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)