打开halcon,按下ctrl+e打开halcon自带例程。应用范围->二维码识别->par_start.hdev
* This is a simple example program that demonstrates the
* Aoption to call several HLtoCON operars or procedures in parallel.
* In general, owing to the overheadof thread creation,
* not in all cases a speedup can be expected,
* especially, if the used operators are already parallelized
* internally using the automatic operator parallelization.
*
* Number of loops to get a more robust time measurement
Loops := 3
*
* Initialize visualization
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)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
disp_message (WindowHandle, 'Read codes sequentially and multithreaded (multiple times)', 'window', 12, 12, 'black', 'true')
*
* Part A
*
* Show thread creation overhead
*
* Thread creation causes a significant overhead.
* Therefore, it is not advisable to create threads for fast tasks.
*
count_seconds (T1)
*循环创建3个线程,线程中什么事情都不做,这里是为了测试创建线程花费的时间,这里是为了告诉你不要频繁的创建线程,那样可能会花费更多的时间
for L := 1 to Loops * 10 by 1
par_start : do_nothing ()
par_start : do_nothing ()
par_start : do_nothing ()
* par_join basically takes as long as the slowest thread (plus overhead).
* Use the profiler to check execution times.
par_join ([Thread1,Thread2,Thread3])
endfor
count_seconds (T2)
* Time needed to create and join the three threads in milliseconds
TimeOverhead := 1000.0 * (T2 - T1) / (Loops * 10)
*
* Part B
*
* Show perfect parallelization
*
* The best speedup can be achieved for long running tasks
* that are not parallelized internally.
*
*测试一下3个处理时间都为1s的程序在应用多线程处理后花费的总时间
count_seconds (T1)
for L := 1 to Loops by 1
par_start : wait_seconds (1)
par_start : wait_seconds (1)
par_start : wait_seconds (1)
* par_join basically takes as long as the slowest thread (plus overhead).
* Use the profiler to check execution times.
*等待三个线程全部执行完成
par_join ([Thread1,Thread2,Thread3])
endfor
count_seconds (T2)
* This should be about 1000 milliseconds
TimeWaitSeconds := 1000.0 * (T2 - T1) / Loops
*
* Part C
*
* Read bar codes and data codes in parallel
*
*
* Create the bar code and data code models
*创建二维码模板/这个例程中不只有二维码,还有ECC,条形码
create_data_code_2d_model ('QR Code', [], [], QrCodeModel)
create_data_code_2d_model ('Data Matrix ECC 200', 'default_parameters', 'maximum_recognition', Ecc200Model)
create_bar_code_model ([], [], BarCodeModel)
*
* Training
*查找二维码
find_data_code_2d (Image, QrCodeXldsSeq, QrCodeModel, ['stop_after_result_num','train'], [2,'all'], QrCodeHandlesSeq, QrCodeStringsSeq)
find_data_code_2d (Image, Ecc200XldsSeq, Ecc200Model, ['stop_after_result_num','train'], [2,'all'], Ecc200HandlesSeq, Ecc200StringsSeq)
find_bar_code (Image, BarCodeRegionsSeq, BarCodeModel, ['EAN-13','Code 128'], BarCodeStringsSeq)
*
* Execute once to fill cache
find_qr_codes (Image, QrCodeXldsSeq, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)
*
* Read codes sequentially
*顺序执行处理二维码查找的问题,计算顺序执行所需花费的时间
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
*
* Read codes in parallel
*创建多线程来处理二维码查找的问题,提升程序执行效率
count_seconds (T1)
for L := 1 to Loops by 1
*选中函数,按alt+enter快速定位到本地函数中
par_start : find_qr_codes (Image, QrCodeXlds, QrCodeModel, QrCodeHandlesSeq, QrCodeStringsSeq)
par_start : find_ecc200_codes (Image, Ecc200Xlds, Ecc200Model, Ecc200Handles, Ecc200Strings)
par_start : find_bar_codes (Image, BarCodeRegions, BarCodeModel, BarCodeStrings)
* par_join basically takes as long as the slowest thread (plus overhead).
*等待三个线程全部执行完成
par_join ([ThreadQRCode,ThreadECC200,ThreadBarcode])
endfor
count_seconds (T2)
TimePar := 1000.0 * (T2 - T1) / Loops
*
* Calculate speedup
Speedup := 100 * ((TimeSeq / TimePar) - 1)
*
* Display result
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)
以上通过创建多线程处理图像,通过创建线程调用本地函数来处理图像,那么本地函数怎么创建呢?
1、在程序编辑器上单机右键
2、添加新函数,输入新函数名称
3、确定函数的输入输出参数,通过加号自由添加删除
(1)输入图像参数
(2)输出图像参数
(3)输入控制参数
(4)输出参数
4、后续需要更改本地函数的输入输出参数时,点击圆圈处按钮