Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW and Halcon

Dear Guys,

Can anyone please give me the guidance or any direction to use MVTech Halcon library functions in LabVIEW. I am aware of using Call Lib Function Node. But don't know how to pass the parameters required to run. In halcon the image is actually a location of the pointer.

---
Silver_Shaper | CLD
0 Kudos
Message 1 of 70
(26,941 Views)

Any ideas guys

---
Silver_Shaper | CLD
0 Kudos
Message 2 of 70
(26,925 Views)

Well, I used all three methods of interfacing Halcon and LabVIEW (ActiveX, .NET, DLL on C++), they work nearly the same way. You can look at this example (it is chinese forum, but pictures are understandable). As you see, first step is to get pointer to image array allocated in LabVIEW memory. I think, this can be done only with your own DLL compiled in language you choose. In the example above it is Visual Basic. I did it with C/C++:

/* CIN source file */

#include "extcode.h"

/* lv_prolog.h and lv_epilog.h set up the correct alignment for LabVIEW data. */
#include "lv_prolog.h"

/* Typedefs */

typedef struct {
    int32 dimSize;
    int32 Data[1];
    } LVArray;
typedef LVArray **LVArrayHandle;

#include "lv_epilog.h"

extern "C"{
MgErr CINRun(LVArrayHandle Array, uInt32 *ArrPtr);
}

MgErr CINRun(LVArrayHandle Array, uInt32 *ArrPtr)
{
*ArrPtr = uInt32(&(*(*Array)).Data);

return noErr;
}

(I know very well that CINs are obsolete but I don't wanna attach to my projects this small DLL which just gets a pointer)

So, the second step is to pass your pointer to Halcon with gen_image1 or gen_image1_extern operator. After that Halcon generates new memory space where copy of initial image is stored. Then you can perform different actions for image processing such as smoothing, filtering, locating objects etc. If you are going to use ActiveX/.NET, don't forget to close in LabVIEW every reference after dealing with it. If you will be using DLL approach, then you should put set_system('thread_pool', 'false') right before leaving/unloading your library. I did it right after my last Halcon operator when I have finished my image processing (take a look at this thread).

 

BTW, I forgot to mention, if you want to look at finished image in LabVIEW, you need to obtain an image pointer with get_image_pointer1 operator. Then you should transfer data from Halcon memory to LV memory with MoveBlock or CopyMemory/RtlCopyMemory from Windows API.

 

A small part of my project:

Halcon_And_LV.jpg

Message 3 of 70
(26,876 Views)

您好!

       您的这篇文章解决了困扰我好久的问题,我现在在使用NI Vision和Halcon混合编程,使用的Vision获取和处理图像,然后使用Halcon里面的一些算子做开发,现在遇到一个问题,如果只是在LabVIEW下使用Halcon读取本地的图片是可以处理的,但是我不知道如何将LabVIEW下面的图片传给Halcon,我也查了一下资料,Halcon里面有genImage1可以使用Pointer将数组转成halcon的图像格式。

        我一直做的都是LabVIEW,对C++里面不太了解,看到你使用CIN节点实现了我想要的功能,能不能向您请教一下这个是怎么实现的?

        我现在使用的是halcon 12.0版,.net有些改变,Pointer处变成了一个引用,这样我就更加搞不懂怎么用了,麻烦您不吝赐教,谢谢啦

.QQ图片20160629145856.png 左图是新版的halcon的GenImage1的接口类型,我看您之前用的是一个数值型,现在变成了类似引用一样的接口。

Message 4 of 70
(24,292 Views)

FanSun

 

Please, use English here as it is English boards and many people don't speak any other languages. From the machine-translated text I could guess that your problem is related to the data transfer between LV and HALCON. Well, if you use .NET nodes and don't want to implement CIN way to get the data pointer, there are another methods to do the trick. I have attached an example to demonstrate one of such ways.

 

There exists some tricky way of getting the image pointer - by using internal Max function from LabVIEW API. This function always returns the maximum element of the two arguments. First argument is array data pointer (configured in CLFN parameters), second one is zero. So the result is always valid array data pointer. I have heard that such approach is kinda risky because LabVIEW could free that array data pointer when it leaves CLFN. But practically I've never seen that. So, I think, we have enough time to generate HALCON image from that pointer before it is destroyed by LV.

 

If you are afraid of such method you may use LabVIEW Memory Manager functions. The sequence of calls is that:

1) allocate enough memory with DSNewPtr / DSNewPClr function;

2) move the image data from the array into newly created memory with MoveBlock function;

3) use that memory pointer anywhere you want;

4) release the memory with DSDisposePtr function.

This way is 100% safe and clear but it entails three additional operations and one extra copy of the initial array.

 

Here's an example of image data transfer from LabVIEW to HALCON and back with some comments on BD. Feel free to play around with it.

Message 5 of 70
(24,284 Views)

Well, here goes old-fashioned CIN approach for getting pointer of data array and for getting data from the pointer. The CINs are placed on BD, so you may copy-paste them into your code. I attach both 32- and 64-bit variants of CINs but they both work witn U8 arrays only. If you need another data type (such as U(I)16, U(I)32 etc.) then the CINs must be altered and recompiled. I don't upload the sources as there's no need for them for now.

Download All
0 Kudos
Message 6 of 70
(24,277 Views)

Thank you very much.I wil learn about it.

I see you answer other question quote from “”http://www.ihalcon.com/“” that make me mistaked you for the Chinese.

It's so nice of you,thank you again!

0 Kudos
Message 7 of 70
(24,263 Views)

ok,I will try both of them,thank you.

0 Kudos
Message 8 of 70
(24,259 Views)

Dear,

Thank you share the Vi(LV_Image_To_Halcon_LV2014.vi ‏31 KB) , I download& run , but i found a problem,  if the  number of for Cycles more thant 721, for example 722, the  HWindowControl will do not display anything .

 

I beg you tell me the reason , I had spend 7 days to solve this porblem ,but i failed .

 

 

 

2016-11-15_092909.jpg

 

 

0 Kudos
Message 9 of 70
(23,636 Views)

the follow picture will show the problem clearly,   I hope you can understood  what say ,

 

as you know , my english is very poor .

 

B/R 

2016-11-15_093605.jpg

 

 

 

2016-11-15_093605.jpg

0 Kudos
Message 10 of 70
(23,634 Views)