Halcon OCV Detection

1. Compared with Halcon, Visionpro, VM and other algorithm platforms. It is found that the effect and output results of the OCV algorithm are similar. Both train the OCV model in advance and output the score at runtime.
2. The following summarizes Halcon's OCV algorithm and optimizes it. The official case of Halcon is to verify a single character area, and can only output the score of a single character. If there are many characters, you need to create more than N OCV models, which is not suitable for industrial projects. I do a little integration, use a model, check all characters, and output the score of each character.

read_image (Image, 'fonts/arial_a1')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2 + 42, 'black', WindowHandle)
dev_set_part (-84, 0, Height - 1, Width - 1)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
dev_set_draw ('margin')
*创建空图像数组,用于OCV训练
gen_empty_obj (ImgArray)
*创建待训练的字符数组
OcvText:=['A','B','C','D','E']
*创建OCV模型
create_ocv_proj (OcvText, OCVHandle)

dev_set_color ('red')
read_image (Image, 'fonts/arial_a' + 1)
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
for I := 1 to 5 by 1
    select_obj (SortedRegions, ObjectSelected, I)
    shape_trans (ObjectSelected, RegionTrans, 'rectangle1')
    dilation_rectangle1 (RegionTrans, RegionDilation, 15, 15)
    reduce_domain (Image, RegionDilation, ImageReduced)
    concat_obj (ImgArray, ImageReduced, ImgArray)
endfor
*训练OCV模型
traind_ocv_proj (ImgArray, OCVHandle, OcvText, 'single')
*
*运行测试训练好的OCV模型
gen_empty_obj (RegionArrayTest)
gen_empty_obj (ImgArrayTest)
for I := 1 to 9 by 1
    gen_empty_obj (ImgArrayTest)
    gen_empty_obj (RegionArrayTest)
    read_image (Image, 'fonts/arial_a' + I)
    binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
    sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'row')
    for J := 1 to 5 by 1
        select_obj (SortedRegions, ObjectSelected, J)
        shape_trans (ObjectSelected, RegionTrans, 'rectangle1')
        dilation_rectangle1 (RegionTrans, RegionDilation, 15, 15)
        reduce_domain (Image, RegionDilation, ImageReduced)
        concat_obj (ImgArrayTest, ImageReduced, ImgArrayTest)
        concat_obj (RegionArrayTest, RegionDilation, RegionArrayTest)
    endfor
    *字符核验
    do_ocv_simple (ImgArrayTest, OCVHandle, OcvText, 'true', 'true', 'true', 'true', 5, Quality)

    * Display quality
    if (Quality > 0.9)
        Color := 'green'
    elseif (Quality > 0.7)
        Color := 'yellow'
    else
        Color := 'red'
    endif
    dev_display (Image)
    dev_set_color (Color)
    dev_set_line_width (2)
    dev_display (RegionArrayTest)
    disp_message (WindowHandle, 'Check print quality of \'A\' (Image  ' + I + ' of 9)', 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandle, 'Quality = ' + Quality$'.2f', 'image', 120, 12, 'black', Color)
    if (I < 9)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif

endfor

insert image description here
insert image description here
insert image description here

おすすめ

転載: blog.csdn.net/Douhaoyu/article/details/130646305