///<summery>
        /// Compares template against overlapped image regions
        /// <param name="image">
        /// Image where the search is running.
        /// It should be 8-bit or 32-bit floating-point.
        /// </param>
        /// <param name="templ">
        /// Searched template;
        /// must be not greater than the source image
        /// and the same data type as the image.
        /// </param>
        /// <param name="result">
        /// A map of comparison results; single-channel
        /// 32-bit floating-point.
        /// If image is W×H and templ is w×h then result must be W-w+1×H-h+1.
        /// </param>
        /// <param name="method">
        /// Specifies the way the template must be compared with
        /// image regions (see below).
        /// </param>
        /// <remarks>
        /// The function cvMatchTemplate is similiar to
        /// cvCalcBackProjectPatch. It slids through image,
        /// compares overlapped patches of size w×h with templ
        /// using the specified method and stores the comparison results to result. Here are the formula for the different comparison methods one may use (I denotes image, T - template, R - result. The summation is done over template and/or the image patch: x'=0..w-1, y'=0..h-1):
        ///
        /// method=CV_TM_SQDIFF:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2

        /// method=CV_TM_SQDIFF_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]

        /// method=CV_TM_CCORR:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]

        /// method=CV_TM_CCORR_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]

        /// method=CV_TM_CCOEFF:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')],

        /// where T'(x',y')=T(x',y') - 1/(w•h)•sumx",y"T(x",y")
        ///       I'(x+x',y+y')=I(x+x',y+y') - 1/(w•h)•sumx",y"I(x+x",y+y")

        /// method=CV_TM_CCOEFF_NORMED:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')]/sqrt[sumx',y'T'(x',y')2•sumx',y'I'(x+x',y+y')2]

        /// After the function finishes comparison, the best matches can be found as global minimums (CV_TM_SQDIFF*) or maximums (CV_TM_CCORR* and CV_TM_CCOEFF*) using cvMinMaxLoc function. In case of color image and template summation in both numerator and each sum in denominator is done over all the channels (and separate mean values are used for each channel).
        /// </remarks>
        ///</summery>
        public static CVImage MatchTemplate(CVImage image,
                                            CVImage templateToSearch,
                                            TemplateMatchMethod method)
        {
            //specify the size needed by the match function
            int resultW = image.Width - templateToSearch.Width + 1;
            int resultH = image.Height - templateToSearch.Height + 1;

            if (image.Channels > 1)
            {
                throw new CVException("CVMatchTemplate supports only one channel image format.");
            }
            if (!(image.Depth == CVDepth.Depth32F || image.Depth == CVDepth.Depth8U))
            {
                throw new CVException("CVMatchTemplate supports only 32F or 8U image format.");
            }
            if (image.Depth != templateToSearch.Depth || image.Channels != templateToSearch.Channels)
            {
                throw new CVException("image and template should be of the same type format.");
            }

            CVImage result = new CVImage(resultW, resultH, CVDepth.Depth32F, 1);

            // Native call to openCV cvMatchTemplate function:
            PInvoke.cvMatchTemplate(new __CvArrPtr(image), new __CvArrPtr(templateToSearch), new __CvArrPtr(result), (int)method);

            return(result);
        }
        ///<summery>
        /// Compares template against overlapped image regions
        /// <param name="image">
        /// Image where the search is running. 
        /// It should be 8-bit or 32-bit floating-point.
        /// </param>
        /// <param name="templ">
        /// Searched template;
        /// must be not greater than the source image 
        /// and the same data type as the image. 
        /// </param>
        /// <param name="result">
        /// A map of comparison results; single-channel 
        /// 32-bit floating-point. 
        /// If image is W×H and templ is w×h then result must be W-w+1×H-h+1. 
        /// </param>
        /// <param name="method">
        /// Specifies the way the template must be compared with 
        /// image regions (see below). 
        /// </param>
        /// <remarks>
        /// The function cvMatchTemplate is similiar to 
        /// cvCalcBackProjectPatch. It slids through image, 
        /// compares overlapped patches of size w×h with templ 
        /// using the specified method and stores the comparison results to result. Here are the formula for the different comparison methods one may use (I denotes image, T - template, R - result. The summation is done over template and/or the image patch: x'=0..w-1, y'=0..h-1):
        /// 
        /// method=CV_TM_SQDIFF:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2
        /// method=CV_TM_SQDIFF_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]
        /// method=CV_TM_CCORR:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]
        /// method=CV_TM_CCORR_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]
        /// method=CV_TM_CCOEFF:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')],
        /// where T'(x',y')=T(x',y') - 1/(w•h)•sumx",y"T(x",y")
        ///       I'(x+x',y+y')=I(x+x',y+y') - 1/(w•h)•sumx",y"I(x+x",y+y")
        /// method=CV_TM_CCOEFF_NORMED:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')]/sqrt[sumx',y'T'(x',y')2•sumx',y'I'(x+x',y+y')2]
        /// After the function finishes comparison, the best matches can be found as global minimums (CV_TM_SQDIFF*) or maximums (CV_TM_CCORR* and CV_TM_CCOEFF*) using cvMinMaxLoc function. In case of color image and template summation in both numerator and each sum in denominator is done over all the channels (and separate mean values are used for each channel). 
        /// </remarks>
        ///</summery>
        public static CVImage MatchTemplate(CVImage image,
            CVImage templateToSearch,
            TemplateMatchMethod method)
        {
            //specify the size needed by the match function
            int resultW = image.Width - templateToSearch.Width + 1;
            int resultH = image.Height - templateToSearch.Height + 1;

            if (image.Channels > 1)
            {
                throw new CVException("CVMatchTemplate supports only one channel image format.");
            }
            if (!(image.Depth == CVDepth.Depth32F || image.Depth == CVDepth.Depth8U))
            {
                throw new CVException("CVMatchTemplate supports only 32F or 8U image format.");
            }
            if (image.Depth != templateToSearch.Depth || image.Channels != templateToSearch.Channels)
            {
                throw new CVException("image and template should be of the same type format.");
            }

            CVImage result = new CVImage(resultW, resultH, CVDepth.Depth32F, 1);

            // Native call to openCV cvMatchTemplate function:
            PInvoke.cvMatchTemplate(new __CvArrPtr(image), new __CvArrPtr(templateToSearch), new __CvArrPtr(result), (int)method);
            CVUtils.CheckLastError();
            return result;
        }