Skip to content

Beginners Opencv · Tutorials

Template matching – OpenCV 3.4 with python 3 Tutorial 20

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

16 comments

  1. Sir,
    I get the following error….
    How to proceed res=cv2.matchTemplate(cap1,template,cv2.TM_CCOEFF_NORMED)
    TypeError: image is not a numpy array, neither a scalar

    1. You get this error when you don’t have any image.
      Make sure that the image or frame is loaded correctly, just try to display it using cv2.imshow(“cap1”, cap1)

    1. Yes, of course. As we know, to design the rectangle we need 2 points, top left and bottom right and we have both of them just adding with and height to the first point. To draw the circle we would need to get the center point, so we can just add half of the with and half of the height and we get it.

      [python]for pt in zip(*loc[::-1]):
      center_point_x = pt[0] + w/2
      center_point_y = pt[1] + h/2
      cv2.circle(img, (int(center_point_x), int(center_point_y)), int(w/2), (0, 255, 0), -1)
      [/python]

  2. hello,

    i have a raspberry pi and i need it to work for that.
    I need the code to match a template in live video feed using the rpi camera.
    can you give me a code for this?

    hope one off you can help me i really need it for a demo

  3. hello,

    i need the same script for running on a rpi3 with picam
    i tried almost everything without succes.
    it’s for a project i wanne make.

    please help me with the code.

  4. hello,
    i want two template
    template1 = cv2.imread(“pen.png”, cv2.IMREAD_GRAYSCALE)
    template2 = cv2.imread(“pen.png”, cv2.IMREAD_GRAYSCALE)
    How to matching
    ****
    res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED)

    Help me please.

  5. you can help me
    my problem
    Traceback (most recent call last):
    File “C:/Users/win10/OneDrive/xulyanh/thu nghiem.py”, line 6, in
    w, h = template.shape[::-1]
    AttributeError: ‘NoneType’ object has no attribute ‘shape’

  6. When the Template is present inside Image, then only it shows correct result.When the Template does not present inside Image then it will make rectangle of same size as template(template not found is not handled).How can I check the template is present inside Image or not ?

  7. img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    cv2.error: OpenCV(4.1.1) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cvtColor’

  8. hi,
    I was wondering how you could make a template match if the orientation of the picture is different, e.g. when it is rotated. How could this be achieved.

    sincerly,
    Thomas

Join the discussion