Advertisement
Guest User

Untitled

a guest
May 16th, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # Python program to illustrate
  2. # template matching
  3. import cv2
  4. import numpy as np
  5.  
  6. # Read the main image
  7. img_rgb = cv2.imread('6image.png')
  8.  
  9. # Convert it to grayscale
  10. img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  11.  
  12. # Read the template
  13. template = cv2.imread('6template.png', 0)
  14.  
  15. # Store width and heigth of template in w and h
  16. w, h = template.shape[::-1]
  17.  
  18. # Perform match operations.
  19. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
  20.  
  21. # Specify a threshold
  22. threshold = 0.1
  23.  
  24. # Store the coordinates of matched area in a numpy array
  25. loc = np.where(res >= threshold)
  26.  
  27. # Draw a rectangle around the matched region.
  28. for pt in zip(*loc[::-1]):
  29.     cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
  30.  
  31. # Show the final image with the matched area.
  32. cv2.imshow('Detected', img_rgb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement