1

EDIT:

template url: http://c.nordstromimage.com/Assets/IDEV/common/00-00-00-free-shipping-evergreen-cid0330153898-7-adam-a4c331d8-38ab-4337-baf9-a46801899b3e-fil-file.png?Version=1

image url:http://shop.nordstrom.com/

I read through a sample tutorial here on template matching. Basically template matching is a technique used to identify/locate a small patch image (aka "template") within a larger image. And here is the easy-to-read documentation explaining the input/outputs to using the template_matching library in python.

Now here is my question. The output of the template_matching library is supposed to be a numpy array consisting of the correlation coefficient values. These coefficients are supposed to be values between -1.0 and 1.0 but occasionally I would have values > 1.0 in my output array. Below is the code I am using (it's almost the same as the one provided in the tutorial):

def template_match(self):

    #self.main_image is actually a screenshot of [this](http://shop.nordstrom.com/) webpage taken in selenium
    #self.template is [this](http://c.nordstromimage.com/Assets/IDEV/common/00-00-00-free-shipping-evergreen-cid0330153898-7-adam-a4c331d8-38ab-4337-baf9-a46801899b3e-fil-file.png?Version=1)  image

    image = rgb2gray(imread(self.main_image))
    template = rgb2gray(imread(self.template))

    result = match_template(image, template) #call the scikit library here

    ij = np.unravel_index(np.argmax(result), result.shape)
    x, y = ij[::-1] #coordinate positions


    h_template, w_template = template.shape #height and width of image
    aspect_ratio = float(w_template)/float(h_template)
    confidence = np.amax(result)

    self.log.info(self.template_url)
    self.log.info('Template Coordinates: {0}, {1}'.format(x, y))
    self.log.info('Image Width x Height Pixel Size: {0} || Template Width x Height Pixel Size: {1}'.format(image.shape[::-1], template.shape[::-1]))
    self.log.info('Aspect Ratio: {0}'.format(aspect_ratio))
    self.log.info('Confidence-Level: {0}\n'.format(confidence))

    #filter out images with low confidence-levels 
    if confidence < self.min_confidence: 
        return

    #HERE IS WHERE I AM CONFUSED, according to the docs, result should be an array of values between -1.0 and 1.0, yet I get values > 1
    elif confidence > 1:
        self.log.info('WHY IS THIS CONFIDENCE VALUE > 1.0? URL: {0}'.format(self.template_url))
        return

    return

What I am trying to do here is keep values whose correlation coefficient (in my case, I called it "confidence") is above some threshold, specified by self.min_confidence, and reject those below the threshold. Yet for some reason, some template images (such as the one in this example) would produce a correlation coefficient > 1. What is going on here?

lollerskates
  • 964
  • 1
  • 11
  • 28

0 Answers0