Template image : The goal is to get the number of the upper left corner of the image
Direct thinking , Directly use a box to frame the number , Then template match the image ( Is not workable , Because the number in the image is not fixed )
So we need to choose the fixed area in the image as the template , Then find our target area according to the template area , The trademark name in the upper left corner of the case is used as the template area
Code : The case picture is in C:\Users\HJ\AppData\Roaming\MVTec\HALCON-21.05-Progress\examples\images\blister( Install it on your own halcon To find )
* Read template image , Understand the target requirements : Get the number in the upper left corner of the picture and recognize it read_image (TemplateImage, 'C:/Users/HJ/Desktop/test_image/cd_cover/cd_cover_01.png') get_image_size (TemplateImage, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_display(TemplateImage) * The first thought must be : Since you are looking for numbers , Just match the template directly and find the number , But template matching looks for areas similar to the template in the image , But the numbers will obviously change . * So we need to find all fixed areas on the image that will not change as templates , And find the center of this region , In this example, the trademark name in the upper left corner is used as the template area draw_rectangle1(WindowHandle, Row1, Column1, Row2, Column2) gen_rectangle1(NumberRectangle, Row1, Column1, Row2, Column2) reduce_domain(TemplateImage, NumberRectangle, TemplateImageReduced) area_center(NumberRectangle, Area, CenterModelROIRow, CenterModelROIColumn) * After selecting the template area , Our target area ( Numbers ) Below the template area , So use a box to frame the target area * In this way, we can know the relationship between the target area and the template area , In the subsequent template matching , After matching to the template area , The target region can be found by affine transformation gen_rectangle1 (NumberROI, Row2, Column1, Row2 + 30, Column2) * Create a shape Model , Angle range 0-rad(360) create_shape_model(TemplateImageReduced, 'auto', 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID) get_shape_model_contours(ShapeModel, ModelID, 1) ImageFiles := 'cd_cover/cd_cover_' for I := 1 to 4 by 1 read_image(SearchImage, ImageFiles + I$'.2d') find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.7, 'least_squares', 0, 1, RowMatch, ColumnMatch, AngleMatch, Score) if (|Score| > 0) * First, affine transform the template area , Find the template area in the picture vector_angle_to_rigid (0, 0, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfModel) affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfModel) * Make an affine transformation , Obtain the affine transformation matrix of the template area , Because the result of template matching defaults to (0,0) As the origin , So you need to map to the position in the actual picture vector_angle_to_rigid (CenterModelROIRow, CenterModelROIColumn, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfObject) affine_trans_region(NumberROI, RegionAffineTrans, MovementOfObject, 'nearest_neighbor') * After obtaining the target area , Find the target area ( matrix ) The inverse of , Then do matrix operation with the inverse matrix and the original graph , Generate new images hom_mat2d_invert(MovementOfObject, HomMat2DInvert) affine_trans_image(SearchImage, RectifiedSearchImage, HomMat2DInvert, 'constant', 'false') * The generated image is rotated correctly , The location of the area is the same as that of the template image , So you can get the front out target area reduce_domain(RectifiedSearchImage, NumberROI, RectifiedNumberROIImage) endif endfor clear_shape_model(ModelID)