基于模板匹配的仿射变换,OCR

博客提及使用Halcon自带图片,涉及模板匹配和仿射变换等信息技术内容。Halcon在图像处理领域有广泛应用,模板匹配可用于目标识别等,仿射变换能对图像进行几何变换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片是halcon自带的图片

 

dev_update_off ()
dev_close_window ()
* 
read_image (ModelImage, 'cd_cover/cd_cover_01.png')
*打开窗口
dev_close_window ()
get_image_size (ModelImage, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (ModelImage)
*框选匹配模板区域
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, 'Please select model template', 'window', 30, 12, 'magenta', 'true')
dev_set_draw ('margin')
dev_set_line_width (1)
*可以通过框选region 或者 rectangle的方法
* draw_region (Region, WindowHandle)
draw_rectangle1 (WindowHandle, Row0, Column0, Row01, Column01)
gen_rectangle1 (RectangleM, Row0, Column0, Row01, Column01)
*获取模板的中心点
area_center (RectangleM, Area, RowM, ColumnM)
*框选感兴趣区域,可以选择多个
dev_set_color ('blue')
gen_empty_obj (RegionRoi)
disp_message (WindowHandle, 'Please select interest region', 'window', 30, 12, 'magenta', 'true')
for Index := 1 to 1 by 1
    draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
    gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
    *把框选的区域合并到RegionRoi中
    concat_obj (RegionRoi, Rectangle, RegionRoi)
endfor

reduce_domain (ModelImage, RectangleM, ImageReducedM)
*提取目标轮廓
inspect_shape_model (ImageReducedM, ModelImages, ModelRegions, 3, 30)
*gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter')

*创建模板,提取轮廓,轮廓的中心点在左上角  
create_shape_model (ImageReducedM, 2, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID)
*得到提取的轮廓,轮廓的中心点为MP(0,0)
get_shape_model_contours (ModelCont, ModelID, 1)

ImageFiles := 'cd_cover/cd_cover_'
for I := 1 to 4 by 1
    dev_set_line_width (1)
    * 
    * Read the search image and search the best
    * match of the shape model in the search image
    read_image (SearchImage, ImageFiles + I$'.2d')
    find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 0, 1, RowMatch, ColumnMatch, AngleMatch, Score)
    * 
    * If a match was found, rectify the results
    if (|Score| > 0)
        *获取从模型轮廓的中心点(0,0)到寻找到的目标中心点的旋转矩阵,用于在目标上显示轮廓
        vector_angle_to_rigid (0, 0, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfModel)
        *获取从框选模型的中心点(RowM, ColumnM)到寻找到的目标中心点的旋转矩阵,用于在新图片上显示感兴趣区域
        *使用hom_mat2d_invert,根据得到的旋转矩阵,得出把新图片转正的矩阵
        vector_angle_to_rigid (RowM, ColumnM, 0, RowMatch, ColumnMatch, AngleMatch, MovementOfObject)
        *显示识别的模板区域
        affine_trans_contour_xld (ModelCont, ModelAtNewPosition, MovementOfModel)
        *显示目标区域
        affine_trans_region (RegionRoi, NumberROIAtNewPosition, MovementOfObject, 'nearest_neighbor')
        * Display the model ROI and the ROI of the numbers at the (new)
        * position in which the match was found in the current search image
        dev_display (SearchImage)
        dev_set_color ('blue')
        dev_display (ModelAtNewPosition)
        dev_set_color ('magenta')
        dev_set_line_width (2)
        dev_display (NumberROIAtNewPosition)
        *OCR识别
        *
        *抠出转正的感兴趣区域有两种方法,
        *一种利用hom_mat2d_invert,根据旋转后的矩阵,得出旋转回去的矩阵
        *一种利用orientation_region得出感兴趣区域的旋转角度        
        *方法1
        hom_mat2d_invert (MovementOfObject, InverseMovementOfObject)
        affine_trans_image (SearchImage, ImageAffineTrans, InverseMovementOfObject, 'constant', 'false')
        reduce_domain (ImageAffineTrans, RegionRoi, ImageReduced1)     
        *方法2
         reduce_domain (SearchImage, NumberROIAtNewPosition, ImageReducedOCR)
        *求图像的旋转角度,如果Phi是负值,vector_angle_to_rigid参数旋转到0,如果是正值,参数旋转到π
        orientation_region (ImageReducedOCR, Phi)
        *求图像的中心点,即旋转中心点
        area_center (ImageReducedOCR, Area, Row, Column)
        *得到把图像旋转为正的向量HomMat2D
        vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
        *把向量作用到SearchImage上
        affine_trans_image (SearchImage, ImageAffineTrans, HomMat2D, 'constant', 'false')
        *把向量作用到ImageReducedOCR上
        affine_trans_region (ImageReducedOCR, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
        *在转正的图片里抠出转正的区域
        reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced1)
        
        
        *识别
        threshold (ImageReduced1, Regions, 0, 104)
        connection (Regions, ConnectedRegions)
        *形成矩形框
        shape_trans (ConnectedRegions, RegionTrans, 'rectangle1')
        *求交集,和二值化之后的区域求交集
        intersection (RegionTrans, Regions, RegionIntersection)
        *排序
        sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
        *读取训练库
        read_ocr_class_mlp ('DotPrint_0-9A-Z_NoRej.omc', OCRHandle)
        *第二个参数需要字体是暗的,背景是亮的图片
        do_ocr_multi_class_mlp (SortedRegions, ImageAffineTrans, OCRHandle, Class, Confidence)
    endif
endfor

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huangle63

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值