Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Do PixelValue2D properties return a new array, or a reference?

I wonder if someone will offer an acceptible C# solution to your issue, but I've found that dealing with the abstraction of C# - lack of control over memory and garbage collection - that the C/C++ libraries are best to use for acquisition, file storage and processing. I plan to create a managed wrapper around my C++ code, and only pass image data to the managed code when I want to display the acquired data to my user interface.

0 Kudos
Message 11 of 16
(893 Views)

Thanks middletongard. I was hoping that wouldn't be the case, but it doesn't surprise me that it is. Looks like I'm going to need some unmanaged code to get things working properly...

 

NI engineers, please add this issue to your feature request list!

0 Kudos
Message 12 of 16
(890 Views)

Hi OrangeAndGreen,

 

Are you using the CreateBufferCollection method to specify your image buffer?

 

Lynn

National Instruments
Senior Systems Engineer
0 Kudos
Message 13 of 16
(876 Views)

Hi Lynn, thanks for inquiring. Yes, I call CreateBufferCollection() when configuring the task. Here are the relevant lines of code:

 

 

            mBufferList = mSession.CreateBufferCollection(10);
            mSession.RingSetup(mBufferList, 0, false);
            mSession.Acquisition.Configure(mBufferList);
            mSession.Start();

 

From my understanding, the problem arises from calling ImaqBuffer.ToPixelArray(), which does some memory allocation (perhaps creating the PixelValue2D object?). Eventually (within a few seconds) the call to ToPixelArray() throws an OutOfMemoryException.

 

Is there any way to get the data out of the ImaqBuffer without allocating new memory? For instance, is it possible to access the data directly from the ImaqBuffer?

 

Thank you for your help!

Dave

 

 

 

 

 

0 Kudos
Message 14 of 16
(872 Views)

One item to note is that the BufferCollection in IMAQ can be of two types - Images or raw data. If you are always going to access them as just raw array data, creating the collection in that format is going to be more efficient. Check out the included Ring examples to see how to do this. That said, I'm still not sure if there is any way to access the buffer in a way that does not do an allocation to return the array.

 

Eric

0 Kudos
Message 15 of 16
(870 Views)

Holy cow, I think you solved it Eric! I changed the call to CreateBufferCollection() to:

 

mBufferList = mSession.CreateBufferCollection(10, ImaqBufferCollectionType.PixelValue2D);

 

I just ran a test and the system was still going strong after several minutes. I guess the buffers default to the VisionImage type if not otherwise specified? Documentation is a bit lacking for this stuff, by which I mean non-existent.

 

I should mention that I also made some changes to reduce memory usage in other parts of the code, so the fix may have been a combination of the two. In other words, I suspect the allocations are only a serious problem when heap space is running low.

 

Thanks again to Eric, Lynn and middletongard for your time and expertise!

 

 

0 Kudos
Message 16 of 16
(865 Views)