* This example program shows how the grid rectification
* can be used to generate arbitrary distortion maps based on
* self-defined grids.
*
* The example application is a print inspection.
* It is assumed that only distortions in the vertical direction
* of the printed document are present, which could,
* e.g., be caused by an inaccurate paper transport.
* These distortions are determined and applied
* to the reference image. Then the test image is compared
* with the distorted reference image.
*
* The example program uses component-based matching for the
* determination of the corresponding points in the test and
* the reference image. Then, it generates a distorted grid
* from which the rectification map is derived.
* 假定 只 在垂直方向上 的扭曲
* Read and preprocess images
dev_close_window ()
ImgPath := '3d_machine_vision/grid_rectification/'
read_image (ImageRef, ImgPath+'document_original')
get_image_size (ImageRef, WidthRef, HeightRef)
dev_open_window_fit_image (ImageRef, 0, 0, 600, 600, WindowHandleRef)
dev_display (ImageRef)
dev_open_window (0, 600*WidthRef/HeightRef+8, 50, 50, 'black', WindowHandleTest)
* 准备 确定 对应点 的 基于组件的 匹配
* Prepare the determine of corresponding points by component based matching
prepare_determination_of_corresponding_points (ImageRef, RootRanking, ComponentModelID, RowCompRef, ColumnCompRef, UpperRow, LowerRow, DRowUpper, DRowLower)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
*
* Online phase
for ImgNum := 1 to 2 by 1
read_image (ImageTest, ImgPath+'document_distorted_'+ImgNum$'02d')
get_image_size (ImageTest, WidthTest, HeightTest)
dev_open_window_fit_image (ImageRef, 0, 600*WidthRef/HeightRef+8, 600, 600, WindowHandleTest)
dev_display (ImageTest)
determine_corresponding_points (ImageTest, ComponentMarkers, DistortionMarkers, ComponentModelID, RootRanking, DRowUpper, DRowLower, UpperRow, LowerRow, ColumnCompRef, RowRef, ColRef, RowTest, ColTest)
dev_set_color ('green')
dev_display (ComponentMarkers)
dev_set_color ('orange red')
dev_display (DistortionMarkers)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
* 确定 扭曲的 网格 点的坐标
* Determine coordinates of distorted grid points
GridSpacing := 10
ColShift := mean(ColTest-ColRef)
RefGridColValues := []
for HelpCol := -0.5 to WidthTest+GridSpacing by GridSpacing
RefGridColValues := [RefGridColValues, HelpCol+ColShift]
endfor
MinValue := 0
MaxValue := HeightTest+GridSpacing
sample_corresponding_values (RowTest, RowRef-0.5, MinValue, MaxValue, GridSpacing, RefGridRowValues)
RefGridRow := []
RefGridCol := []
dev_update_off ()
Ones := gen_tuple_const(|RefGridColValues|, 1)
for r := 0 to |RefGridRowValues|-1 by 1
RefGridRow := [RefGridRow, RefGridRowValues[r]*Ones]
RefGridCol := [RefGridCol, RefGridColValues]
endfor
dev_update_on ()
*
gen_arbitrary_distortion_map (Map, GridSpacing, RefGridRow, RefGridCol, |RefGridColValues|, WidthRef, HeightRef, 'bilinear')
*
* Map test image and compare it to reference image
map_image (ImageRef, Map, ImageMapped)
crop_part (ImageMapped, ImagePart, 0, 0, WidthTest, HeightTest)
sub_image (ImagePart, ImageTest, ImageSub, 1, 128)
dev_display (ImageSub)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
*
* Visualize differences
MinDev := 100
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
endfor
clear_component_model (ComponentModelID)
dev_update_window ('on')
dev_set_draw ('fill')
dev_close_window ()
* can be used to generate arbitrary distortion maps based on
* self-defined grids.
*
* The example application is a print inspection.
* It is assumed that only distortions in the vertical direction
* of the printed document are present, which could,
* e.g., be caused by an inaccurate paper transport.
* These distortions are determined and applied
* to the reference image. Then the test image is compared
* with the distorted reference image.
*
* The example program uses component-based matching for the
* determination of the corresponding points in the test and
* the reference image. Then, it generates a distorted grid
* from which the rectification map is derived.
* 假定 只 在垂直方向上 的扭曲
* Read and preprocess images
dev_close_window ()
ImgPath := '3d_machine_vision/grid_rectification/'
read_image (ImageRef, ImgPath+'document_original')
get_image_size (ImageRef, WidthRef, HeightRef)
dev_open_window_fit_image (ImageRef, 0, 0, 600, 600, WindowHandleRef)
dev_display (ImageRef)
dev_open_window (0, 600*WidthRef/HeightRef+8, 50, 50, 'black', WindowHandleTest)
* 准备 确定 对应点 的 基于组件的 匹配
* Prepare the determine of corresponding points by component based matching
prepare_determination_of_corresponding_points (ImageRef, RootRanking, ComponentModelID, RowCompRef, ColumnCompRef, UpperRow, LowerRow, DRowUpper, DRowLower)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
*
* Online phase
for ImgNum := 1 to 2 by 1
read_image (ImageTest, ImgPath+'document_distorted_'+ImgNum$'02d')
get_image_size (ImageTest, WidthTest, HeightTest)
dev_open_window_fit_image (ImageRef, 0, 600*WidthRef/HeightRef+8, 600, 600, WindowHandleTest)
dev_display (ImageTest)
determine_corresponding_points (ImageTest, ComponentMarkers, DistortionMarkers, ComponentModelID, RootRanking, DRowUpper, DRowLower, UpperRow, LowerRow, ColumnCompRef, RowRef, ColRef, RowTest, ColTest)
dev_set_color ('green')
dev_display (ComponentMarkers)
dev_set_color ('orange red')
dev_display (DistortionMarkers)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
* 确定 扭曲的 网格 点的坐标
* Determine coordinates of distorted grid points
GridSpacing := 10
ColShift := mean(ColTest-ColRef)
RefGridColValues := []
for HelpCol := -0.5 to WidthTest+GridSpacing by GridSpacing
RefGridColValues := [RefGridColValues, HelpCol+ColShift]
endfor
MinValue := 0
MaxValue := HeightTest+GridSpacing
sample_corresponding_values (RowTest, RowRef-0.5, MinValue, MaxValue, GridSpacing, RefGridRowValues)
RefGridRow := []
RefGridCol := []
dev_update_off ()
Ones := gen_tuple_const(|RefGridColValues|, 1)
for r := 0 to |RefGridRowValues|-1 by 1
RefGridRow := [RefGridRow, RefGridRowValues[r]*Ones]
RefGridCol := [RefGridCol, RefGridColValues]
endfor
dev_update_on ()
*
stop()
*生成从扭曲的网格畸变的map
* Generate distortion map from distorted gridgen_arbitrary_distortion_map (Map, GridSpacing, RefGridRow, RefGridCol, |RefGridColValues|, WidthRef, HeightRef, 'bilinear')
*
* Map test image and compare it to reference image
map_image (ImageRef, Map, ImageMapped)
crop_part (ImageMapped, ImagePart, 0, 0, WidthTest, HeightTest)
sub_image (ImagePart, ImageTest, ImageSub, 1, 128)
dev_display (ImageSub)
disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
*
* Visualize differences
MinDev := 100
MinSize := 5
**可视化差异
visualize_differences (ImageSub, ImageTest, ComponentMarkers, DistortionMarkers, MinDev, MinSize, 1)disp_continue_message (WindowHandleRef, 'black', 'true')
stop ()
endfor
clear_component_model (ComponentModelID)
dev_update_window ('on')
dev_set_draw ('fill')
dev_close_window ()