35.8、仿射变换实例五(图像变换用于拟合圆、直线)
在前面的拟合圆、拟合直线中,draw_spoke、draw_rake时要输入一个固定坐标的圆形和直线。但在实际工程应用中,相机取得的图像不可能每张都是固定不变的,存在有平移、缩放、旋转。这时需要先进行定位,获取的特征点后再对图像进行仿射变换,使得每张图像都是固定不变的。
dev_update_window ('off')
*读入模板图像
read_image (Image1, 'C:/图片/1.bmp')
* draw_circle (3600, Row, Column, Radius)
gen_circle (Circle, 547, 566, 231)
reduce_domain (Image1, Circle, ImageReduced)
*创建模板并显示轮廓
create_shape_model (ImageReduced, 'auto', 0, 0, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
*定位模板图像
find_shape_model (Image1, ModelID, 0, 0, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1)
*显示定位结果
dev_display_shape_matching_results123 (Out_countours1, ModelID, 'red', Row1, Column1, Angle1, 1, 1, 0)
gen_cross_contour_xld (Cross1, Row1, Column1, 20, Angle1)
* draw_rectangle1 (3600, Row11, Column11, Row21, Column21)
* dev_set_draw ('margin')
* gen_rectangle1 (Rectangle, 878, 352, 1000, 764)
spoke (Image1, Regions3, 30, 60, 15, 1, 20, 'negative', 'first',[376,722,623,374,376], [674,668,374,459,674], 'outer', ResultRow1, ResultColumn1, ArcType)
* Code generated by Image Acquisition 01
list_files ('C:/图片', ['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)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
dev_display (Image)
stop ( )
* Do something
*定位实时图像
find_shape_model (Image, ModelID, 0, 0, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score2)
*显示结果
dev_display_shape_matching_results123 (Out_countours2, ModelID, 'red', Row2, Column2, Angle2, 1, 1, 0)
gen_cross_contour_xld (Cross2, Row2, Column2, 20, Angle2)
*根据实时图像和模板图像定位出来的特征点创建仿射变换矩阵,让实时图像往模板图像对齐靠近
vector_angle_to_rigid ( Row2, Column2, Angle2,Row1, Column1, Angle1, HomMat2D)
* affine_trans_region (Rectangle, RegionAffineTrans, HomMat2D, 'false')
*对图像进行仿射变换让实时图像往模板图像对齐靠近
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
*显示仿射变换后的图像
dev_clear_window ()
dev_display (ImageAffinTrans)
*拟合直线
* draw_rake (Regions, 3600, 15, 30, 10, Row12, Column12, Row22, Column22)
* rake (ImageAffinTrans, Regions1, 15, 30, 10, 1, 60, 'negative', 'first', 935, 372, 938, 742, ResultRow, ResultColumn)
* pts_to_best_line (Line, ResultRow, ResultColumn, 2, Row13, Column13, Row23, Column23)
* dev_display (Line)
*拟合圆
* draw_spoke (ImageAffinTrans, Regions2, 3600, 30, 60, 15, ROIRows, ROICols, Direct)
spoke (ImageAffinTrans, Regions3, 30, 60, 15, 1, 20, 'negative', 'first',[376,722,623,374,376], [674,668,374,459,674], 'outer', ResultRow1, ResultColumn1, ArcType)
pts_to_best_circle (Circle1, ResultRow1, ResultColumn1, 6, 'circle', RowCenter, ColCenter, Radius1, StartPhi, EndPhi, PointOrder)
dev_display (Circle1)
stop ( )
endfor