Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Race conditions while using image variables

Sorry if this is a beginner question...

I've been using Labview and the Vision Development Toolkit for about a year. I can usually get it to do what I want to do after a fair amount of trial and error, but I've always been confused on the way the image data type is handled. I'm used to procedural programming languages where you can explicitly pass a value "by reference" (pointer) or "by value" (physical data copy). It confuses me how to know which is which with Labview Vision. It seems that different VI's handle it inconsistently? Some offer a "Img Dest" terminal and some don't.

The way I understand it, passing by wire is like passing "by reference". How can I take a "snapshot" of an image to insure that it doesn't get changed later in the VI accidentally? Basically I want to snapshot the image at various stages of processing to be used "read only" later. Also, how can it be properly cleaned up by IMAQ Dispose without destroying the snapshot. (FYI. I am aware of the implications on memory usage, but I'm not constrained by memory.)

Thanks in advance.

Mike
0 Kudos
Message 1 of 5
(3,477 Views)
The image data is always a reference to an image in memory.  Even if you branch the wire, you still have only one version of the image in memory.  Any vi that modifies the image will modify the only copy that exists.  You can get a big headache if you run an analysis routine parallel with a modification routine.  You never know which will happen first, so you don't know if your analysis is of the original image or the modified image.
 
The solution is to use IMAQ copy to duplicate the image, or feed a different image in as the destination image for your processing.
 
I typically use several images at a time.  I have Original, Processed, and Display.  I copy Original to Processed during the first step of the analysis.  When I am all done processing, I copy Processed to Display and show it on the screen.  If I don't do this, the display often shows a half processed version of the next image instead of the completed version of the previous image.  It can cause a lot of flickering on the display.
 
You don't have to free (dispose) all your images at the end of your program.  If you allocated a large number of images, it is a good idea to free them.  If you don't free them, they will be reused the next time you run the program.  IMAQ create will just use the existing image if the name matches.  This lets you view your image after the program has stopped.  I guess my policy would be to always free any undisplayed images, and leave freeing the displayed images as a decision to be made for each program.
 
Bruce
Bruce Ammons
Ammons Engineering
0 Kudos
Message 2 of 5
(3,471 Views)
Thanks for the clear answer, Bruce.   I have had that headache so many times, aspirin doesn't work anymore!  ;-)   I basically had the same goal as what you are doing, but wanted to hold some intermediate copies for troubleshooting.

After I posted, I had an idea.  I was thinking about the "Image Buffer" function in the Vision Assistant.   It does what I want to do, so I was wondering how it does it.  So I did a quick script in the Vision Assistant:

Open image A.
Add it to buffer slot 1.
Perform some arbitrary filter on it
Add the result to buffer slot 2
Recall from Buffer slot 1

Then migrated the script to Labview...

I found it uses a VI that seemed to be exactly what I've been hoping for.  It's called:   "IVA Image Buffer.vi.  I opened it and did a "Save as" and  was thinking about using this VI to hold intermediate steps.   Am I missing anything important?   I'm not really sure what those IVA functions are, but they seem to show up a lot when stuff is migrated from the Vision Assistant.

Thanks again,
Mike
0 Kudos
Message 3 of 5
(3,470 Views)

Image Buffer is probably just making copies of the image using IMAQ Copy.  I would guess it stores them in numbered images so you can retrieve them later.

My method works for multiple intermediate images as well.  Just create Processed 1, Processed 2, etc. and make new copies as needed during processing.  Anytime you want to keep an image, use a different image as the destination.

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 4 of 5
(3,468 Views)
Sounds good.  Thanks for your help.

Mike
0 Kudos
Message 5 of 5
(3,464 Views)