Halcon之多线程

本文介绍了如何在Halcon中使用多线程进行图像处理操作,包括读取图像和识别条码数据码。通过示例展示了线程创建的开销以及理想情况下的并行速度提升。实验结果显示,对于长时间运行的任务,多线程可以显著提高效率,但需要注意内部已并行化的操作可能不会带来额外加速。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

H a l c o n 之多线程 Halcon之多线程 Halcon之多线程

开多线程

par_start <ThreadID> : read_image (Image1, 'printer_chip/printer_chip_01')

等待多个线程都结束

* 开启两个子线程
par_start <ThreadID1> : producer_proc()
par_start <ThreadID2> : consumer_proc()
* 等到两个子线程都结束
par_join ([ThreadID1, ThreadID2])

示例1:

******************************************************************
************************* halcon实现多线程*************************
******************************************************************
* 开启多个子线程
par_start <ThreadID1> : read_image (Image1, 'printer_chip/printer_chip_01')
par_start <ThreadID2> : read_image (Image2, 'printer_chip/printer_chip_01')
par_start <ThreadID1> : read_image (Image3, 'printer_chip/printer_chip_01')
par_start <ThreadID2> : read_image (Image4, 'printer_chip/printer_chip_01')
par_start <ThreadID1> : read_image (Image5, 'printer_chip/printer_chip_01')
par_start <ThreadID2> : read_image (Image6, 'printer_chip/printer_chip_01')
par_start <ThreadID1> : read_image (Image7, 'printer_chip/printer_chip_01')
par_start <ThreadID2> : read_image (Image8, 'printer_chip/printer_chip_01')
par_start <ThreadID1> : read_image (Image9, 'printer_chip/printer_chip_01')
par_start <ThreadID2> : read_image (Image10, 'printer_chip/printer_chip_01')
* 等到多个子线程都结束(从这里开始执行子线程)
par_join ([ThreadID1, ThreadID2])

示例2:

* This is a simple example program that demonstrates the
* option to call several HALCON operators or procedures in parallel.
* In general, owing to the overhead of 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)
for L := 1 to Loops * 10 by 1
    par_start<Thread1> : do_nothing ()
    par_start<Thread2> : do_nothing ()
    par_start<Thread3> : 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.
* 
count_seconds (T1)
for L := 1 to Loops by 1
    par_start<Thread1> : wait_seconds (1)
    par_start<Thread2> : wait_seconds (1)
    par_start<Thread3> : 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
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
    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)
    * 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值