CreateAnchorTemplate Method (GdPictureImaging)
Creates an Anchor Template from a GdPicture image.
Anchoring mechanism (or template recognition) can help to align area to be processed by filters, OMR,
OCR or barcode recognition. For example, if several documents are scanned of the same form, and the
scanning orientation or quality is not guaranteed, the GdPicture Anchoring System can be used to
specify the orientation of each document and the translation made to each document from the one where
the user selected their Areas (surrounding rectangles).
Note: To maximize detection speed and accuracy, the Anchor region must contain less than 50% white pixel and should minimize white margins.
public IntPtr CreateAnchorTemplate(
int ,
int ,
int ,
int ,
int
)
public function CreateAnchorTemplate(
: Integer;
: Integer;
: Integer;
: Integer;
: Integer
): IntPtr;
public function CreateAnchorTemplate(
: int,
: int,
: int,
: int,
: int
) : IntPtr;
public: IntPtr CreateAnchorTemplate(
int ,
int ,
int ,
int ,
int
)
public:
IntPtr CreateAnchorTemplate(
int ,
int ,
int ,
int ,
int
)
'Declaration
Public Function CreateAnchorTemplate( _
ByVal As Integer, _
ByVal As Integer, _
ByVal As Integer, _
ByVal As Integer, _
ByVal As Integer _
) As IntPtr
Parameters
- ImageID
- GdPicture image identifier.
- PosLeft
- The left position, in pixel, (0-based), of the rectangle surrounding the
template.
- PosTop
- The top position, in pixel, (0-based), of the rectangle surrounding the
template.
- Width
- The width, in pixel, of the rectangle surrounding the template.
- Height
- The height, in pixel, of the rectangle surrounding the template.
Return Value
An Anchor Template Identifier to be used with the FindAnchor and the DeleteAnchorTemplate method.
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);
}