35.9、仿射变换实例六(两个点仿射变换)
目标:根据两个点进行仿射变换,实现区域跟随。
一、实时图形不变,对ROI进行仿射变换
read_image (Image11, '1-1.bmp')
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle1, 169, 135, 346, 663)
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle2, 680, 667, 863, 1125)
*剪裁区域1,用于创建模板1
reduce_domain (Image11, Rectangle1, ImageReduced1)
*剪裁区域2,用于创建模板2
reduce_domain (Image11, Rectangle2, ImageReduced2)
*创建模板1
create_shape_model (ImageReduced1, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
*创建模板2
create_shape_model (ImageReduced2, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID2)
*获取模板轮廓
get_shape_model_contours (ModelContours1, ModelID1, 1)
get_shape_model_contours (ModelContours2, ModelID2, 1)
*查找多模板
find_shape_models (Image11, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1, Model1)
*如果两个模板都找到则显示结果
if( |Score1|>1)
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row1, Column1, Angle1, 1, 1, Model1)
endif
*创建要进行仿射变换的区域
* draw_rectangle2 (3600, Row3, Column3, Phi, Length1, Length2)
gen_rectangle2 (Rec, 238, 412, 0.196503, 168, 21)
* Code generated by Image Acquisition 02
list_files ('C:/11图片', ['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])
* 查找实时图像
find_shape_models (Image, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score2, Model2)
*如果两个模板都找到则显示结果
if( |Score2|>1)
*显示结果
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row2, Column2, Angle2, 1, 1, Model2)
if(Model2[0]=0)
*假如实时图像找到的模板和模板图像查找到的模板次序一样
vector_to_rigid (Column1, Row1, Column2, Row2, HomMat2D)
else
*假如实时图像找到的模板和模板图像查找到的模板次序相反,则调动点的次序
vector_to_rigid (Row1,Column1,[Row2[1],Row2[0]],[ Column2[1],Column2[0]] , HomMat2D)
endif
*利用创建好的反射矩阵对矩形进行仿射变换跟随
affine_trans_region (Rec, RegionAffineTrans, HomMat2D, 'false')
endif
endfor
二、ROI不变,对实时图形进行仿射变换
分析:只是把上面第一中的计算仿射变换矩阵vector_to_rigid算子中的坐标点兑换了位置,即改变了它的方向。计算出仿射变换矩阵后再对实时图像进行仿射变换,使实时图像向模板图像对齐。
read_image (Image11, '1-1.bmp')
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle1, 169, 135, 346, 663)
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle2, 680, 667, 863, 1125)
*剪裁区域1,用于创建模板1
reduce_domain (Image11, Rectangle1, ImageReduced1)
*剪裁区域2,用于创建模板2
reduce_domain (Image11, Rectangle2, ImageReduced2)
*创建模板1
create_shape_model (ImageReduced1, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
*创建模板2
create_shape_model (ImageReduced2, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID2)
*获取模板轮廓
get_shape_model_contours (ModelContours1, ModelID1, 1)
get_shape_model_contours (ModelContours2, ModelID2, 1)
*查找多模板
find_shape_models (Image11, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1, Model1)
*如果两个模板都找到则显示结果
if( |Score1|>1)
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row1, Column1, Angle1, 1, 1, Model1)
endif
*创建要进行仿射变换的区域
* draw_rectangle2 (3600, Row3, Column3, Phi, Length1, Length2)
gen_rectangle2 (Rec, 238, 412, 0.196503, 168, 21)
* Code generated by Image Acquisition 02
list_files ('C:/11图片', ['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])
* 查找实时图像
find_shape_models (Image, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score2, Model2)
*如果两个模板都找到则显示结果
if( |Score2|>1)
*显示结果
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row2, Column2, Angle2, 1, 1, Model2)
if(Model2[0]=0)
*假如实时图像找到的模板和模板图像查找到的模板次序一样
vector_to_rigid ( Column2, Row2,Column1, Row1, HomMat2D)
else
*假如实时图像找到的模板和模板图像查找到的模板次序相反,则调动点的次序
vector_to_rigid ([Row2[1],Row2[0]],[ Column2[1],Column2[0]] ,Row1,Column1, HomMat2D)
endif
*利用创建好的反射矩阵对图像进行仿射变换
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
dev_display (Rec)
endif
endfor
三、仿射变换后可以用于拟合直线
read_image (Image11, 'C:/11图片/1-1.bmp')
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle1, 169, 135, 346, 663)
* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle2, 680, 667, 863, 1125)
*剪裁区域1,用于创建模板1
reduce_domain (Image11, Rectangle1, ImageReduced1)
*剪裁区域2,用于创建模板2
reduce_domain (Image11, Rectangle2, ImageReduced2)
*创建模板1
create_shape_model (ImageReduced1, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
*创建模板2
create_shape_model (ImageReduced2, 'auto', rad(-180), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID2)
*获取模板轮廓
get_shape_model_contours (ModelContours1, ModelID1, 1)
get_shape_model_contours (ModelContours2, ModelID2, 1)
*查找多模板
find_shape_models (Image11, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1, Model1)
*如果两个模板都找到则显示结果
if( |Score1|>1)
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row1, Column1, Angle1, 1, 1, Model1)
endif
*创建要进行仿射变换的区域
* draw_rectangle2 (3600, Row3, Column3, Phi, Length1, Length2)
gen_rectangle2 (Rec, 238, 412, 0.196503, 168, 21)
* draw_rake (Regions, 3600, 20, 60, 10, Row11, Column11, Row21, Column21)
rake_row1:=276
rake_col1:=281
rake_row2:=218
rake_col2:=557
rake (Image11, Regions1, 20, 60, 10, 1, 20, 'positive', 'first', rake_row1, rake_col1, rake_row2, rake_col2, ResultRow, ResultColumn)
pts_to_best_line (Line, ResultRow, ResultColumn, 10, Row12, Column12, Row22, Column22)
* Code generated by Image Acquisition 02
list_files ('C:/11图片', ['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])
* 查找实时图像
find_shape_models (Image, [ModelID1,ModelID2], rad(-180), rad(360), 0.5, 2, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score2, Model2)
*如果两个模板都找到则显示结果
if( |Score2|>1)
*显示结果
dev_display_shape_matching_results ( [ModelID1,ModelID2], 'red', Row2, Column2, Angle2, 1, 1, Model2)
if(Model2[0]=0)
*假如实时图像找到的模板和模板图像查找到的模板次序一样
vector_to_rigid (Column1, Row1, Column2, Row2, HomMat2D)
else
*假如实时图像找到的模板和模板图像查找到的模板次序相反,则调动点的次序
vector_to_rigid (Row1,Column1,[Row2[1],Row2[0]],[ Column2[1],Column2[0]] , HomMat2D)
endif
*利用创建好的反射矩阵对矩形进行仿射变换跟随
affine_trans_region (Rec, RegionAffineTrans, HomMat2D, 'false')
affine_trans_point_2d (HomMat2D, [rake_row1,rake_row2],[rake_col1,rake_col2], Qx, Qy)
rake (Image, Regions1, 90, 60, 10, 1, 20, 'positive', 'first', Qx[0], Qy[0], Qx[1], Qy[1], ResultRow, ResultColumn)
pts_to_best_line (Line, ResultRow, ResultColumn, 10, Row12, Column12, Row22, Column22)
stop()
endif
endfor