halcon学习-二维码识别过程中对参数的选择以及对畸变的处理学习日志及遇到的问题

写在前面:

本日志内容参考资料如下:

1.set_data_code_2d_param参数的选择 https://blog.csdn.net/cherish_now_forever/article/details/72870598
2.提高解码能力的其他措施 https://www.cnblogs.com/xh6300/p/10492110.html
十分感谢原博主,如有侵权,联系删除。

遇到的问题:

1.set_data_code_2d_param参数’mirrored’,‘version’,'module_gap’我不知道什么意思
2.在演示了如何读取倾斜的2d数据代码预处理与纠正程序中
遇到的问题:hom_vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners,[1,1,1,1], [70,270,270,70], [100,100,300,300], [1,1,1,1], ‘normalized_dlt’, HomMat2D1)这PW QX QY QW四个坐标是怎么得到的呢??
希望有大神能够帮我解答一下。不胜感激。

内容:

一、以下例程通过灰度值形状腐蚀,从而找到和解码二维数据代码符号。
gray_erosion_shape(Image : ImageMin : MaskHeight, MaskWidth, MaskShape : )确定选定掩模内的最小灰度值。

gray_erosion_shape计算每个图像点形状MaskShape、垂直大小 MaskHeight和水平大小MaskWidth掩码内输入图像图像的最小灰度值。生成的图像在ImageMin中返回。

如果参数MaskHeight或MaskWidth类型为偶整数,则将它们更改为下一个较大的奇数值。相反,如果这两个参数中至少有一个是float类型,则对输入图像图像进行下一个较大和下一个较小的奇掩码大小的转换,并从这两个中间图像中插入输出图像ImageMin。因此,注意gray_erosion_shape对于掩码大小返回不同的结果,例如,4和4.0。

dev_update_off ()
dev_close_window ()
read_image (Image, 'datacode/ecc200/ecc200_to_preprocess_003')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')
dev_display (Image)
Message := 'This program demonstrates how to'
Message[1] := 'preprocess data code symbols with'
Message[2] := 'large gaps between the modules using'
Message[3] := 'gray value morphology.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
 *将参数设置为允许的最大模块间隙
create_data_code_2d_model('Data Matrix ECC 200',['module_gap_min','module_gap_max'], ['no','big'], DataCodeHandle1) 
find_data_code_2d(Image, SymbolXLDs1, DataCodeHandle1, [], [], ResultHandles1, DecodedDataStrings1)

gray_erosion_shape(Image, ImageMin1, 15, 15, 'rectangle')
find_data_code_2d(ImageMin1, SymbolXLDs2, DataCodeHandle1, [], [], ResultHandles2, DecodedDataStrings2)

disp_message (WindowHandle, 'Decoded string: ' + DecodedDataStrings2, 'window', 80, 12, 'black', 'true')
clear_data_code_2d_model(DataCodeHandle1)

处理前

处理后

二、以下例程二维码主要通过gray_opening_shape 对二维码进行给定掩模的开运算,由于二维码上有一定的椒盐噪声,我们可以使用中值滤波来平滑它。

gray_opening_shape(Image : ImageOpening : MaskHeight, MaskWidth, MaskShape : ) 相当于先gray_erosion_shape再gray_dilation_shape

对具有形状MaskShape结构元素的输入图像应用灰度值打开。掩模的偏移量为0,其水平和垂直大小由MaskHeight和MaskWidth定义。生成的图像在ImageOpening中返回。

如果参数MaskHeight或MaskWidth类型为偶整数,则将它们更改为下一个较大的奇数值。相反,如果这两个参数中至少有一个是float类型,则对输入图像图像进行下一个较大和下一个较小的奇掩码大小的转换,并从这两个中间图像中插值输出图像ImageOpening。因此,注意gray_opening_shape对于掩码大小返回不同的结果,例如,4和4.0。(这一点与gray_erosion_shape相同)

对于MaskShape控制参数的值“菱形”和“八边形”,MaskHeight和MaskWidth必须相等。MaskShape的参数值“八角形”表示一个等边八角形掩模,它是圆形结构的一个合适近似。在图像的边界处,灰度值被镜像。

dev_update_off ()
dev_close_window ()
read_image (Image, 'datacode/ecc200/ecc200_to_preprocess_004')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')
dev_display (Image)
Message := 'This program demonstrates the'
Message[1] := 'preproccessing of a data code symbol'
Message[2] := 'with gaps, modules of different color'
Message[3] := 'and a distorted quiet zone.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

create_data_code_2d_model('Data Matrix ECC 200', 'default_parameters', 'enhanced_recognition', DataCodeHandle1)
set_data_code_2d_param(DataCodeHandle1, ['module_gap_min','module_gap_max'], ['no','big'])
find_data_code_2d(Image, SymbolXLDs1, DataCodeHandle1, [], [], ResultHandles1, DecodedDataStrings1)
gray_opening_shape(Image, ImageOpening1, 7, 7, 'octagon')
find_data_code_2d(ImageOpening1, SymbolXLDs2, DataCodeHandle1, [], [], ResultHandles2, DecodedDataStrings2)
median_image(ImageOpening1, ImageMedian1, 'circle', 1, 'mirrored')
find_data_code_2d(ImageMedian1, SymbolXLDs3, DataCodeHandle1, [], [], ResultHandles3, DecodedDataStrings3)
disp_message (WindowHandle, 'Preprocessing:  Median filtering', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Decoded string: ' + DecodedDataStrings3, 'window', 40, 12, 'black', 'true')
stop ()
clear_data_code_2d_model(DataCodeHandle1)

三、这个程序演示了如何读取倾斜的2d数据代码预处理与纠正
遇到的问题:hom_vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners,[1,1,1,1], [70,270,270,70], [100,100,300,300], [1,1,1,1], ‘normalized_dlt’, HomMat2D1)
这PW QX QY QW四个坐标是怎么得到的呢??

dev_update_off ()
dev_close_window ()
* Get the image and display it
read_image (Image_slanted, 'datacode/ecc200/ecc200_to_preprocess_001')
dev_open_window_fit_image (Image_slanted, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_color ('green')
dev_set_line_width (3)
Message := 'This program demonstrates how to preprocess'
Message[1] := 'a slanted 2d data code with rectification'
Message[2] := 'before reading the data code symbol.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Initialize coordinates
XCoordCorners := [130,225,290,63]
YCoordCorners := [101,96,289,269]
gen_cross_contour_xld(Cross, XCoordCorners, YCoordCorners, 6, 0.785398)
dev_display (Image_slanted)
dev_display (Cross)
hom_vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners,[1,1,1,1], [70,270,270,70], [100,100,300,300], [1,1,1,1], 'normalized_dlt', HomMat2D1)
projective_trans_image(Image_slanted, TransImage, HomMat2D1, 'bilinear', 'false', 'false')
create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle1)
find_data_code_2d(TransImage, SymbolXLDs1, DataCodeHandle1, [], [], ResultHandles1, DecodedDataStrings1)
disp_message (WindowHandle, DecodedDataStrings1, 'window', 350, 70, 'forest green', 'true')
clear_data_code_2d_model(DataCodeHandle1)

四、解码模型的参数
首先,为了获得高解码率,必须要使用如下函数建立解码模型
create_data_code_2d_model (‘QR Code’, ‘default_parameters’, ‘maximum_recognition’, DataCodeHandle)
同时,在建立解码模型之后,调用如下函数对模型进行参数的修正。
(1)其中最终要的参数之一是解码时长的设置,如果解码函数在一定时长范围内难以解出条码,直接将条码丢弃也是一种弃车保帅的明智之举。而如下参数设置函数能实现
set_data_code_2d_param (DataCodeHandle, ‘timeout’, 50)
如下函数能将解码时长限制在200ms内,这意味着,及时解不出条码,200ms之后制动放弃该条码的解析。
(2)就是条码极性的设置,所谓极性是指条码颜色和条码底色。如下函数设置指明,条码较亮,而底色较暗。如果实际图片与设置的极性吻合,就能有较好的检测的结果。注意:如果极性设置与实际相反,则会严重的降低解码率。
set_data_code_2d_param (DataCodeHandle, ‘polarity’, ‘light_on_dark’)
(3)条码的基本特征设置,二维码的基本特征包括码粒数、码粒像素大小。比如二维码码粒数,QRcode的条码每行大约有18个码粒。而每个码粒所占的像素大小需根据实际的情况来设置。注意:如下代码的参数需为偶数。
*码粒个数设置
set_data_code_2d_param (DataCodeHandle, ‘symbol_size_min’, 21)

set_data_code_2d_param (DataCodeHandle, ‘symbol_size_max’, 29)

*码粒像素设置

set_data_code_2d_param (DataCodeHandle, ‘module_size_min’, 10)

set_data_code_2d_param (DataCodeHandle, ‘module_size_max’, 14)

作者:Cherishnow1
来源:CSDN
原文:https://blog.csdn.net/cherish_now_forever/article/details/72870598

另外,当参数设置’contrast_tolerance’的值为’high’时,对标志的对比度比较不敏感,可以让图像对比度差异较低的图像识别出来。相反,为’low’时,对对比度敏感。
参数为’small_modules_robustness’为’ high’时,更能识别小的码粒。
参数为’strict_quiet_zon’且设置为yes时,可以抑制在较大数据代码中检测错误数据代码(假阳性)。
参数’mirrored’,‘version’,'module_gap’我不知道什么意思…求告诉
提高解码能力的其他措施

如果二维码图像预处理以后,仍旧解码困难或者解码率不高,那么可以通过以下措施进一步提高解码能力:

1、如果整张图信息太多,则可以先把二维码区域挖出来,使用reduce_domain和crop_domain算子,这样不仅可以降低解码难度,还可以减少解码时间。

2、当二维码很小的时候,可以尝试用zoom_image_factor放大了二维码图像。

3、create_data_code_2d_model (‘QR Code’, [], [], DataCodeHandleQR)

创建模型时,[ ]中不填内容,实际默认属性名是‘default_parameters’,默认属性值是‘standard_recognition’。
如果想大幅度提高解码成功率,可以将属性值置为‘enhanced_recognition’或者‘maximum_recognition’。注意:解码能力越强,解码时间越长。

4、find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, ‘train’, ‘all’, ResultHandles, DecodedDataStrings)
该算子中的GenParamNames、GenParamValues默认是空的,就是说直接找,找不到拉倒。如果是’train’,就是一面找一面调整模板参数。
默认情况下,它只会最多找到1个二维码。如果想找出更多的二维码(例如3个),可以这样:
find_data_code_2d (Image2, SymbolXLDs, DataCodeHandle, ‘stop_after_result_num’, 3, ResultHandles, DecodedDataStrings)
(资料来自https://www.cnblogs.com/xh6300/p/10492110.html)

五、本程序演示了如何为一类ECC200数据矩阵符号训练一个二维数据模型,以及如何在训练前后获得模型参数的值。
*此外,演示了如何将经过训练的数据代码模型写入文件,以及如何读取保存的数据代码模型,然后使用该模型来查找序列图像中的所有ECC200符号。

关键步骤:
1.配置参数
create_data_code_2d_model(‘Data Matrix ECC 200’, [], [], DataCodeHandle1)

query_data_code_2d_params(DataCodeHandle1, ‘get_model_params’, GenParamName1)
*获取给定2D数据代码模型中可在其他2D数据代码操作符中使用的泛型参数或对象的名称获取给定2D数据代码模型中可在其他2D数据代码操作符中使用的泛型参数或对象的名称。
在这里主要是为后面get_data_code_2d_param准备参数GenParamName1
get_data_code_2d_param(DataCodeHandle1, GenParamName1, ModelBeforeTraining)
获取描述2D数据代码模型的一个或多个参数的值。

2.开始训练
find_data_code_2d(Image, SymbolXLDs1, DataCodeHandle1, ‘train’, ‘all’, ResultHandles1, DecodedDataStrings1)*注意这里参数的改变,要选择’train’
get_data_code_2d_param(DataCodeHandle1, GenParamName1, ModelAfterTraining)

3.保存训练结果并清除训练的句柄
write_data_code_2d_model(DataCodeHandle1, ‘2d_data_code_model.dcm’)
clear_data_code_2d_model(DataCodeHandle1)

4.读取训练文件
read_data_code_2d_model(‘2d_data_code_model.dcm’, DataCodeHandle)

5.clear_data_code_2d_model (DataCodeHandle)

dev_update_off ()
dev_close_window ()
ImageFiles := 'datacode/ecc200/ecc200_cpu_0'
ImageNum := 16
read_image (Image, ImageFiles + '07')
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')
* 
* Display short description
* This program demonstrates the training of a 2d data code model
* and how to write the trained model into a file and also how to
* read the saved model from the file. The model is then used
* to find data codes in a sequence of images.
Message := 'This program demonstrates the training of a 2d data code'
Message[1] := 'model, how to write the trained model into a file, and'
Message[2] := 'also how to read the saved model from the file. The'
Message[3] := 'model is then used to find data codes in a sequence of'
Message[4] := 'images.'
Message[5] := 'Furthermore it is shown how to obtain the values of the'
Message[6] := 'model parameters before and after the training.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle1)
query_data_code_2d_params(DataCodeHandle1, 'get_model_params', GenParamName1)
get_data_code_2d_param(DataCodeHandle1, GenParamName1, ModelBeforeTraining)

read_image (Image, 'datacode/ecc200/ecc200_cpu_007')
find_data_code_2d(Image, SymbolXLDs1, DataCodeHandle1, 'train', 'all', ResultHandles1, DecodedDataStrings1)
dev_display (Image)
dev_display (SymbolXLDs1)
TitleMessage := 'Train on a dark image'
display_found_data_codes (SymbolXLDs1, WindowHandle, DecodedDataStrings1, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
read_image (Image, 'datacode/ecc200/ecc200_cpu_008')
find_data_code_2d(Image, SymbolXLDs1, DataCodeHandle1, 'train', 'all', ResultHandles1, DecodedDataStrings1)
dev_display (Image)
dev_display (SymbolXLDs1)
TitleMessage := 'Train on a dark image'
display_found_data_codes (SymbolXLDs1, WindowHandle, DecodedDataStrings1, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
get_data_code_2d_param(DataCodeHandle1, GenParamName1, ModelAfterTraining)
write_data_code_2d_model(DataCodeHandle1, '2d_data_code_model.dcm')
clear_data_code_2d_model(DataCodeHandle1)
read_data_code_2d_model('2d_data_code_model.dcm', DataCodeHandle)


for Index := 7 to 16 by 1
    read_image (Image, ImageFiles + Index$'.2d')
    dev_display (Image)
    * 
    * Find and decode the data codes and measure the runtime
    count_seconds (T1)
    find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
    count_seconds (T2)
    Time := 1000 * (T2 - T1)
    * 
    * Display the results
    TitleMessage := 'Image ' + (Index - 6) + ' of ' + (ImageNum - 6)
    ResultMessage := 'Data code found in ' + Time$'.1f' + ' ms'
    display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, ResultMessage, 'forest green', 'black')
    * 
    * Deactivate the following lines to run the program without breaks
    if (Index < ImageNum)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
* 
* Clear the 2d data code model
clear_data_code_2d_model (DataCodeHandle)
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值