This MVTec Halcon wrapper can pass HObjects of type XLD between functions. The XLD is not mapped to a Matlab structure. It is simply serialized to a byte stream.
The build process is packed into the function build(). Executing build() will create a new folder "../halcon-XXX-YYY-ZZZ/+halcon", where XXX stands for the Halcon version, YYY for the matlab('arch') and ZZZ for the short name of the mex C++ compiler.
After adding the "halcon-XXX-YYY-ZZZ" folder to the matlab path one can e.g. use the model finding functions
xld = halcon.EdgesSubPix(templateImage, ...);
modelId = halcon.CreateScaledShapeModelXld(xld, ...);
[rows, cols, angles, scales, scores] = halcon.FindScaledShapeModel(searchImage, modelId, ...);
T. Dunker (2020). MVTec Halcon wrapper with XLD serialization (https://github.com/T-Dunker/MatlabHalcon), GitHub. Retrieved .
The problem is that the mex function (DLL, Windows) can not be unloaded. The HALCON 12.0.3 / Programmer's Guide / Tips and Tricks chapter 3.2 "Terminate HALCON Library" says the following:
"In applications where DLLs are unloaded in a thread-exclusive context (such as applications using COM), the HALCON library will not terminate properly if the thread pool is still active, except when using the HALCON/COM interface.
To overcome this problem, it is necessary to either call set_system('thread_pool','false') early enough before terminating the application (typically in the main application windows close event), or to disable thread pool cleanup by setting HShutdownThreadPool = FALSE at any time."
Actually, in the beginning of each mex file from the package SetSystem("thread_pool", "false") is called.
After using a Halcon function Matlab can not clear its complete workspace any more, e.g.
>> clear all
>> num = halcon.GetSystem('thread_num')
>> clear all
will not return. In contrast, clearing only Matlab variables like ">>clear num" works correctly. I use "taskkill /f /im matlab.exe" to finish Matlab after using Halcon. So far I could not identify the reason.
I found it! Just use function GetContourXld()~
Thanks for sharing. I also want to ask how to use the byte stream in matlab. Function EdgeSubPix() return structure containing a field named xld. I thought this may be the coordinates of edge. But the facts was the size of xld is n by 1. So how to transfer this variable into the coordinates:)
moved sources to https://github.com/T-Dunker/MatlabHalcon
ambiguity between function and variable names removed
removed error in build.m
Prevent dynamical unloading of the dll when clearing for a halcon mex function is called by mexLock(). This avoids the hang of matlab.
Inspired by: MVTec Halcon Wrapper