GdPicture.NET.14
GdPicture14 Namespace / GdPictureImaging Class / FindAnchor Method
GdPicture image identifier.
An Anchor Template Identifier returned by the CreateAnchorTemplate method.
A member of the OMRMode enumeration. FavorSpeed is suggested since there is almost no accuracy difference between FavorSpeed and FavorQuality.
The left position, in pixel, (0-based), of the area to search the anchor template.
The top position, in pixel, (0-based), of the area to search the anchor template.
The width, in pixel, of the area to search the anchor template.
The height, in pixel, of the area to search the anchor template.
Output parameter. Returns the left position, in pixel, (0-based), of the detected anchor template.
Output parameter. Returns the top position, in pixel, (0-based), of the detected anchor template.
Output parameter. Returns the width, in pixel, of the detected anchor template.
Output parameter. Returns the height, in pixel, of the detected anchor template.
Output parameter. Accuracy in percentage [0 - 100].
Example





In This Topic
FindAnchor Method (GdPictureImaging)
In This Topic
Tries to determine the position of a specific template on a region of interest of a GdPicture image. The sample application "OMR - Anchor" demonstrates clearly the usage of the Anchor Mechanism.
Syntax
'Declaration
 
Public Function FindAnchor( _
   ByVal ImageID As Integer, _
   ByVal AnchorTemplateID As IntPtr, _
   ByVal Mode As OMRMode, _
   ByVal SearchLeft As Integer, _
   ByVal SearchTop As Integer, _
   ByVal SearchWidth As Integer, _
   ByVal SearchHeight As Integer, _
   ByRef PosLeft As Integer, _
   ByRef PosTop As Integer, _
   ByRef PosWidth As Integer, _
   ByRef PosHeight As Integer, _
   ByRef Accuracy As Double _
) As GdPictureStatus
public GdPictureStatus FindAnchor( 
   int ImageID,
   IntPtr AnchorTemplateID,
   OMRMode Mode,
   int SearchLeft,
   int SearchTop,
   int SearchWidth,
   int SearchHeight,
   ref int PosLeft,
   ref int PosTop,
   ref int PosWidth,
   ref int PosHeight,
   ref double Accuracy
)
public function FindAnchor( 
    ImageID: Integer;
    AnchorTemplateID: IntPtr;
    Mode: OMRMode;
    SearchLeft: Integer;
    SearchTop: Integer;
    SearchWidth: Integer;
    SearchHeight: Integer;
   var  PosLeft: Integer;
   var  PosTop: Integer;
   var  PosWidth: Integer;
   var  PosHeight: Integer;
   var  Accuracy: Double
): GdPictureStatus; 
public function FindAnchor( 
   ImageID : int,
   AnchorTemplateID : IntPtr,
   Mode : OMRMode,
   SearchLeft : int,
   SearchTop : int,
   SearchWidth : int,
   SearchHeight : int,
   PosLeft : int,
   PosTop : int,
   PosWidth : int,
   PosHeight : int,
   Accuracy : double
) : GdPictureStatus;
public: GdPictureStatus FindAnchor( 
   int ImageID,
   IntPtr AnchorTemplateID,
   OMRMode Mode,
   int SearchLeft,
   int SearchTop,
   int SearchWidth,
   int SearchHeight,
   ref int PosLeft,
   ref int PosTop,
   ref int PosWidth,
   ref int PosHeight,
   ref double Accuracy
) 
public:
GdPictureStatus FindAnchor( 
   int ImageID,
   IntPtr AnchorTemplateID,
   OMRMode Mode,
   int SearchLeft,
   int SearchTop,
   int SearchWidth,
   int SearchHeight,
   int% PosLeft,
   int% PosTop,
   int% PosWidth,
   int% PosHeight,
   double% Accuracy
) 

Parameters

ImageID
GdPicture image identifier.
AnchorTemplateID
An Anchor Template Identifier returned by the CreateAnchorTemplate method.
Mode
A member of the OMRMode enumeration. FavorSpeed is suggested since there is almost no accuracy difference between FavorSpeed and FavorQuality.
SearchLeft
The left position, in pixel, (0-based), of the area to search the anchor template.
SearchTop
The top position, in pixel, (0-based), of the area to search the anchor template.
SearchWidth
The width, in pixel, of the area to search the anchor template.
SearchHeight
The height, in pixel, of the area to search the anchor template.
PosLeft
Output parameter. Returns the left position, in pixel, (0-based), of the detected anchor template.
PosTop
Output parameter. Returns the top position, in pixel, (0-based), of the detected anchor template.
PosWidth
Output parameter. Returns the width, in pixel, of the detected anchor template.
PosHeight
Output parameter. Returns the height, in pixel, of the detected anchor template.
Accuracy
Output parameter. Accuracy in percentage [0 - 100].

Return Value

A member of the GdPictureStatus enumeration.
Remarks
Be aware that this method ignores DPI. Templates and candidates need to have the same characteristics to achieve consistent results. That means you need to rescale images in question according to the DPI if required.

This method is used in the "OMR" Demo.

Example
Creating an anchor template from the template image and finding the anchor element on the target image. The result is saved into a PNG file.
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
    // Open image files.
    int anchorImage = gdpictureImaging.CreateGdPictureImageFromFile("templateImage.tif");
    int targetImage = gdpictureImaging.CreateGdPictureImageFromFile("image.tif");
 
    // Create the anchor template. Let supose, that the key element is present on coordinates 100,100 and it has size 50 x 50 pixels.
    IntPtr template = gdpictureImaging.CreateAnchorTemplate(anchorImage, 100, 100, 50, 50);
 
    // Change the resolution of the target image to have the same dimensions (in pixels), as the template image.
    int anchorImgWidth = gdpictureImaging.GetWidth(anchorImage);
    int anchorImgHeight = gdpictureImaging.GetHeight(anchorImage);
    gdpictureImaging.Resize(targetImage, anchorImgWidth, anchorImgHeight, System.Drawing.Drawing2D.InterpolationMode.Default);
 
    int left = 0;
    int top = 0;
    int width = 0;
    int height = 0;
    double accuracy = 0;
    gdpictureImaging.FindAnchor(targetImage, template, OMRMode.FavorSpeed, 0, 0, anchorImgWidth, anchorImgHeight, ref left, ref top, ref width, ref height, ref accuracy);
 
    // If the anchor is successfully recognized, we will mark it on the target image.
    if (accuracy > 75)
    {
        gdpictureImaging.DrawRectangle(targetImage, left, top, width, height, 2, gdpictureImaging.ARGBI(255, 255, 0, 0), false);
    }
 
    gdpictureImaging.SaveAsPNG(targetImage, "output.png");
 
    // Release used resources.
    gdpictureImaging.ReleaseGdPictureImage(anchorImage);
    gdpictureImaging.ReleaseGdPictureImage(targetImage);
}
See Also