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()?

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()".
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

Similar Messages

  • Problema con VB nel generare un file png

    Sono nuovo all'utilizzo di prodotti NI  questo è il mio problema:
    Visualizzo un'immagine catturata da uma telecamera e faccio scehliere all'utente una zona da usare come template per una funzione di PatternMatching. Ho usato un esempio di quelli disponibili ed ho adattato la procedura
        Dim LearnOptions As New CWIMAQLearnPatternOptions
        Dim Rectangle As New CWIMAQRectangle
        'Estrai la regione corrispondente
        CWMachineVision1.GetSelectedRectangleFromViewer CWIMAQViewer1, Rectangle
        'CWIMAQViewer1.Regions.RemoveAll
        CWIMAQVision1.Extract2 CWIMAQViewer1.Image, CWIMAQViewer2.Image, Rectangle
        'Setta parameteri
        LearnOptions.LearnMode = cwimaqLearnAll
        'Setta template
        CWIMAQVision1.LearnPattern2 CWIMAQViewer2.Image, LearnOptions
        ' Salva Immagine Template
        CWIMAQVision2.WritePNGFile CWIMAQViewer2.Image, App.Path & "\Tmp_P.png"
    fino qui tutto OK o quasi  perchè se eseguo la ricerca del pattern con una sequenza di istruzioni tipo:
        Dim ProcessedPartImage As New CWIMAQImage
        Dim ParticleReport As New CWIMAQParticleReport
        Dim AreaArray As Variant
        Dim TextOptions As New CWIMAQTextOptions
        Dim UncorrectedImage As New CWIMAQImage
        'Parametri di Confronto
        Opzioni_Match.MatchMode = cwimaqMatchRotationInvariant
        Opzioni_Match.MinimumMatchScore = 480
        Opzioni_Match.NumMatchesRequested = 5
        Opzioni_Match.MinimumContrast = 10
        Opzioni_Match.SubPixelAccuracy = True
        Opzioni_Match.AdvancedOptions.SubpixelTolerance = 10
        CWMachineVision1.GetSelectedRectangleFromViewer CWIMAQViewer1, Rettangolo_Ricerca, False
        'Confronta e cerca immagine tipo 1
        CWIMAQVision1.MatchPattern2 CWIMAQViewer1.Image, CWIMAQViewer2.Image, Opzioni_Match, Report_Dati_Trovati, Rettangolo_Ricerca
    tutto funziona.
    Ma se provo ad usare come template di ricerca l'immagine  precedentemente salvata  "Tmp_P.png" 
    ho un errore del tipo
    Run Time error "31913"
    Invalid Template Descriptor
    dove sto sbagliando?
    Un anticipato grazie a tutti

    Hi,
    Look at this:
    Problem:
    I am getting the error -1074395247: "invalid template descriptor" from IMAQ Match Pattern. What could be the cause of this error?
    Solution :
    This often happens when you use the IMAQ Save Image to save your template. The IMAQ Save Image saves the pixel data but not the template data. You should use IMAQ Save Image and Vision Info. This will save all of the required template data.
    Or next links:
    http://digital.ni.com/public.nsf/websearch/21BF4BF6A4DD2CD186256D2F00394899?OpenDocument
    http://digital.ni.com/public.nsf/websearch/F05924D50AD6472B86256F320075CDBF?OpenDocument
    Hope it helps,
    Regards,
    Jaime Cabrera
    NI Applications Engineering Spain

Maybe you are looking for