多线程并行读取QR二维码
Halcon中的多线程使用方法
par_start<Thread_1> : Method_1()
par_start<Thread_2> : Method_2()
par_start<Thread_3> : Method_3()
...
par_start<Thread_n> : Method_n()
* 等待n个线程执行完成
par_join ([Method_1,Method_2,Method_3....Method_n])
多线程并行读取QR二维码
**************************************************************************1.初始化****************************************************
*测量次数
Loops := 3
*关闭程序计数器,图形窗口更新,图像变量更新
dev_update_off ()
*关闭已经打开的窗口
dev_close_window ()
*读取一张图像
read_image (Image, 'barcode/mixed/barcodes_datacodes_mixed_01')
*创建一个窗口,图像可以自适应该窗口
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
*设置字体显示信息:字体大小:16;字体类型:mono;粗体,斜体;
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*显示图像
dev_display (Image)
**************************************************************************1.多线程来对多个二维码进行解码****************************************************
*创建一个QR Code类型的二维码模版
create_data_code_2d_model ('QR Code', [], [], QrCodeModel)
*创建一个Data Matrix ECC 200类型的二维码模版
create_data_code_2d_model ('Data Matrix ECC 200', 'default_parameters', 'maximum_recognition', Ecc200Model)
*创建一个一维码模版
create_bar_code_model ([], [], BarCodeModel)
* 训练Qr二维码模版
find_data_code_2d (Image, QrCodeXldsSeq, QrCodeModel, ['stop_after_result_num','train'], [2,'all'], QrCodeHandlesSeq, QrCodeStringsSeq)
* 训练Ecc200二维码模版
find_data_code_2d (Image, Ecc200XldsSeq, Ecc200Model, ['stop_after_result_num','train'], [2,'all'], Ecc200HandlesSeq, Ecc200StringsSeq)
* 训练EAN-13一维码模版
find_bar_code (Image, BarCodeRegionsSeq, BarCodeModel, ['EAN-13','Code 128'], BarCodeStringsSeq)
* 串行识别二维码信息,循环识别3次
count_seconds (T1)
for L := 1 to Loops by 1
find_qr_codes (Image, QrCodeXldsSeq, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)
endfor
count_seconds (T2)
*串行操作花费的时间
TimeSeq := 1000.0 * (T2 - T1) / Loops
* 并行识别二维码信息,循环识别3次
count_seconds (T1)
for L := 1 to Loops by 1
par_start<ThreadQRCode> : find_qr_codes (Image, QrCodeXlds, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
par_start<ThreadECC200> : find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
par_start<ThreadBarcode> : find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)
* 等待3个线程执行完成
par_join ([ThreadQRCode,ThreadECC200,ThreadBarcode])
endfor
count_seconds (T2)
*并行操作花费的时间
TimePar := 1000.0 * (T2 - T1) / Loops
* 计算并行相对串行速度提升了多少
Speedup := 100 * ((TimeSeq / TimePar) - 1)
* 显示识别到的二维码区域
dev_set_draw ('margin')
dev_set_line_width (4)
dev_set_color ('yellow')
dev_display (QrCodeXlds)
dev_set_color ('green')
dev_display (Ecc200Xlds)
dev_set_color ('cyan')
dev_display (BarCodeRegions)
Color := ['forest green','red']
disp_message (WindowHandle, 'Multithreading speedup: ' + Speedup$'.2f' + '%', 'window', 42, 12, Color[Speedup < 1], 'true')
*删除各自条码模版并释放分配内存
clear_bar_code_model (BarCodeModel)
clear_data_code_2d_model (Ecc200Model)
clear_data_code_2d_model (QrCodeModel)
运行速度提升88.5%