AForge.Imaging.ExhaustiveTemplateMatching.ProcessImage C# (CSharp) Method

ProcessImage() public method

Process image looking for matchings with specified template.
The source image has incorrect pixel format. Template image is bigger than source image.
public ProcessImage ( Bitmap image, Bitmap template, Rectangle searchZone ) : AForge.Imaging.TemplateMatch[]
image System.Drawing.Bitmap Source image to process.
template System.Drawing.Bitmap Template image to search for.
searchZone System.Drawing.Rectangle Rectangle in source image to search template for.
return AForge.Imaging.TemplateMatch[]
        public TemplateMatch[] ProcessImage( Bitmap image, Bitmap template, Rectangle searchZone )
        {
            // check image format
            if (
                ( ( image.PixelFormat != PixelFormat.Format8bppIndexed ) &&
                  ( image.PixelFormat != PixelFormat.Format24bppRgb ) ) ||
                ( image.PixelFormat != template.PixelFormat ) )
            {
                throw new UnsupportedImageFormatException( "Unsupported pixel format of the source or template image." );
            }

            // check template's size
            if ( ( template.Width > image.Width ) || ( template.Height > image.Height ) )
            {
                throw new InvalidImagePropertiesException( "Template's size should be smaller or equal to source image's size." );
            }

            // lock source and template images
            BitmapData imageData = image.LockBits(
                new Rectangle( 0, 0, image.Width, image.Height ),
                ImageLockMode.ReadOnly, image.PixelFormat );
            BitmapData templateData = template.LockBits(
                new Rectangle( 0, 0, template.Width, template.Height ),
                ImageLockMode.ReadOnly, template.PixelFormat );

            TemplateMatch[] matchings;

            try
            {
                // process the image
                matchings = ProcessImage(
                    new UnmanagedImage( imageData ),
                    new UnmanagedImage( templateData ),
                    searchZone );
            }
            finally
            {
                // unlock images
                image.UnlockBits( imageData );
                template.UnlockBits( templateData );
            }

            return matchings;
        }

Same methods

ExhaustiveTemplateMatching::ProcessImage ( Bitmap image, Bitmap template ) : AForge.Imaging.TemplateMatch[]
ExhaustiveTemplateMatching::ProcessImage ( BitmapData imageData, BitmapData templateData ) : AForge.Imaging.TemplateMatch[]
ExhaustiveTemplateMatching::ProcessImage ( BitmapData imageData, BitmapData templateData, Rectangle searchZone ) : AForge.Imaging.TemplateMatch[]
ExhaustiveTemplateMatching::ProcessImage ( UnmanagedImage image, UnmanagedImage template ) : AForge.Imaging.TemplateMatch[]
ExhaustiveTemplateMatching::ProcessImage ( UnmanagedImage image, UnmanagedImage template, Rectangle searchZone ) : AForge.Imaging.TemplateMatch[]

Usage Example

        /// <summary>
        /// Scans rank of face cards
        /// </summary>
        /// <param name="cardImage"></param>
        /// <returns></returns>
        private Rank ScanFaceRank(Bitmap bmp)
        {
            //Initiliaze template matching class with 0.75 threshold
            ExhaustiveTemplateMatching templateMatchin = new ExhaustiveTemplateMatching(0.75f);
            Rank rank = Rank.NOT_RECOGNIZED;

            if (templateMatchin.ProcessImage(bmp, j).Length > 0) //If Jack template matches
                rank = Rank.Jack;
            if (templateMatchin.ProcessImage(bmp, k).Length > 0)//If King template matches
                rank = Rank.King;
            if (templateMatchin.ProcessImage(bmp, q).Length > 0)//If Queen template matches
                rank = Rank.Queen;

            return rank;
        }
All Usage Examples Of AForge.Imaging.ExhaustiveTemplateMatching::ProcessImage