0

I have trained an image(plain paper) so using that from the video I need to find distance, velocity, acceleration. I found all those things. but my video is getting detected by many different objects. I want to detect only my plain paper. What should I need to do?

  • 1
    Please read the information guides in the **help center** (https://stackoverflow.com/help), in particular, "How to Ask A Good Question" (https://stackoverflow.com/help/how-to-ask) and "How to create a Minimal, Reproducible Example" (https://stackoverflow.com/help/minimal-reproducible-example). – fmw42 Sep 16 '20 at 17:24

1 Answers1

1

You can use template-matching.

  1. You have the following paper image (template):

  • enter image description here

  1. Apply Canny edge-detection to find the edges of the image or video-frame.

  • Assume below is your image:

  • enter image description here

  • edged = cv2.Canny(resized, 50, 200)
    
  • resized is a grayscale and scaled frame. You can see the description in the below code.

  • enter image description here


  1. Find the matched paper using matchTemplate

  • result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
    
  • You don't have to use cv2.TM_CCOEFF. You can find the different modes in here

  • Result:

  • enter image description here

Code:


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, 50, 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, 50, 200)
        result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
        (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

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

    (_, maxLoc, 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, 0, 255), 2)
    cv2.imwrite("img2/out.jpg", image)
    print("Table coordinates: ({}, {}, {}, {})".format(startX, startY, endX, endY))
Ahmet
  • 7,527
  • 3
  • 23
  • 47