*读取图像
read_image (Image41, '41.bmp')
dev_close_window ()
get_image_size (Image41, Width, Height)
dev_open_window (0, 0, Width/4, Height/4, 'black', WindowHandle)
dev_display (Image41)
*模板匹配
Row1 := 989.5
Column1 := 863.5
Row2 := 1161.5
Column2 := 1239.5
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
*gen_rectangle1 (Rectangle, 989.5, 863.5, 1161.5, 1239.5)
area_center (Rectangle, Area, Row, Column)
*1076 1052
Rect1Row := 211.5
Rect1Col := -4.5
RectPhi := rad(0.9)
RectLength1 := 244
RectLength2 := 67.5
*gen_rectangle2 (ROI_0, 1287.5, 1047.5, rad(0.939191), 244.033, 67.5975)
gen_rectangle2 (Rectangle1, Row + Rect1Row, Column + Rect1Col, RectPhi, RectLength1, RectLength2)
reduce_domain (Image41, Rectangle, ImageReduced)
*创建模板
create_shape_model (ImageReduced, 4, 0, rad(360), rad(1), 'none', 'use_polarity', 30, 10, ModelID)
get_shape_model_contours (ShapeModel, ModelID, 1)
*仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DTranslate)
dev_display (Image41)
dev_set_color ('green')
dev_display (ShapeModelTrans)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Rectangle1)
dev_set_draw ('fill')
stop()
*列出图片文件
list_image_files ('.', 'default', 'follow_links', ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
*循环读图
is_ng:=0
for ImgIndex := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[ImgIndex])
get_image_size (Image, Width, Height)
*搜寻模板
find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', [5,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
if (|ModelScore| > 0)
dev_set_color ('green')
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, ModelRow, ModelColumn, HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, ModelAngle, ModelRow, ModelColumn, HomMat2DRotate)
affine_trans_contour_xld (ShapeModel, ShapeModelTrans, HomMat2DRotate)
dev_display (ShapeModelTrans)
affine_trans_pixel (HomMat2DRotate, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, ModelAngle+RectPhi, RectLength1, RectLength2)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Rectangle1Check)
dev_set_draw ('fill')
*预处理
scale_image_max (Image, ImageScaleMax)
invert_image (ImageScaleMax, ImageInvert)
*测量
gen_measure_rectangle2 (Rect1RowCheck, Rect1ColCheck, ModelAngle+RectPhi, RectLength1, RectLength2, Width, Height, 'bilinear', MeasureHandle1)
measure_pairs (ImageInvert, MeasureHandle1, 18, 4.6, 'all', 'all', RowEdgeFirst1, ColumnEdgeFirst1, AmplitudeFirst1, RowEdgeSecond1, ColumnEdgeSecond1, AmplitudeSecond1, IntraDistance1, InterDistance1)
close_measure (MeasureHandle1)
dev_display (Image)
dev_set_line_width(2)
*双边显示
dev_display_measure_object (Rect1RowCheck, Rect1ColCheck, ModelAngle+RectPhi, RectLength1, RectLength2)
Pin := 1
dev_set_color ('red')
disp_line (WindowHandle, RowEdgeFirst1-RectLength2 *cos(ModelAngle+RectPhi), ColumnEdgeFirst1-RectLength2 *sin(ModelAngle+RectPhi), RowEdgeFirst1+RectLength2 *cos(ModelAngle+RectPhi), ColumnEdgeFirst1+RectLength2 *sin(ModelAngle+RectPhi))
disp_line (WindowHandle, RowEdgeSecond1-RectLength2 *cos(ModelAngle+RectPhi), ColumnEdgeSecond1-RectLength2 *sin(ModelAngle+RectPhi), RowEdgeSecond1+RectLength2 *cos(ModelAngle+RectPhi), ColumnEdgeSecond1+RectLength2 *sin(ModelAngle+RectPhi))
for I := 0 to |ColumnEdgeFirst1| - 2 by 1
disp_message (WindowHandle, '引脚间距: ' + InterDistance1[I]$'.2f' , 'image', RowEdgeSecond1[I]+200, ColumnEdgeSecond1[I]-250, 'yellow', 'false')
get_string_extents (WindowHandle, Pin, Ascent, Descent, SWidth, SHeight)
Pin := Pin + 1
endfor
*判断
for Index := 0 to |InterDistance1|-1 by 1
tuple_select (InterDistance1, Index, Selected)
if( Selected<110 or Selected>115)
is_ng:=1
endif
endfor
if (is_ng == 1)
disp_message (WindowHandle, '不合格', 'window', 0, 0, 'black', 'true')
else
disp_message (WindowHandle, '合格', 'window', 0, 0, 'black', 'true')
endif
is_ng:=0
endif
stop()
endfor
重点关注:
Rect1Row := 211.5
Rect1Col := -4.5
RectPhi := rad(0.9)
*对点坐标进行仿射变换,并非原测量矩形的中心坐标,而是相对差值坐标
affine_trans_pixel (HomMat2DRotate, Rect1Row, Rect1Col, Rect1RowCheck, Rect1ColCheck)
gen_rectangle2 (Rectangle1Check, Rect1RowCheck, Rect1ColCheck, ModelAngle+RectPhi, RectLength1, RectLength2)
运行结果: