halcon 多车牌识别

文章目录

  • 车牌定位
  • 字符分割
  • 字符识别
  • 多张图片

车牌定位

dev_close_window()
dev_open_window(0,0,512,400,'black',WindowHandle)
list_image_files('C:/Users/Augustine/Desktop/车牌照图片','jpg',[],ImageFiles)
for i:=0 to |ImageFiles|-1 by 1
    read_image(Image, ImageFiles[i])
    *获取三色通道
    decompose3(Image, r, g, b)
    *将三色通道处理为hsv
    trans_from_rgb(r,g,b,h,s,v,'hsv')
    *按照s方式阈值处理
    threshold (s, Regions, 177, 251)
    *膨胀运算
    dilation_rectangle1(Regions, RegionDilation, 1, 3)
    connection(RegionDilation, ConnectedRegions)
    *选择特征
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 6639.49, 9284.42)
    *闭运算,先膨胀在腐蚀
    closing_rectangle1(SelectedRegions,RegionClosing,150, 200)
    *开运算
    opening_rectangle1(RegionClosing,RegionOpening,10,10)
    *获取最小外接矩形
    smallest_rectangle2(RegionOpening,Row,Column,Phi,Length1, Length2)
    *产生最小外界矩形
    gen_rectangle2(Rectangle,Row,Column,Phi,Length1,Length2)
    *获取数量
    count_obj(RegionOpening,Number)
    * 仿射变换
    a:=deg(Phi)
    if(a<-90)
        if(a<=-180)
            a:=180+a
            Phi:=rad(a)
        endif
    endif
    vector_angle_to_rigid(Row, Column, Phi, Row, Column, Phi, HomMat2D)
    affine_trans_region(Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
    affine_trans_image(Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    *裁剪
    reduce_domain(ImageAffineTrans,RegionAffineTrans,ImageReduced)
endfor

halcon 多车牌识别_第1张图片

字符分割

  *裁剪
    reduce_domain(ImageAffineTrans,RegionAffineTrans,ImageReduced)
    *做车牌处理,字符分割
    decompose3(ImageReduced,r,g,b)
    *阈值处理
    threshold(r,Region,140, 255)
    *去除噪声
    opening_rectangle1(Region, RegionOpening1, 1,2)
    connection(RegionOpening1, ConnectedRegions1)
    select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 91, 306.9)
    *排序
    sort_region(SelectedRegions1, SortedRegions, 'character', 'true', 'column')

字符识别

 count_obj(SortedRegions,Number)
    if(Number == 6)
        *反转图片
        invert_image(r,ImageInvert)
        *分类识别
        read_ocr_class_mlp('Industrial_0-9A-Z_Rej.omc', OCRHandle)
        do_ocr_multi_class_mlp(SortedRegions, ImageInvert, OCRHandle, Class, Confidence)
        *车牌中没有I,将识别的I改成1
        dev_clear_window()
        dev_display(Image)
        set_display_font(WindowHandle,50, 'mono', 'true', 'false')
        carNumber:=''
        for Index:=1 to |Class|-1 by 1
            data:=Class[Index]
            if(data=='I')
                data:=1
            endif
            carNumber:=carNumber+data
        endfor
    endif
    dev_set_color('red')
    set_tposition(WindowHandle,150,100)
    write_string(WindowHandle,carNumber)

halcon 多车牌识别_第2张图片

多张图片

dev_close_window()
dev_open_window(0,0,512,400,'black',WindowHandle)
list_image_files('C:/Users/Augustine/Desktop/车牌照图片','jpg',[],ImageFiles)
for i:=0 to |ImageFiles|-1 by 1
    read_image(Image, ImageFiles[i])
    *获取三色通道
    decompose3(Image, r, g, b)
    *将三色通道处理为hsv
    trans_from_rgb(r,g,b,h,s,v,'hsv')
    *按照s方式阈值处理
    threshold (s, Regions, 177, 251)
    *膨胀运算
    dilation_rectangle1(Regions, RegionDilation, 1, 3)
    connection(RegionDilation, ConnectedRegions)
    *选择特征
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 6639.49, 9284.42)
    *闭运算,先膨胀在腐蚀
    closing_rectangle1(SelectedRegions,RegionClosing,150, 200)
    *开运算
    opening_rectangle1(RegionClosing,RegionOpening,10,10)
     *获取数量
    count_obj(RegionOpening,Number)
    
    if(Number!=1)
        if(Number ==0)
            *换通道定位车牌,处理第二张
            threshold (s, Regions1, 108, 188)
            connection(Regions1, ConnectedRegions2)
            select_shape (ConnectedRegions2, SelectedRegions2, ['column','row','area'], 'and', [145.61,385.75,81.52], [335.57,411.74,10000])
            opening_rectangle1(SelectedRegions2, RegionOpening2, 2, 3)
            connection(RegionOpening2, ConnectedRegions3)
            select_shape (ConnectedRegions3, SelectedRegions3, ['row','column'], 'and', [376.792,145.61], [414.427,380.38])
            closing_circle(SelectedRegions3, RegionClosing1,3)
            closing_rectangle1(RegionClosing1, RegionClosing2, 15, 30)
            connection(RegionClosing2, ConnectedRegions4)
            select_shape (ConnectedRegions4, SelectedRegions4, 'area', 'and', 31.36, 3920.25)
            opening_rectangle1(SelectedRegions4, RegionOpening, 3, 2)
            erosion_circle(RegionOpening, RegionOpening, 1.5)
        endif
    endif
    
    
    *获取最小外接矩形
    smallest_rectangle2(RegionOpening,Row,Column,Phi,Length1, Length2)
    *产生最小外界矩形
    gen_rectangle2(Rectangle,Row,Column,Phi,Length1,Length2)
    * 仿射变换
    a:=deg(Phi)
    if(a<-90)
        if(a<=-180)
            a:=180+a
            Phi:=rad(a)
        endif
    endif
    vector_angle_to_rigid(Row, Column, Phi, Row, Column, Phi, HomMat2D)
    affine_trans_region(Rectangle, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
    affine_trans_image(Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    *裁剪
    reduce_domain(ImageAffineTrans,RegionAffineTrans,ImageReduced)
    *做车牌处理,字符分割
    decompose3(ImageReduced,r,g,b)
    *阈值处理
    threshold(r,Region,140, 255)
    *去除噪声
    opening_rectangle1(Region, RegionOpening1, 1,2)
    connection(RegionOpening1, ConnectedRegions1)
    select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 91, 306.9)
    *排序
    sort_region(SelectedRegions1, SortedRegions, 'character', 'true', 'column')
    
    count_obj(SortedRegions,Number)
    
    if(Number!=6)
        *第二张图片处理
    endif
    
    
    
    if(Number == 6)
        *反转图片
        invert_image(r,ImageInvert)
        *分类识别
        read_ocr_class_mlp('Industrial_0-9A-Z_Rej.omc', OCRHandle)
        do_ocr_multi_class_mlp(SortedRegions, ImageInvert, OCRHandle, Class, Confidence)
        *车牌中没有I,将识别的I改成1
        dev_clear_window()
        dev_display(Image)
        set_display_font(WindowHandle,50, 'mono', 'true', 'false')
        carNumber:=''
        for Index:=1 to |Class|-1 by 1
            data:=Class[Index]
            if(data=='I')
                data:=1
            endif
            carNumber:=carNumber+data
        endfor
    endif
    dev_set_color('red')
    set_tposition(WindowHandle,150,100)
    write_string(WindowHandle,carNumber)
endfor

你可能感兴趣的:(计算机视觉)