我以为我可以自己完成这个项目!
因此,我一直在开发自己的项目,需要在图像中找到一个/多个对象(目前,我正在使用简单的模板匹配)。
当找到一个对象时,我想要删除该区域的像素并使其透明或填充任何颜色。
例如,我有这张图片(我想要找到可乐瓶藤):
运行对象检测脚本后,我得到了:
您可以看到红色矩形内的匹配对象!
现在,我想做的是删除这个矩形区域并将其变成透明或填充任何颜色!
我已经尝试了很多方法,仍在尝试,但没有成功。这是我到目前为止的进展:
import numpy as np
import argparse
import imutils
import glob
import cv2
from matplotlib import pyplot as plt
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--template", required=True, help="Path to template image")
ap.add_argument("-i", "--images", required=True,
help="Path to images where template will be matched")
ap.add_argument("-v", "--visualize",
help="Flag indicating whether or not to visualize each iteration")
args = vars(ap.parse_args())
def match_and_count(template, image):
img_rgb = cv2.imread(image)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(template,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
f = set()
sensitivity = 100
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
# I want to make each rectangle transparent here
f.add((round(pt[0]/sensitivity), round(pt[1]/sensitivity)))
cv2.imwrite('multiple_objects.jpg',img_rgb)
print("Occurence of Object: %s" % len(f))
match_and_count(args["template"], args["images"])
如果有人能够给出一个提示或一段代码,实现同样的功能。我将非常感激,谢谢。