LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Use AForge .NET in LabVIEW with a USB Webcam?

Hello everyone, 

 

I have been trying to work on a Vision System that uses a Webcam, and the AForge.NET vision library. I am having trouble using the event callback module in LabVIEW. I am trying to emulate this part of the code: 

 

 

videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                    videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

//eventhandler if new frame is ready
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();
            //do processing here
            pictureBox1.Image = img;
        }

 

This piece of code wants to put the webcam image into a .NET picture box and do all of the image processing on that picturebox. I have read several entries about how to use Event Callback, and have even looked at the examples in LabVIEW. Has anyone used AForge.NET with LabVIEW before? I would appreciate any suggestions on how I can translate this C# code to LabVIEW. 

 

 

 

Thanks, 

Jameson

0 Kudos
Message 1 of 7
(5,112 Views)

Which part are you having problems with? Creating a callback VI, or implementing the code inside the event handler (i.e., the casing of the eventArgs to a Bitmap and then assigning that to the PictureBox)?

0 Kudos
Message 2 of 7
(5,099 Views)

I am having a problem with casting the NewFrameEventArgs to a picture box. I want to do some motion recognition, so I would like to do all of the image processing with respects to the picture box. I have never used event callbacks before especially when i'm trying to pass all of that callback info to a picture frame in my main VI. 

 

0 Kudos
Message 3 of 7
(5,095 Views)

You should be able to use the "To More Specific Class" function to cast the return value of the Clone() method to the Bitmap class. You would need to pass in the reference to the PictureBox so you can access it in the callback. This you can do with the User Parameter part of the Register Event Callback function. Attached is something to start with. Here I used a calendar control just so I could register the control. In your case you'd wire the VideoCaptureDevice class so you can access its list of callbacks.

0 Kudos
Message 4 of 7
(5,080 Views)

Hello, I've re-visited this project, and I've come across another problem.

 

I've been getting a fairly large memory leak in my program. When I clone the picture, i need to dispose of the original picture. I am unsure of where and how to do this (inside the callback, or outside of it?) How would i go about setting up a mechanism to dispose of old pictures as new ones come in? 

 

Thank you

Message 5 of 7
(4,883 Views)

Hi;

 

I also have problem with memory leak when using queue to pass the bitmap object to another loop. Does anyone have a solution for this? I attached the program example. Thanks

DAKBIAS
Download All
0 Kudos
Message 6 of 7
(3,823 Views)

Hi Dakbias,

 

I have a couple suggestions that might help:

 

1. Avoid using queues if possible. You may want to load/acquire and process the image in a single while loop. Having a queue of 10 images waiting for processing can definitly lead to memory issues.

2. Use the standard queue functions instead of the RT FIFO. The RT FIFO is for deterministic execution, but more resource intensive and not meant for the Windows environment.

3. Close the FIFO/queue functions. You aren't releasing the queue so these items are staying in memory. Please see below for more info. 

What is the Fastest producer consumer method. Queue, RT-FIFO, Event

http://forums.ni.com/t5/LabVIEW/What-is-the-Fastest-producer-consumer-method-Queue-RT-FIFO-Event/td-...

 

Also, you may want to create a new thread for new questions. The last post in the thread was in 2010.

 

 

0 Kudos
Message 7 of 7
(3,776 Views)