简体   繁体   中英

Python OpenCV matchTemplate with Masking - Found Matches at All Locations

Problem: The results I receive from matchTemplate indicate that I have matches at every location with a value of 1.0 .

Expected Results : I expected one location in results to have a much higher score than others locations.

Code:

def template_match(filename=base_name,
                   img_folder=trn_imgs_path,
                   templates=['wet_install.png',
                              'wet_install_cleaned.png',
                              'wet_install_tag.png',
                              'wet_install_tag_cleaned.png'],
                   template_path=template_path,
                   threshold=0.8,
                   save_dir=save_dir):
    ''' 
    Perform template matching on an input image using a few templates.
    It draws bounding boxes on a copy of the original image. 

    Args:
        filename (str): name of the file with the .svg extension
        img_folder (str): path to folder containing the images
        templates (list): list of template filenames to match against
        template_path (str): path to folder containing the templates
        threshold (float): the threshold for a match from template matching
        save_dir (str): path to folder to save results
    '''
    print('Working on file: {}.png'.format(filename))

    # load the original BGR image
    img_rgb  = cv2.imread(img_folder + filename + '.png')[5143:5296, 15169:15368] # TODO(mtu): Don't keep these indices here!
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)

    # loop over each template
    colors = [(0,0,255), (0,255,0), (255,255,0), (255,0,255)]
    for itemp in range(len(templates)):
        template_name = templates[itemp]
        print('Using Template: {}'.format(template_name))

        # load the template as grayscale and get its width and height
        template = cv2.imread(template_path + '{}'.format(template_name), 0)
        height, width = template.shape[:2]

        template  = cv2.adaptiveThreshold(template, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
        temp_mask = cv2.adaptiveThreshold(template, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1)

        # do template matching using grayscale image and find points above theshold
        results = cv2.matchTemplate(image=img_gray, templ=template, method=cv2.TM_CCORR_NORMED, mask=temp_mask)
        loc = np.where(results >= threshold)

        # draw rectangles on points above threshold on RGB image
        for pt in zip(*loc[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + width, pt[1] + height), colors[itemp%len(colors)], 5)

    # save the file with bounding boxes drawn on
    filename = save_dir + '{}_found.png'.format(filename) 
    print('Saving bounding boxes to: {}'.format(filename))
    cv2.imwrite(filename, img_rgb)

Comments:

  • Debug output shows what img_gray , template , and temp_mask visually look like
  • img_gray is just template with 10 extra pixel rows of white padding on top
  • template and temp_mask are the same shape and type
  • Saved output image

Inverting img_gray and template fixed the error.

The comparison metric I used was cv2.TM_CCORR_NORMED . This works by taking the dot product of img_gray and template where the binary numpy array temp_mask has value 1 .

In my sample image I wanted to match up black pixels in template against black pixels in img_gray , however the pixel value for black is 0 . Thus, the dot product at the location I wanted to detect was low.

By inverting img_gray and template I'm matching up white pixels in template against white pixels in img_gray . Since white has pixel value 255 , the dot product of white against white, template against image, becomes high at the location I want to detect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM