FindPattern Method

NI Vision for Visual Basic

FindPattern Method

Syntax

CWMachineVision.FindPattern SearchImage, Pattern, PatternMatchReport [, SearchRotatedRectangle] [, FindPatternOptions] [, CoordinateTransformation]

Return Type

Long

On success, this method returns 0. On failure, this function returns a negative number.

Purpose

Searches for a template image in a rectangular search area of the image. If the template image has not been previously learned, this method automatically learns the template for the configured match stage.

Remarks

Use this method with U8 images.

This method can overlay, on the image returned, the position of the search area, the centers, and bounding rectangles of the objects detected.

Parameters

SearchImage As CWIMAQImage

The image in which the template image is to be located.

Pattern As CWIMAQImage

The image to be located during the pattern matching process. The template image need not have been learned previously.

PatternMatchReport As CWIMAQPatternMatchReport

On return, a report containing information about each match found in the image.

SearchRotatedRectangle As Variant

[Optional] A CWIMAQRotatedRectangle object that specifies the region within the image. This rotated rectangle will be transformed by TransformRegions if a CoordinateTransformation is supplied and the bounding rectangle of the result is used to search for the template pattern. Do not supply this parameter if you want the entire image to be examined.

FindPatternOptions As Variant

[Optional] A CWMVFindPatternOptions object that specifies the parameters the method uses for matching. If not supplied, the defaults are used.

CoordinateTransformation As Variant

[Optional] A CWMVCoordinateTransformation object. If supplied, the input search area is transformed using this parameter.

Example

Private Sub Learn_Click()
    'Declarations
    Dim TemplateRectangle As New CWIMAQRectangle
        
    'Get the currently selected rectangle from the viewer
    CWMachineVision1.GetSelectedRectangleFromViewer CWIMAQViewer1, TemplateRectangle

    'Extract the template image
    CWIMAQVision1.Extract2 CWIMAQViewer1.Image, CWIMAQViewer2.Image, TemplateRectangle
End Sub

Private Sub Match_Click()
    'Declarations
    Dim SearchArea As New CWIMAQRotatedRectangle
    Dim Options As New CWMVFindPatternOptions
    Dim Transformation As New CWMVCoordinateTransformation
    Dim Report As New CWIMAQPatternMatchReport
        
    'Get the currently selected rotated rectangle from the viewer. If none is selected,
    'search the entire image.
    CWMachineVision1.GetSelectedRotatedRectFromViewer CWIMAQViewer1, SearchArea, False

    'Use rotation invariant pattern matching
    Options.MatchMode = cwimaqMatchRotationInvariant
    Options.NumMatchesRequested = 10
    
    'Find the pattern on the Viewer image in the selected rectangle. If this is the first
    'first time matching after a learn, this process will take longer than normal because
    'it will first learn the template image.
    CWMachineVision1.FindPattern CWIMAQViewer1.Image, CWIMAQViewer2.Image, Report, SearchArea, _
                                 Options, Transformation
End Sub

See Also

CWIMAQVision.MatchPattern2

CWIMAQVision.LearnPattern2

CWIMAQRotatedRectangle

CWMVFindPatternOptions

GetSelectedRotatedRectFromViewer

CWIMAQVision.Extract2