【问题标题】:Python OpenCV for template matching用于模板匹配的 Python OpenCV
【发布时间】:2020-05-24 01:35:42
【问题描述】:

我有一个使用 openCV 的小型 python 脚本,它在图像中进行模板匹配并很好地返回一个边界框,如下所示。该脚本也是大小不变的,这使其更加健壮。

鉴于返回的边界框,我如何用另一个模板替换它并保存更改后的图像?

这里是original templatemain 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


【解决方案1】:

我做了一些改变...

1] 我没有使用参数解析器

2] tepmlate2 是反恐精英。

3] Image2 是 COUNTER STRIKE ON TOP OF COD 的图像。

步骤:提取 roi(感兴趣区域),然后调整新的大小 相应的图像......,然后用新的调整大小的图像操作 roi, 将 roi 放回 image2 上。

优势 => 您可以通过以下方式更改 roi 和模板的不透明度 在 addWeighted 中更改 alpha 和 beta。

# 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

#New template
template2 = cv2.imread("template2.png")

# 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("template.png")
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("mainImage.jpg")
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))

#MY CODE
image2 = image.copy()
resizedTemplate = cv2.resize(template2, (endX-startX, endY-startY), interpolation = cv2.INTER_AREA)
roi = image2[startY:endY, startX:endX]
img = cv2.addWeighted(resizedTemplate, 1, roi, 0, 0)
image2[startY:endY, startX:endX] = img


# draw a bounding box around the detected result and display the image
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 128), 2)
cv2.imshow("Image", image)
cv2.imshow("Image2", image2)
#cv2.imshow("resizedTemplate", resizedTemplate)
cv2.waitKey(0)

results

【讨论】:

  • 谢谢。一个错误是因为\[Lexical error at line 80, column 9. Encountered: "[" (91), after : "\\"。删除了``并修复了。
  • 由于某种原因,在“[”和“]”之前添加了“\”。但是代码是正确的,你可以解决那些小错误:)。
【解决方案2】:

你能做的是……

1] 使用addWeighted 在原始图像之上放置另一个图像。

cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])

您也可以使用 cv2.add(),cv2.addWeighted() 用于混合和赋予不同的透明度。

注意:您要裁剪的区域大小和要替换的新图像的大小必须相同。

【讨论】:

  • 当然,请稍等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
相关资源
最近更新 更多