35.7、仿射变换实例四(图像变换用于拟合圆、直线)
目标:对一组如下图的图像的左边圆弧进行拟合(这组图像中的其他图像可能与下图存在位移、选择、缩放等关系)
分析:由日志《33.1、拟合圆、直线(补充)》可知,只要知道拟合的大概区域的几个点(四个点以上)就可以对这块区域进行拟合操作。可以用上图作为定位模板对这组图像进行定位并找出用于拟合上图的几个点用于拟合上图,然后根据定位返回的相关信息把由于拟合模板的点仿射变换到实时图像上,再进行定位。
read_image (Image16, 'C:/pma/16.bmp')
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, 96, 154, 174, 489)
*剪裁区域用于创建模板
reduce_domain (Image16, Rectangle, ImageReduced)
*创建模板并获取轮廓
create_scaled_shape_model (ImageReduced, 'auto', rad(-45), rad(90), 'auto', 0.5, 1.5, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
*查找模板
find_scaled_shape_model (Image16, ModelID, rad(-45), rad(90), 0.5, 1.5, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Scale1, Score1)
*显示结果
dev_display_shape_matching_results123 (Out_countours, ModelID, 'red', Row1, Column1, Angle1, Scale1, Scale1, 0)
* 获取用于拟合模板图像的点
* draw_spoke (Image16, Regions1, 3600, 15,25, 5, ROIRows, ROICols, Direct)
fit_Rows:=[182,212,256,297]
fit_Cols:=[460,440,433,452]
fit_Direct:= 'outer'
*把这些用于拟合圆形的点显示出来
gen_cross_contour_xld (Cross,fit_Rows, fit_Cols, 6, 0)
*把这些用于拟合点创建出一个区域用卡尺取点
spoke (Image16, Regions, 15, 25, 5, 1, 30, 'positive', 'first', fit_Rows,fit_Cols, fit_Direct, ResultRow, ResultColumn, ArcType)
*卡尺输出的点用来拟合圆形
pts_to_best_circle (Circle, ResultRow, ResultColumn, 5, 'circle', RowCenter, ColCenter, Radius, StartPhi, EndPhi, PointOrder)
* Code generated by Image Acquisition 01
list_files ('C:/pma', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima)$','ignore_case'], ImageFiles)
while(1)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*定位实时图像并显示结果
find_scaled_shape_model (Image, ModelID, rad(-45), rad(90), 0.5, 1.5, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Scale2, Score2)
dev_display_shape_matching_results123 (Out_countours, ModelID, 'red', Row2, Column2, Angle2, Scale2, Scale2, 0)
*根据定位模板图像和定位实时图像获取得到的信息(位移、缩放、角度)创建由模板图像到实时图像的仿射变换矩阵
vector_angle_to_rigid (Row1, Column1, Angle1,Row2, Column2, Angle2, HomMat2D)
hom_mat2d_scale (HomMat2D, Scale2, Scale2, Row2, Column2, HomMat2DScale)
*把用于拟合模板图像的点根据仿射变换矩阵仿射到实时图像上
affine_trans_point_2d (HomMat2DScale,fit_Rows, fit_Cols, Qx, Qy)
*显示出来
gen_cross_contour_xld (Cross,Qx, Qy, 6, 0)
*拟合实时图像
spoke (Image, Regions, 15, 25, 5, 1, 20, 'positive', 'first', Qx, Qy, 'outer', ResultRow, ResultColumn, ArcType)
pts_to_best_circle (Circle, ResultRow, ResultColumn, 5, 'circle', RowCenter, ColCenter, Radius, StartPhi, EndPhi, PointOrder)
endfor
endwhile