使用opencv python从视频检测图像时如何忽略背景?

问题描述

我已经训练了一张图像(普通纸),因此使用录像中的图像,我需要找到距离,速度和加速度。我找到了所有这些东西。但是我的视频被许多不同的物体检测到。我只想检测我的普通纸。我该怎么办?

解决方法

您可以使用template-matching

  1. 您具有以下纸张图像(模板):

  • enter image description here

  1. 应用Canny边缘检测来找到图像或video-frame的边缘。

  • 假设下面是您的图片:

  • enter image description here

  • edged = cv2.Canny(resized,50,200)
    
  • resized是灰度和缩放的框架。您可以在下面的代码中查看说明。

  • enter image description here


  1. 使用matchTemplate
  2. 查找匹配的纸张

  • result = cv2.matchTemplate(edged,template,cv2.TM_CCOEFF)
    
  • 您不必使用cv2.TM_CCOEFF。您可以在here

    中找到不同的模式
  • 结果:

  • enter image description here

代码:


import numpy as np
import imutils
import glob
import cv2

template = cv2.imread("template.jpg")
template = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template,200)
(h,w) = template.shape[:2]

for imagePath in glob.glob("img2" + "/pXobJ.jpg"):
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    found = None

    for scale in np.linspace(0.2,1.0,20)[::-1]:
        resized = imutils.resize(gray,width=int(gray.shape[1] * scale))
        r = gray.shape[1] / float(resized.shape[1])

        if resized.shape[0] < h or resized.shape[1] < w:
            break

        edged = cv2.Canny(resized,200)
        result = cv2.matchTemplate(edged,cv2.TM_CCOEFF)
        (_,maxVal,_,maxLoc) = cv2.minMaxLoc(result)

        if found is None or maxVal > found[0]:
            found = (maxVal,maxLoc,r)

    (_,r) = found
    (startX,startY) = (int(maxLoc[0] * r),int(maxLoc[1] * r))
    (endX,endY) = (int((maxLoc[0] + w) * r),int((maxLoc[1] + h) * r))

    cv2.rectangle(image,(startX,startY),(endX,endY),(0,255),2)
    cv2.imwrite("img2/out.jpg",image)
    print("Table coordinates: ({},{},{})".format(startX,startY,endX,endY))