【发布时间】:2020-05-24 01:35:42
【问题描述】:
我有一个使用 openCV 的小型 python 脚本,它在图像中进行模板匹配并很好地返回一个边界框,如下所示。该脚本也是大小不变的,这使其更加健壮。
鉴于返回的边界框,我如何用另一个模板替换它并保存更改后的图像?
这里是original template 和main image
现在我想简单地用下面调整大小的 template2 版本替换确切的框,并保存新图像。我该怎么做?
这是我的简单代码:
# USAGE
# python match.py --template cod_logo.png --images images
# import the necessary packages
import numpy as np
import argparse
import imutils
import glob
import cv2
# construct the argument parser and parse the arguments
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 dir where template will be matched")
ap.add_argument("-v", "--visualize",
help="Flag 0 or 1 indicating whether or not to visualize each iteration")
args = vars(ap.parse_args())
# load the image image, convert it to grayscale, and detect edges
template = cv2.imread(args["template"])
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
cv2.imshow("Template", template)
# loop over the images to find the template in
for imagePath in glob.glob(args["images"] + "/*.*"):
# load the image, convert it to grayscale, and initialize the
# bookkeeping variable to keep track of the matched region
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found = None
# loop over the scales of the image
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break
# detect edges in the resized, grayscale image and apply template
# matching to find the template in the image
edged = cv2.Canny(resized, 50, 200)
result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
# check to see if the iteration should be visualized
if args.get("visualize", False):
# draw a bounding box around the detected region
clone = np.dstack([edged, edged, edged])
cv2.rectangle(clone, (maxLoc[0], maxLoc[1]),
(maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)
cv2.imshow("Visualize", clone)
cv2.waitKey(0)
# if we have found a new maximum correlation value, then ipdate
# the bookkeeping variable
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
# unpack the bookkeeping varaible and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
(_, maxLoc, r) = found
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
(endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))
# draw a bounding box around the detected result and display the image
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
【问题讨论】:
-
那么,您只是想替换图像而不担心适当的混合,还是只想显示文本而其余的背景保持不变?后一项是一项涉及很多的任务。调整替换图像的大小和拟合应该相当简单,但您的图像会具有假图像的外观。
-
是的,现在只是简单的方法。
-
如果是这种情况,那么您的代码中几乎已经包含了所有内容。找到匹配模板后,您需要做的就是读取替换图像,将其调整为模板图像的大小(假设两者的纵横比相同,否则您可能必须调整它的大小以使替换完全隐藏模板) 然后执行类似 image[startY:endY, startX:endX, :] = replacement_img 的操作(大小为 endX-startX, endY-startY)。我误解了你想要做什么吗?
-
是的,我确实做到了,但使用了 PIL。我必须将 cv2 转换为 PIL 并反转。我正在视频帧上尝试它,但它非常不稳定。有什么方法可以消除抖动?
-
粘贴我在之前的一个 cmets 中已经提到过。这里是调整大小 img_resized = cv2.resize(img, (width, height),0,0,cv2.INTER_CUBIC)
标签: python opencv image-processing computer-vision template-matching