形状匹配之detect_brochure_pages.hdev

此示例介绍了一种在图片数据库中精确查找并定位文章页码的方法,通过训练不同页面模型,再在未知图像中搜索匹配,实现快速准确的页码检测。

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

*这个例程演示了在图片数据库中寻找文章的页码
*第一步:不同页被训练,并模型内创建,之后,就在未知的图像中搜寻类似的文章。
*因为训练数据很大,所以需要一些内存

  • This example finds pages of articles in a picture database.
  • In the first step different pages are trained and models are created.
  • Afterwards unknown images are searched and the correct article
  • pages are detected.
  • Please notice that this example needs some memory to train the models.

*滚啊比更新
dev_update_off ()
*关闭窗体
dev_close_window ()
*读取图像
read_image (Image, ‘brochure/brochure_page_01’)
*获取图像尺寸
get_image_size (Image, Width, Height)
*打开适合图像尺寸的窗体
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*设置窗体字体,会影响窗体尺寸
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
*设置填充方式
dev_set_draw (‘margin’)
*显示图像
dev_display (Image)

  • 清除所有创建的描述符模型数据
  • Clear all already created descriptor models.
    ModelIDs := []
    ModelsFound := 0
    NumPoints := []
    NumModels := 3
    TotalTime := 0
  • 创建可视区域
  • Create region for visualization purpose.
    RowRoi := [10,10,Height - 10,Height - 10]
    ColRoi := [10,Width - 10,Width - 10,10]
    *生成矩形
    gen_rectangle1 (Rectangle, 10, 10, Height - 10, Width - 10)
    *显示信息
    disp_message (WindowHandle, [‘Press ‘Run’ to start model creation …’,’(may take a few minutes)’], ‘window’, 10, 10, ‘black’, ‘true’)
    *右下角显示“PRESS F5 TO CONTINUE”
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • 为每一页创建描述符模型
  • For every page the descriptor model is created.
    for Index := 1 to NumModels by 1
    *读取图像
    read_image (Image, ‘brochure/brochure_page_’ + Index$’.2’)
    *转为灰度图像
    rgb1_to_gray (Image, ImageGray)
    *获取图像尺寸
    get_image_size (ImageGray, Width, Height)
    *裁剪区域
    reduce_domain (ImageGray, Rectangle, ImageReduced)
    *清除窗体
    dev_clear_window ()
    *显示灰度图像
    dev_display (ImageGray)
    *显示创建第几个模型的信息
    disp_message (WindowHandle, ‘Creating model no. ’ + Index + ‘/’ + NumModels + ’ … please wait.’, ‘window’, 10, 10, ‘black’, ‘true’)
    • 用默认设置参数创建基于描述符的模型
    • Create the descriptor model with default parameters (except scaling)
      *为了更快检测,参数选用哈里斯二项式角点检测
    • For a fast detection, the harris binomial point detector is chosen.
      *计时1开始
      count_seconds (Seconds1)
      *创建描述符模型
      create_uncalib_descriptor_model (ImageReduced, ‘harris_binomial’, [], [], [‘min_rot’,‘max_rot’,‘min_scale’,‘max_scale’], [-90,90,0.2,1.1], 42, ModelID)
      count_seconds (Seconds2)
      *计时2
      *计算时间差
      TotalTime := TotalTime + (Seconds2 - Seconds1)
    • 为了后续矩形的投影正确,modelID中心应该放在图像原点
    • For the correct projection of the rectangles in a later step the origin
    • of the model has to be set to the image origin
      *设置模型原点
      set_descriptor_model_origin (ModelID, -Height / 2, -Width / 2)
      *模型集合
      ModelIDs := [ModelIDs,ModelID]
    • 存储从模型中提取出来的点位
    • Store the points which are extracted from the model for later matching.
      *提取点位
      get_descriptor_model_points (ModelID, ‘model’, ‘all’, Row_D, Col_D)
      *把点位存到数组中
      NumPoints := [NumPoints,|Row_D|]
      endfor
  • 创建模型完成
  • Model creation finished.
    *显示灰度图像
    dev_display (ImageGray)
    *显示创建了几个模型
    disp_message (WindowHandle, NumModels + ’ models created in ’ + TotalTime$’.4’ + ’ seconds.’, ‘window’, 10, 10, ‘black’, ‘true’)
    *右下角显示“PRESS F5 TO CONTINUE”
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • 由于图像尺寸有变化,所以需要再次初始化窗体
  • Initialize the window again, because the image size has changed.
    *读取图像
    read_image (Image, ‘brochure/brochure_01’)
    *打开适合图像尺寸的窗体
    dev_resize_window_fit_image (Image, 0, 0, -1, -1)
    *设置窗体字体,会影响窗体尺寸
    set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
  • 主循环
  • Main loop:
    *在所有图像中寻找模型
  • Search the models in all images
    for Index1 := 1 to 12 by 1
    OutputString := []
    NumMsgs := 0
    ModelsFound := 0
    TotalTime := 0
    *读取图像
    read_image (Image, ‘brochure/brochure_’ + Index1$’.2’)
    *转灰度图像
    rgb1_to_gray (Image, ImageGray)
    *显示图像
    dev_display (Image)
    *显示信息,搜寻中…
    disp_message (WindowHandle, ‘Searching image …’, ‘window’, 10, 10, ‘black’, ‘true’)
    • 在图像中搜寻每一个模型

    • Search every model in each image
      for Index2 := 0 to |ModelIDs| - 1 by 1

      • 默认参数,寻找模型
      • Find model (using default parameters)
        count_seconds (Seconds1)
        *寻找模型
        find_uncalib_descriptor_model (ImageGray, ModelIDs[Index2], ‘threshold’, 600, [‘min_score_descr’,‘guided_matching’], [0.003,‘on’], 0.25, 1, ‘num_points’, HomMat2D, Score)
        count_seconds (Seconds2)
        Time := Seconds2 - Seconds1
        TotalTime := TotalTime + Time
      • 找到的点的数量来判定找到结果是否正确
      • Check if the found instance is to be considered as a possible right match
      • depending on the number of points which were considered

      if ((|HomMat2D| > 0) and (Score > NumPoints[Index2] / 4))
      *获取找到的点
      get_descriptor_model_points (ModelIDs[Index2], ‘search’, 0, Row, Col)
      *生成十字叉
      gen_cross_contour_xld (Cross, Row, Col, 6, 0.785398)
      * 投影ROI和点
      * Project the ROI rectangle and points
      *投影区域
      projective_trans_region (Rectangle, TransRegion, HomMat2D, ‘bilinear’)
      *投影点
      projective_trans_pixel (HomMat2D, RowRoi, ColRoi, RowTrans, ColTrans)
      *求两条线的夹角
      angle_ll (RowTrans[2], ColTrans[2], RowTrans[1], ColTrans[1], RowTrans[1], ColTrans[1], RowTrans[0], ColTrans[0], Angle)
      Angle := deg(Angle)
      * * 找到的角度来判定找到结果是否正确
      * Check if the projected rectangle is to be considered as a right match
      * depending on the angle in the right upper edge.
      *如果角度在70-110
      if (Angle > 70 and Angle < 110)
      *求区域中心
      area_center (TransRegion, Area, Row, Column)
      ModelsFound := ModelsFound + 1
      *设置显示颜色
      dev_set_color (‘green’)
      *社会i线宽
      dev_set_line_width (4)
      *显示变换后的区域
      dev_display (TransRegion)
      *设置多色显示
      dev_set_colored (12)
      *设置线宽
      dev_set_line_width (1)
      *显示十字叉
      dev_display (Cross)
      *显示一些信息
      disp_message (WindowHandle, ‘Page ’ + (Index2 + 1), ‘window’, Row, Column, ‘black’, ‘true’)
      OutputString := [OutputString,‘Page ’ + (Index2 + 1) + ’ found in ’ + (Time * 1000)KaTeX parse error: Undefined control sequence: \n at position 12: '.4' + ' ms\̲n̲'] endif …’.4’ + ’ ms’,OutputString]
      disp_message (WindowHandle, OutputString, ‘window’, 10, 10, ‘black’, ‘true’)
      disp_continue_message (WindowHandle, ‘black’, ‘true’)
      stop ()
      endfor
      dev_display (ImageGray)
      disp_message (WindowHandle, ‘Program finished.\nPress ‘Run’ to clear all descriptor models.’, ‘window’, 10, 10, ‘black’, ‘true’)
      stop ()

  • 清除所有模型,释放内存
    for Index := 0 to |ModelIDs| - 1 by 1
    clear_descriptor_model (ModelIDs[Index])
    endfor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值