C#-Halcon二维码识别

新手学习过程中看历程试着写的。

ihatetheqrcode函数

ihatetheqrcode(ImageIn : Image_Out, Code_Xld : Window_Handle, QrType, Model_Type, Affine_Model, Scal, Multiple, Offset, Time_Out, Noise, Gap : Strings)
参数
ImageIn (input_object)  image → object
输入图像
Image_Out (output_object)  object → object
Code_Xld (output_object)  object → object
输出二维码区域
Window_Handle (input_control)  integer → (tuple)
窗口句柄
QrType (input_control)  integer → (integer)
条码类型,一共8种
该值的一般区间: QrType ≤ 7
Model_Type (input_control)  integer → (integer)
创建二维码的匹配模式 1标准 2加强 3最强
该值的一般区间: 1 ≤ Model_Type ≤ 3
Affine_Model (input_control)  integer → (integer)
斜着扫码 1创建斜扫映射 此后两个tuple为元组 2读取模板进行映射 3不进行映射
该值的一般区间: 1 ≤ Affine_Model ≤ 3
Scal (input_control)  any → (tuple)
bool类型 表明明亮变化 后两个是变化参数
Multiple (input_control)  integer → (integer)
灰度值乘数 Scal若为false此项不启用
该值的一般区间: 0 ≤ Multiple ≤ 10
Offset (input_control)  integer → (integer)
灰度值偏移量 Scal若为false此项不启用
该值的一般区间: -1000 ≤ Offset ≤ 1000
Time_Out (input_control)  integer → (integer)
扫码超时时间 0-10000ms
该值的一般区间: 0 ≤ Time_Out ≤ 10000
Noise (input_control)  any → (tuple)
bool类型进行噪声削弱,用的不多
Gap (input_control)  any → (tuple)
bool类型Gap进行调整,用的不多
Strings (output_control)  string(-array) → (string)

返回扫到的码

函数Halcon源码如下

 SymbolType:=[]
 SymbolType:=[ SymbolType,'Aztec Code']
 SymbolType:=[ SymbolType,'Data Matrix ECC 200']
 SymbolType:=[ SymbolType,'GS1 Aztec Code']
 SymbolType:=[ SymbolType,'GS1 DataMatrix']
 SymbolType:=[ SymbolType,'GS1 QR Code']
 SymbolType:=[ SymbolType,'Micro QR Code']
 SymbolType:=[ SymbolType,'PDF417']
 SymbolType:=[ SymbolType,'QR Code']


switch(Affine_Model)
case 1:
       Affine_Tuple_X:=[]
       Affine_Tuple_Y:=[]
       for Index := 0 to 3 by 1
        draw_point(Window_Handle, Row, Column)
        Affine_Tuple_X:=[Affine_Tuple_X,Row]
        Affine_Tuple_Y:=[Affine_Tuple_Y,Column]
       endfor 
*进行斜着扫码,在这里进行校正
hom_vector_to_proj_hom_mat2d(Affine_Tuple_X, Affine_Tuple_Y, [1,1,1,1], [50,300,300,50], [50,50,300,300], [1,1,1,1], 'normalized_dlt', HomMat2D)
write_tuple(HomMat2D, 'C:/Users/Administrator/Desktop/Halcon代码/校正二维码')
*变换图像
projective_trans_image(ImageIn, Image1, HomMat2D, 'bilinear', 'false', 'false')
break
case 2:
read_tuple('C:/Users/Administrator/Desktop/Halcon代码/校正二维码', HomMat2D)
*变换图像
projective_trans_image(ImageIn, Image1, HomMat2D, 'bilinear', 'false', 'false')
break
case 3:
Image1:=ImageIn
break
endswitch


*明亮变化
if(Scal)
    scale_image(Image1, Image1,Multiple, Offset)
endif
*噪声变化
if(Noise)
    gray_opening_shape(Image1, Image1, 11, 11, 'octagon')
endif
*Gap过大弥补
if(Gap)
    gray_erosion_shape(Image1, Image1, 7, 7, 'octagon')
endif


switch(Model_Type)
case 1: 
create_data_code_2d_model (SymbolType[QrType], 'default_parameters', 'standard_recognition', DataCodeHandleStandard)
*设置超时时间防止读取时间过长
set_data_code_2d_param(DataCodeHandleStandard, 'timeout', Time_Out) 
find_data_code_2d(Image1, Code_Xld, DataCodeHandleStandard, [], [], ResultHandles, DecodedDataStrings)
clear_data_code_2d_model(DataCodeHandleStandard)
break
case 2:
create_data_code_2d_model (SymbolType[QrType], 'default_parameters', 'enhanced_recognition', DataCodeHandleEnhanced)
*设置超时时间防止读取时间过长
set_data_code_2d_param(DataCodeHandleEnhanced, 'timeout', Time_Out) 
find_data_code_2d(Image1, Code_Xld, DataCodeHandleEnhanced, [], [], ResultHandles, DecodedDataStrings)
clear_data_code_2d_model(DataCodeHandleEnhanced)
break
case 3:
create_data_code_2d_model (SymbolType[QrType], 'default_parameters', 'maximum_recognition', DataCodeHandleMaximum)
*设置超时时间防止读取时间过长
set_data_code_2d_param(DataCodeHandleMaximum, 'timeout', Time_Out) 
find_data_code_2d(Image1, Code_Xld, DataCodeHandleMaximum, [], [], ResultHandles, DecodedDataStrings)
clear_data_code_2d_model(DataCodeHandleMaximum)
break
endswitch
if(|ResultHandles|!=0)
* set_system('filename_encoding', 'utf8')  //设置可以显示中文
Strings:=DecodedDataStrings
Image_Out:=Image1
else
*set_system('filename_encoding', 'utf8')  //设置可以显示中文
Strings:=''
endif

return ()

C#使用这个做出的例子或者demo如下,如若不能运行请检查halcon的引用是否正确。

https://download.csdn.net/download/qq_36685891/10478206


你可能感兴趣的:(halcon学习)