Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

LearnPattern2 PMLearnAdvancedSetupDataOptions

I am trying to dynamically learn a template for grayscale pattern matching.

 

 I am programming in C# and I have Vision Development Module  2013 SP1 installed.

I’m calling the PatternMatch3(VisionImage template, etc…) function.

If I create a template image in Vision Assistant and save it to a .png file. Then open it in my code using VisionImage.ReadVisionImage()

PatternMatch3() works fine.

 

As I said, this does not help me. I need to capture an image using a frame grabber, then extract an ROI, then create a template from the image I extract from the roi.

I tried using LearnPattern() to train the template, which works without an exception. But, when I try this template in PatternMatch3(), I get the following error

ErrorCode.TemplateDescriptor=-1074395247 - Invalid template descriptor.

 

So, I dug a little and found LearnPattern2().

LearnPattern2() method accepts a parameter that is a collection of PMLearnAdvancedSetupDataOption values as one of its parameters.  

I’ve tried creating a collection of PMLearnAdvancedSetupDataOption parameters that match the PMMatchAdvancedSetupDataOption used in PatternMatch3().

But, no matter what I try, I keep getting the following error when I call LearnPattern2()

 

Clearly there is Vision Information in the template file that holds the data needed for a valid template that works.

My question is, how the heck can one set the Vision Information in a dynamically created template using LearnPattern2(), so that it would be the same as the vision information when you open a previously created template in a .png file using ReadVisionImage()?

 

0 Kudos
Message 1 of 8
(5,332 Views)

There's an imaqLearnPattern3 function, and I believe the number needs to correspond to the PatternMatch3 in order to be compatible.  E.g., you are getting this error because you're creating the template in LearnPattern2 and trying to use it in PatternMatch3.  Try using the imaqLearnPattern3 function and let me know if that works.

Julian R.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 8
(5,311 Views)

Hi Julian,

Didn't you read that  JohnW programs in C#? In NI Vision .NET there's no imaqLearnPattern3 function as you suggested. There're just LearnPattern and LearnPattern2. And NI make it so that if you use LearnPattern2 then must you use PatternMatch3. I know it makes no sense but it's so. NI did this way and i find it very poor.

 

Devrim

0 Kudos
Message 3 of 8
(5,131 Views)

Hi JohnW,

I am programming in C#, too. NI Vision .NET makes no sense anymore. I had always problem, poor documentary, ...

 

LearnPattern2 example:

 

            // Learn Patterns Matching Template with Gray Value Pyramid
            // Set the angle range.
            RotationAngleRange rotAngleRange = new RotationAngleRange(0, 360);
            // Set the advanced options.      
            Collection<PMLearnAdvancedSetupDataOption> learnAdvSetupDataOption = new Collection<PMLearnAdvancedSetupDataOption>();
            learnAdvSetupDataOption.Add(new PMLearnAdvancedSetupDataOption(LearnSetupOption.PyramidMaxPyramidLevelToStoreData, 2));
            Algorithms.LearnPattern2(image, null, MatchingAlgorithm.MatchGrayValuePyramid, rotAngleRange, learnAdvSetupDataOption);

 

 

Devrim

0 Kudos
Message 4 of 8
(5,130 Views)

Hello John,

Did you find a solution for your problem.

 

I have problems with LearnPattern(2) and PatternMatch3(), too. Sometimes, not always, gives in my code "PatternMatch3()" for the same template  "Invalid template descriptor" error. What I don't understand why I get error. My template (for same template) must have either always "invalid template descriptor or always valid template descriptor. But not random.

 

I'm using for pattern matching "gray value pyramid". So I must use "LearnPattern2()" with advanced setup data options.

 

My code for template learning:

 

            ////////////////////////////
            // Learn Patterns Matching Template with Gray Value Pyramid
            // Set the angle range.
            RotationAngleRange rotAngleRange = new RotationAngleRange(0, 360);
            // Set the advanced options.      
            Collection<PMLearnAdvancedSetupDataOption> learnAdvSetupDataOption = new Collection<PMLearnAdvancedSetupDataOption>();
            
            // Pyramid - Max Pyramid Level To Store
            // The default value is 2, which means data for levels 1 and 0 is stripped.
            // When learning a large template for a large angle range, 
            // the amount of data generated can be fairly significant and this parameter can be used to control the size.
            // However, all the removed data needs to be re-computed by IMAQ Match Pattern 4, which increases match times. 
            // A trade-off needs to be made between a smaller template size and faster match times.
            
            learnAdvSetupDataOption.Add(new PMLearnAdvancedSetupDataOption(LearnSetupOption.PyramidMaxPyramidLevelToStoreData, 2));
            //Algorithms.LearnPattern2(image, null, MatchingAlgorithm.MatchGrayValuePyramid, rotAngleRange, learnAdvSetupDataOption);

            Algorithms.LearnPattern2(image, null, MatchingAlgorithm.MatchGrayValuePyramid, rotAngleRange);

// Check template           PatternMatchTemplateInfo patMatchTempInfo = new PatternMatchTemplateInfo();
            patMatchTempInfo = Algorithms.GetPatternMatchTemplateInfo(image, null, MatchingAlgorithm.MatchGrayValuePyramid);

 

I save my templates with WriteVisionFile()

 

                        viTemplateToBeSaved.WriteVisionFile(sActualPatternWithPath);
                        bTemplateSaved = true;

 

 

Ans I read my templates with ReadVisionFile()

                templateImage.ReadVisionFile(templatesInPastalDirectory[i]);

 

 

 

 Because I use "gray value pyramid" then must I use "MatchPattern3()".

Exception_InvalidTemplateDescriptor.jpg

 

I can open my templates in Template editor withot any problem. And the error above get I not always. Where is the problem?

 

Can anybody help me?

 

Devrim

 

 

0 Kudos
Message 5 of 8
(5,033 Views)

Anybody?

0 Kudos
Message 6 of 8
(4,999 Views)

Devrim,

   I have not had a chance to look back into this recently. But, I will be doing so soon.

I'll be gald to post any findings that I come up with. I will keep you posted.

There has got to be a simple reason why this LearnPattern2 poses so many problems.

John

0 Kudos
Message 7 of 8
(4,990 Views)

John,

I know now why I get "Invalid template descriptor" error/exception.But knowing this not solves my problem and knowing why this problem exists makes me just angry against NI.

 

All my templates are OK. Problem is, NI Vision .NET is 32-bit and supports memory just up to about 1.2GB no matter how much memory your PC has.Depending on your image and template use MatchPattern3 a lot of memory. For example in my case about 350 MB. If your program uses at the moment before calling MatchPattern3 about 900MB memory then exceeds NI Vision .NET this 1.2 GB (maybe 1.1GB) upper limit and can't load template right template and gives out an exception. That's my experience. To find out this have I wasted many days! 

 

I thought I make mistake in template generating because of this !@& "invalid template descriptor" exception. Actually it must like "not enough memory" or something similar.

 

Devrim

 

 

0 Kudos
Message 8 of 8
(4,924 Views)