halcon进行二维码检测

二 维 码 检 测 二维码检测

find_data_code_2d (ImageReduced, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)

使用halcon进行二维码检测的核心在于
1.图像的预处理(去除噪点等)
2.适当算子参数的调整


Halcon检测二维码流程

  1. create_data_code_2d_model:创建一个二维码数据class模型
  2. set_bar_code_param:设置参数
  3. find_data_code_2d:检测和读取图像中的二维数据代码符号
  4. clear_data_code_2d_model:删除2D数据代码模型并释放分配的内存。

获取中间结果

  1. get_data_code_2d_results
  2. get_data_code_2d_objects

create_data_code_2d_model ('QR Code', [], [], DataCodeHandle)
read_image (Image, ImageFiles + Index$'.2d')
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
* 1. Display the results
dev_display (Image)
dev_display (SymbolXLDs)
disp_message (WindowHandle, 'Image ' + Index + ' of ' + ImageNum, 'window', 12, 12, 'black', 'true')

  1. SymbolXLDs:检测出的二维码轮廓

create_data_code_2d_model (‘QR Code’, [], [], DataCodeHandle)

‘QR Code’:二维码的类型

标题

if (|DecodedDataStrings|==0)
    *设置字体颜色
    dev_set_color ('red')
    *设置文字大小
    set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
    *设置文字位置
    set_tposition (WindowHandle, 240, 120)
    *设置文字内容
    write_string (WindowHandle, 'NG')
    stop()
endif

二维码识别两个重要点:1.图像预处理;2.二维码算子参数调整


1.图像预处理

* 开运算,进行腐蚀,去除噪点
gray_opening_shape (Image, ImageOpening, 11, 11, 'octagon')

技巧1:掩膜

技巧2:中值滤波

* 中值滤波,去除噪点
median_image (Image, ImageMedian, 'circle', 1, 'mirrored')

2.二维码算子参数调整

create_data_code_2d_model ('QR Code', 'default_parameters', 'standard_recognition', DataCodeHandle)
create_data_code_2d_model ('QR Code', 'default_parameters', 'enhanced_recognition', DataCodeHandle)
create_data_code_2d_model ('QR Code', 'default_parameters', 'maximum_recognition', DataCodeHandle)
  1. ‘standard_recognition’:速度快,准确率低
  2. ‘enhanced_recognition’:速度慢,准确率高
  3. ‘maximum_recognition’:速度慢,准确率高

small_modules_robustness high
    find_data_code_2d (ImageScaled, SymbolXLDs, DataCodeHandle, ['train'], ['polarity'], ResultHandles, DecodedDataStrings)

  1. train
  2. all
  3. polarity

面对缩放

其他

reduce_domain (, , ImageReduced1)
count_seconds (Seconds)
try
dev_update_off ()
dev_close_window ()
dev_get_window (WindowHandle)
dev_open_window (0, 0, 760, 570, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')

************************* Step 1: Create a 2d data code model *************************
* 创建模型
create_data_code_2d_model ('Data Matrix ECC 200', [], [], DataCodeHandle)
* 查询模型参数名称
query_data_code_2d_params (DataCodeHandle, 'get_model_params', GenParamName)
* 获取模型训练前参数
get_data_code_2d_param (DataCodeHandle, GenParamName, ModelBeforeTraining)
read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)

************************* Step 2: Train the model *************************
list_files ('D:/File/二维码识别', 'files', Files)
* 读取图像  Files[Index]
for Index1 := 0 to |Files|-1 by 1
        * Files[Index1]
*         read_image (Image,'D:\\File\\二维码识别\\8号料_19.bmp')
*         stop()
       * 计算开始时间
       count_seconds (TStart)
       * 读取图像
       read_image (Image, Files[Index1])
       * 中值滤波,去噪
*        mean_image (Image, ImageMean, 5, 5)
       * 阈值分割
       threshold (Image, Region, 150, 255)
       * 连通
       connection (Region, ConnectedRegions)
       * 填充
       fill_up (ConnectedRegions, RegionFillUp)
       * 选取特征区域
       select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 20000, 999999999)
       * 区域相减l
       difference (Image, SelectedRegions, RegionDifference)
        
        
*         draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
*         gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
        reduce_domain (Image, RegionDifference, ImageReduced1)
        * 需要调整参数
        * 中值滤波,平滑去噪
        mean_image (ImageReduced1, ImageMean1, 7, 7)
        * 双边滤波,去噪
        bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], [])
        * gamma变换
        gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true')
        * 图像相乘,图像增强
        * 第一次判定
        mult_image (GammaImage, GammaImage, ImageResult, 0.0300, -500)
*         reduce_domain (Image, RegionDifference, Rectangle)
        * 训练
        find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
        * 多次判定
        if(|DecodedDataStrings| == 0)
            * 第二次判定
            mult_image (GammaImage, GammaImage, ImageResult, 0.0203, -500)
            find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
            if(|DecodedDataStrings| == 0)
                * 第三次判定
                 mult_image (GammaImage, GammaImage, ImageResult, 0.0220, -500)
                 find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
                if(|DecodedDataStrings| == 0)
                * 第四次判定
                 mult_image (GammaImage, GammaImage, ImageResult, 0.0360, -500)
                 find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
                   if(|DecodedDataStrings| == 0)
                      * 第五次判定
                       mult_image (GammaImage, GammaImage, ImageResult, 0.021, -500)
                       find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
                    endif
                endif
            endif
        endif
        
         if(|DecodedDataStrings| == 0)
               disp_message (WindowHandle, 'NG', 'window', 60, 12, 'red', 'false')
               stop()
*                dev_display (Image)
*                draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
*                gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
*                reduce_domain (Image, Rectangle, ImageReduced1)
                * 需要调整参数
                * 中值滤波,平滑去噪
*                 mean_image (ImageReduced1, ImageMean1, 5, 5)
                * 双边滤波,去噪
*                 bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], [])
                * gamma变换
*                 gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true')
                * 图像相乘,图像增强
                * 第一次判定 0.0212
*                 mult_image (GammaImage, GammaImage, ImageResult, 0.0202, -500)
*                 reduce_domain (Image, RegionDifference, Rectangle)
                * 训练
*                 find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)
                
         endif
       count_seconds (TFinish)
       dev_display (ImageResult)
       dev_display (SymbolXLDs)
       DuiringTime := (TFinish-TStart)*1000.0
       disp_message (WindowHandle, 'Time: '+DuiringTime, 'window', 30, 12, 'white', 'false')
       disp_message (WindowHandle, DecodedDataStrings, 'window', 60, 12, 'white', 'false')
       wait_seconds (1) 
       dev_clear_window ()
endfor
* read_image (Image, TargetPath)
* 中值滤波,去噪
* mean_image (Image, ImageMean, 5, 5)
* 阈值分割
* threshold (ImageMean, Region, 150, 255)
* 连通
* connection (Region, ConnectedRegions)
* 填充
* fill_up (ConnectedRegions, RegionFillUp)
* 选取特征区域
* select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 20000, 999999999)
* 区域相减l
* difference (Image, SelectedRegions, RegionDifference)
* 获取图像区域
* draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
* gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
* reduce_domain (Image, Rectangle, ImageReduced1)
* 需要调整参数
* 中值滤波,平滑去噪
* mean_image (ImageReduced1, ImageMean1, 7, 7)
* 双边滤波,去噪
* bilateral_filter (ImageMean1, ImageMean1, ImageBilateral,1, 5, [], [])
* gamma变换
* gamma_image (ImageBilateral, GammaImage, 0.5, 0.05, 0.5, 255, 'true')
* 图像相乘,图像增强
* 第一次判定
* mult_image (GammaImage, GammaImage, ImageResult, 0.0300, -500)
* reduce_domain (Image, RegionDifference, Rectangle)
* 训练
* find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)

* if(|DecodedDataStrings| == 0)
    * 第二次判定
*     mult_image (GammaImage, GammaImage, ImageResult, 0.0203, -500)
*     find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)
*     if(|DecodedDataStrings| == 0)
        * 第三次判定
*          mult_image (GammaImage, GammaImage, ImageResult, 0.0220, -500)
*          find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)
*         if(|DecodedDataStrings| == 0)
        * 第四次判定
*          mult_image (GammaImage, GammaImage, ImageResult, 0.0360, -500)
*          find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)
*            if(|DecodedDataStrings| == 0)
              * 第五次判定
*                mult_image (GammaImage, GammaImage, ImageResult, 0.0390, -500)
*                find_data_code_2d (ImageResult, SymbolXLDs, DataCodeHandle, ['train'], ['all'], ResultHandles, DecodedDataStrings)
    
*             endif
*         endif
*     endif
* endif



* get_data_code_2d_param (DataCodeHandle, GenParamName, ModelAfterTraining)
* 显示结果
* dev_clear_window ()
* ModelAdaption := (GenParamName + ':')$'-35' + ModelBeforeTraining$'15' + ' -> ' + ModelAfterTraining
* disp_message (WindowHandle, 'Model parameters before and after the training:', 'window', 12, 12, 'white', 'false')
* disp_message (WindowHandle, ModelAdaption, 'window', 60, 12, 'white', 'false')
* disp_continue_message (WindowHandle, 'black', 'true')
* stop()
************************* Step 3: Write the model into a file *************************
* write_data_code_2d_model (DataCodeHandle, '2d_data_code_model.dcm')

************************* Step 4: Read an existing 2d data code model *************************
* read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)
************************* Step 5: Read the data codes *************************

* list_files ('D:/File/二维码识别', 'files', Files)
* for Index := 0 to |Files|-1 by 1
    * 读取图像
*     read_image (Image, Files[Index])
    * 阈值分割
*     mean_image (Image, ImageMean, 5, 5)
*     threshold (ImageMean, Region, 150, 255)
    * 连通
*     connection (Region, ConnectedRegions)
    * 填充
*     fill_up (ConnectedRegions, RegionFillUp)
    * 选取特征区域
*     select_shape (RegionFillUp, SelectedRegions, 'area', 'and', 2000, 999999999)
    * 生产区域
*     gen_rectange1 (Rectangle, 0, 0, 3072, 4096)
    * 区域相减l
*     difference (Image, SelectedRegions, RegionDifference)
    * 获取图像区域
*     reduce_domain (Image, RegionDifference, ImageReduced)
    * 识别二维码
*     count_seconds (T1)
*     find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
*     count_seconds (T2)
    * 显示结果
*     dev_display (ImageReduced)
*     dev_display (SymbolXLDs)
*     ConsumeTime := (T2-T1)*1000.0
*     disp_message (WindowHandle, 'Image ' + 88 + ' of ' + Index, 'window', 12, 12, 'black', 'true')
*     disp_message (WindowHandle, 'Time:'+ConsumeTime, 'window', 38, 12, 'black', 'true')
*     disp_message (WindowHandle, '识别结果:'+DecodedDataStrings, 'window', 65, 12, 'black', 'true')
    * 存入本地txt
*     open_file ('time.txt', 'append', FileHandle)
*     fwrite_string (FileHandle, 'Time:'+ConsumeTime)
*     fnew_line (FileHandle)
*     close_file (FileHandle)
    * 判别字符串长度是否为0
*     if (|DecodedDataStrings|==0)
        *设置字体颜色
*         dev_set_color ('blue')
        *设置文字大小
*         set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
        *设置文字位置
*         set_tposition (WindowHandle, 240, 120)
        *设置文字内容
*         write_string (WindowHandle, 'NG')
*         stop()
*     endif

* stop()
    
* endfor
catch (Exception)
        *设置字体颜色
        dev_set_color ('red')
        *设置文字大小
        set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
        *设置文字位置
        set_tposition (WindowHandle, 240, 120)
        *设置文字内容
        write_string (WindowHandle, 'NG')
        stop()
endtry



你可能感兴趣的:(halcon)