Template matching - OpenCV 3.4 with python 3 Tutorial 20

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024

ความคิดเห็น • 51

  • @changyongkang7651
    @changyongkang7651 5 ปีที่แล้ว +1

    thank you^^ from s. korea

  • @pfever
    @pfever 5 ปีที่แล้ว +2

    Thank you for the tutorials! In 7:08 I think it would have been better to find the maximum value in the max value in the result instead of just a threshold

  • @harmindersinghnijjar
    @harmindersinghnijjar 3 ปีที่แล้ว

    That image is crisp.

  • @more_u-listen_more_u_love9543
    @more_u-listen_more_u_love9543 5 ปีที่แล้ว +1

    Can you please tell me one thing if I am going to use a different template which is not associated with the original or input picture then how can I detect or print a message that ""Template Is not matched ""

  • @mohamedfatih8879
    @mohamedfatih8879 5 ปีที่แล้ว +5

    how can i solve this please
    AttributeError: 'NoneType' object has no attribute 'shape'

    • @evelynlau1929
      @evelynlau1929 4 ปีที่แล้ว

      most probably cv2 fails to read the image, so None is returned. Try check your image path.

  • @tymothylim6550
    @tymothylim6550 3 ปีที่แล้ว

    Thank you very much for this video! I enjoyed it :)

  • @APatInTheHat
    @APatInTheHat 6 ปีที่แล้ว

    Thank you for the awesome tutorial!

  • @naghechebilal7269
    @naghechebilal7269 6 ปีที่แล้ว

    thank you for sharing your knowledge

  • @agungbachtiars7188
    @agungbachtiars7188 6 ปีที่แล้ว +1

    Can we add more template image? So we can detect more different object. And give different color of rectangle or text when object detected?

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      yes you could use cv2.matchTemplate as many times as you want with a different template and then draw a rectangle with different color

    • @penguin2875
      @penguin2875 5 ปีที่แล้ว

      Hi, after added cv2.matchTemplate again with another image, should I write 'frame2'?

  • @sultanmansour639
    @sultanmansour639 4 ปีที่แล้ว

    Thanks For your great video!

  • @mathismind
    @mathismind 5 ปีที่แล้ว

    Love you! Ty for tutorials!

  • @NiteshSingh-cm1md
    @NiteshSingh-cm1md 5 ปีที่แล้ว

    Sir can You please help me out in determining that point 0.8 you used in result>=0.8 for the while point...

  • @BlackBirdFactories
    @BlackBirdFactories 5 ปีที่แล้ว

    Nice video, thank you sir!

  • @more_u-listen_more_u_love9543
    @more_u-listen_more_u_love9543 5 ปีที่แล้ว +1

    Awsome

  • @mayconvsg
    @mayconvsg 3 ปีที่แล้ว

    How can I check if a piece of a print I took earlier exists in the current print?
    For example, I took a print of my desktop and saved it to a folder. This print shows a calculator.
    Then I took another print with the browser open without showing a calculator.
    Now, I want the program to tell me (without showing the images on the screen, all under the covers) if the calculator that exists in the 1st print, exists in the 2nd print?
    I'm trying to use Open CV's Model Matching, but I'm still not clear on how to do that, because the tutorials I see all show the image on the screen and circle the point that the other detection image found... I don't want to circle nothing, except the program tells me if it found a calculator or not...

    • @mayconvsg
      @mayconvsg 3 ปีที่แล้ว

      I would also like to be able to do this through the saved image and not the current moment on my screen... In other words, I didn't want to have to use "pyautogui.locateOnScreen" :/

  • @riyushachannel4761
    @riyushachannel4761 2 ปีที่แล้ว

    It work thanks 🙏

  • @ligajapina1283
    @ligajapina1283 6 ปีที่แล้ว +1

    Is it possible to make a circle around template with using cv2.circle()?

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      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.
      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)

  • @fabriciobichara6542
    @fabriciobichara6542 5 ปีที่แล้ว

    thank u for this tutorial!

  • @yashgokhale6010
    @yashgokhale6010 4 ปีที่แล้ว

    Hello Sir , is it possible to send the matched image onto the web through django? If yes then could you suggest me how to do so?, Thanks

  • @_hkt_
    @_hkt_ 6 ปีที่แล้ว +1

    Sir, Kindly teach how to load multiple templates for the comparison of multiple objects.

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว +2

      you need to add them individually:
      #FiRST template
      template = cv2.imread("barts_face.jpg", cv2.IMREAD_GRAYSCALE)
      w, h = template.shape[::-1]
      result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
      loc = np.where(result >= 0.4)
      for pt in zip(*loc[::-1]):
      cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 3)
      # SECOND TEMPLATE
      template2 = cv2.imread("barts_face.jpg", cv2.IMREAD_GRAYSCALE)
      w2, h2 = template.shape[::-1]
      result2 = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
      loc2 = np.where(result >= 0.4)
      for pt in zip(*loc2[::-1]):
      cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 3)

    • @_hkt_
      @_hkt_ 6 ปีที่แล้ว

      Thank You So much sir, it worked. It was a great help.

    • @rahulpillai6415
      @rahulpillai6415 4 ปีที่แล้ว

      @@pysource-com instead of np.where() and for loop to find the location of the point
      you could use cv2.minmaxloc() to get the location it saves you from lines of code

  • @jules.anderson3416
    @jules.anderson3416 4 ปีที่แล้ว

    @Pysource ist there a way it can detect the pencil when its in any position? like horozontal etc. ?

    • @pysource-com
      @pysource-com  4 ปีที่แล้ว

      Not with this method, it's fine to use it only if you have equals template to match, it's not good at all for object detection in a picture

    • @jules.anderson3416
      @jules.anderson3416 4 ปีที่แล้ว

      @@pysource-com ​ Pysource ok. So is there any method you could recommend me? I have to localize parts that have been laser cut out of a big metal sheet for my thesis. I think its way easier that that - only 2D, clear contures, same perspective and I have a template. But it has to work with rotation. I tried your tutorials with meanshift and camshift but as you say yourself, they are not relieable enough. And your homography tutorial doesnt work anymore because that cv2.xfeatures2d.SIFT_create() function is no longer supported somehow. So can you help me with that? I'd even pay you some bucks. Greetings from Germany

  • @srinivasang4697
    @srinivasang4697 6 ปีที่แล้ว

    is there any video for measuring the width,length through pixel calculation

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      there isn't but I will take it into consideration for next tutorials. thanks

  • @andreasrigas3140
    @andreasrigas3140 5 ปีที่แล้ว

    Why do you convert both image in gray-scale could you please explain? Why we dont need colours?

    • @kennethbolmelund6302
      @kennethbolmelund6302 5 ปีที่แล้ว +1

      Color means more data to process.
      more data to process means longer computation times.
      longer computation times means longer waiting times before the program finds the template.
      Then again, less data to process means it's a bigger chance of the program finding the pattern in things that are wrong.
      So you compromise slightly less accuracy with way faster detection times.

  • @Khufos
    @Khufos 3 ปีที่แล้ว

    hey bro, how can we get the location of the rectangle and put it out of the loop In real time ?

    • @pysource-com
      @pysource-com  3 ปีที่แล้ว

      check one of my latest videos "Crop images" and you'll know how to extract the portion of an image once you have the coordinates

  • @Kage1128
    @Kage1128 6 ปีที่แล้ว

    How do i capture the screen instead of a camera?

  • @budoray
    @budoray 6 ปีที่แล้ว

    I am using this code for a personal project to convert images of chess positions to PGN notation. This code has helped me accurately identify light and dark pieces on light and dark squares as expected and without error. Many thanks. Now, how do I convert the locations of the values found in the code -- for pt in zip(*loc[::-1] -- to a row and column value? My loc variable is of size two (found two rooks), but my pt variable has many points for rectangles. It appears that multiple rectangles are drawn around the two rooks though the perception is that of two rectangles in the resulting image.
    Finding the midpoint for each of the rectangles is trivial and I am thinking this is how I will have to interpret board coordinates in to a1 and a8 for two white rooks on their starting square. Maybe there is an easier way to determine row, column values than this? My advantage is that I know the height and width of the board position images (800x800) and the piece images are (100x100). I have images for each piece on dark and light squares - 24 images - to make matching more error proof. Any guidance is greatly appreciated.

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      HI Ray, considering that in your specific case you can get the exact position using template matching, you can simply create an array with the top left point of each square.
      You will have an array with 24 points.
      For example:
      chass_squares=[(0,0), (101, 0), (201, 0)...]
      on the loop "for pt in zip(*loc[::-1]):" you can compare the point found with that array and then you can get the exact square.
      Hope it's clear. Acqually it's much easier to do it than explain it :)

    • @budoray
      @budoray 6 ปีที่แล้ว

      Yes. Thank you. I worked out something similar last night. I know the center point of each square so I check to see if that point is contained within the found rectangle. A bit procedural, but it gets the job done for now so that I can keep moving forward and continue learning.
      My images are black and white. Well, many shades of black and many shades of white. If you have a tutorial on sharpening grayscale images so that I can just deal with true black and true white, then please share the link.

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      There is a specific video about that, it's called thresholding.
      you can check it here th-cam.com/video/rbqA5UFXQoI/w-d-xo.html

  • @karthikmp7364
    @karthikmp7364 4 ปีที่แล้ว

    Why do we have invert shape[::-1]??

    • @cvcvka588
      @cvcvka588 4 ปีที่แล้ว

      because the 'shape' command takes the height and the width and we reverse it so we take the width before the height

  • @wpontog
    @wpontog 6 ปีที่แล้ว

    hi, do you have a tutorial of real time template matching with video not picture?

    • @pysource-com
      @pysource-com  6 ปีที่แล้ว

      there is already in this video, please look it entirely. Video template matching starts at 13:55

    • @wpontog
      @wpontog 6 ปีที่แล้ว

      Thanks, sorry and congratulations on your videos.

  • @martinahuber8494
    @martinahuber8494 5 ปีที่แล้ว

    could you share a prepared file? I'm a total beginner.

    • @martinahuber8494
      @martinahuber8494 5 ปีที่แล้ว

      and where do you save the images? in which folder?