'**SUMMARY**
This function searches an image for a template image.  The template
image is a smaller image that is searched for in the bigger image.
This is a basic pattern finder in an image.  This uses the standard
OpenCV template (pattern) matching and cannot handle scaling or rotation
Template matching returns a match score for every pixel in the image.
Often pixels that are near to each other and a close match to the template
are returned as a match. If the threshold is set too low expect to get
a huge number of values. The threshold parameter is in terms of the
number of standard deviations from the mean match value you are looking
For example, matches that are above three standard deviations will return
0.1% of the pixels. In a 800x600 image this means there will be
800*600*0.001 = 480 matches.
This method returns the locations of wherever it finds a match above a
threshold. Because of how template matching works, very often multiple
instances of the template overlap significantly. The best approach is to
find the centroid of all of these values. We suggest using an iterative
k-means approach to find the centroids.
**PARAMETERS**
* *template_image* - The template image.
* *threshold* - Int
* *method* -
* SQR_DIFF_NORM - Normalized square difference
* SQR_DIFF      - Square difference
* CCOEFF        -
* CCOEFF_NORM   -
* CCORR         - Cross correlation
* CCORR_NORM    - Normalize cross correlation
* *grayscale* - Boolean - If false, template Match is found using BGR image.
**EXAMPLE**
>>> image = Image("/path/to/img.png")
>>> pattern_image = image.crop(100,100,100,100)
>>> found_patterns = image.findTemplate(pattern_image)
>>> found_patterns.draw()
>>> image.show()
**RETURNS**
This method returns a FeatureSet of TemplateMatch objects.'

def findTemplate(self, template_image=None, threshold=5, method='SQR_DIFF_NORM', grayscale=True, rawmatches=False):

    if (template_image == None):
        logger.info('Need    image    for    matching')
        return
    if (template_image.width > self.width):
        return
    if (template_image.height > self.height):
        logger.info('Image    too    tall')
        return
    check = 0
    if ((method is None) or (method == '') or (method == 'SQR_DIFF_NORM')):
        method = cv.CV_TM_SQDIFF_NORMED
        check = 1
    elif (method == 'SQR_DIFF'):
        method = cv.CV_TM_SQDIFF
        check = 1
    elif (method == 'CCOEFF'):
        method = cv.CV_TM_CCOEFF
    elif (method == 'CCOEFF_NORM'):
        method = cv.CV_TM_CCOEFF_NORMED
    elif (method == 'CCORR'):
        method = cv.CV_TM_CCORR
    elif (method == 'CCORR_NORM'):
        method = cv.CV_TM_CCORR_NORMED
    else:
        logger.warning("ooops..    I    don't    know    what    template    matching    method    you    are    looking    for.")
        return None
    matches = cv.CreateMat(((self.height - template_image.height) + 1), ((self.width - template_image.width) + 1), cv.CV_32FC1)
    if grayscale:
        cv.MatchTemplate(self._getGrayscaleBitmap(), template_image._getGrayscaleBitmap(), matches, method)
    else:
        cv.MatchTemplate(self.getBitmap(), template_image.getBitmap(), matches, method)
    mean = np.mean(matches)
    sd = np.std(matches)
    if (check > 0):
        compute = np.where((matches < (mean - (threshold * sd))))
    else:
        compute = np.where((matches > (mean + (threshold * sd))))
    mapped = map(tuple, np.column_stack(compute))
    fs = FeatureSet()
    for location in mapped:
        fs.append(TemplateMatch(self, template_image, (location[1], location[0]), matches[(location[0], location[1])]))
    if rawmatches:
        return fs
    finalfs = FeatureSet()
    if (len(fs) > 0):
        finalfs.append(fs[0])
        for f in fs:
            match = False
            for f2 in finalfs:
                if f2._templateOverlaps(f):
                    f2.consume(f)
                    match = True
                    break
            if (not match):
                finalfs.append(f)
        for f in finalfs:
            f.rescale(template_image.width, template_image.height)
        fs = finalfs
    return fs
