Choose any picture
After the rotation correction of this picture , The image becomes a template image . Its area region The location is as shown in the figure :
When a new picture comes , Let it relate to region Compare , There is obvious deviation from the position of the formwork , As shown in the figure :
Our goal is to make this new image coincide with the template area after processing . As shown in the figure below :
In fact, the overall function is similar to rotating matting , Only the functions used are a little different .
Code aggregation :
The image used in the code is :C:\Users\HJ\AppData\Roaming\MVTec\HALCON-21.05-Progress\examples\images\blister( Install it on your own halcon To find )
* Read a picture for processing , And take the processed figure as a template , Let the following pictures look like this one read_image (Image, 'C:/Users/HJ/Desktop/test_image/blister/blister_01.png') threshold(Image, Region, 100, 255) connection(Region, ConnectedRegions) select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 15000, 9999999) shape_trans(SelectedRegions, RegionTrans, 'convex') * Calculation region The direction of ( angle ),Phi Is the farthest point of the area and x Angle between axes orientation_region(RegionTrans, Phi) * Get the center of the area area_center(RegionTrans, Area, Row, Column) if ((1.57<Phi and Phi<3.142) or (-3.142<Phi and Phi<-1.57)) vector_angle_to_rigid(Row, Column, Phi, Row, Column, 3.14159, HomMat2D) else vector_angle_to_rigid(Row, Column, Phi, Row, Column, 0, HomMat2D) endif * Rotate pictures and areas affine_trans_image(Image, ImageAffineTrans, HomMat2D, 'constant', 'false') affine_trans_region(RegionTrans, RegionTrans, HomMat2D, 'nearest_neighbor') * Calculate the center of the area after rotation , That is, the center of the template area area_center (RegionTrans, AreaRef, RowRef, ColumnRef) list_files ('C:/Users/HJ/Desktop/test_image/blister', ['files','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) Width := 550 Height := 400 for Index := 0 to |ImageFiles| - 1 by 1 * Data preprocessing read_image (Image2, ImageFiles[Index]) threshold(Image2, Region_Each, 100, 255) connection(Region_Each, ConnectedRegions_Each) select_shape(ConnectedRegions_Each, SelectedRegions_Each, 'area', 'and', 15000, 9999999) shape_trans(SelectedRegions_Each, RegionTrans_Each, 'convex') * Calculate the direction of the current area ( angle ) orientation_region(RegionTrans_Each, Phi_Each) * Get the center of the current area area_center(RegionTrans_Each, Area_Each, Row_Each, Column_Each) if ((1.57<Phi_Each and Phi_Each<3.142) or (-3.142<Phi_Each and Phi_Each<-1.57)) * Use the center of the current area 、 angle and Calculate the center and angle of the template area , Generate a matrix of affine transformation vector_angle_to_rigid(Row_Each, Column_Each, Phi_Each, RowRef, ColumnRef, 3.14159, HomMat2D1) else vector_angle_to_rigid(Row_Each, Column_Each, Phi_Each, RowRef, ColumnRef, 0, HomMat2D1) endif * Adjust the picture according to the affine transformation matrix , Become the same as the template image affine_trans_image(Image2, ImageAffineTrans_Each, HomMat2D1, 'constant', 'false') endfor