繁体   English   中英

带遮罩的Python OpenCV matchTemplate-在所有位置都找到匹配项

[英]Python OpenCV matchTemplate with Masking - Found Matches at All Locations

问题:我从matchTemplate收到的结果表明我在每个位置都具有值1.0匹配项。

预期结果 :我希望在一个位置, results有高得多的分数比其他人的位置。

码:

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)

评论:

  • 调试输出显示img_graytemplatetemp_mask外观
  • img_gray只是template ,顶部有10个额外像素行的白色填充
  • templatetemp_mask具有相同的形状和类型
  • 保存的输出图像

反转img_graytemplate img_gray该错误。

我使用的比较指标是cv2.TM_CCORR_NORMED 通过采用img_graytemplate的点积进行工作,其中二进制numpy数组temp_mask值为1

在我的示例图像中,我想将template中的黑色像素与img_gray中的黑色像素进行匹配,但是black的像素值为0 因此,我要检测的位置的点积较低。

通过反转img_graytemplate我将template中的白色像素与img_gray中的白色像素进行匹配。 由于白色的像素值为255 ,因此白色相对于白色的点积,图像相对于模板的点积在我要检测的位置变高。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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