Advertisement
SS_B-Rabbit

Template matching V1.0

Apr 14th, 2021 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5. import imutils
  6.  
  7. def detect_one(img_gray, template):
  8.     # template = cv2.Canny(template, 50, 250)
  9.     # img_gray = cv2.Canny(img_gray, 50, 250)
  10.     res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
  11.  
  12.     min_Val, max_Val, min_loc, max_loc = cv2.minMaxLoc(res)
  13.     print(min_Val, min_loc)
  14.  
  15.     width, height = template.shape
  16.     stanga_sus = min_loc
  17.     dreapta_jos = (stanga_sus[0] + width, stanga_sus[1] + height)
  18.     cv2.rectangle(img_gray, stanga_sus, dreapta_jos, (255, 0, 0), 2)
  19.  
  20.     cv2.imshow("gasit", img_gray)
  21.     cv2.waitKey(0)
  22.     cv2.destroyAllWindows()
  23.  
  24. def detect_multe(img_gray, template, threshold = 0.33):
  25.     res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
  26.  
  27.     loc = np.where(res > threshold)
  28.     width, height = template.shape
  29.  
  30.     for avion in zip(*loc[::-1]):
  31.         cv2.rectangle(img_gray, avion, (avion[0] + width, avion[1] + height), (255, 0 ,0), 2)
  32.    
  33.     cv2.imshow("gasit", img_gray)
  34.     cv2.waitKey(0)
  35.     cv2.destroyAllWindows()
  36.  
  37. def detect_toate(img_gray, template, threshold = 0.66):
  38.  
  39.     for avion in range(10, 250):
  40.         resized = cv2.resize(template, (avion + 1, avion))
  41.         res = cv2.matchTemplate(img_gray, resized, cv2.TM_CCOEFF_NORMED)
  42.  
  43.         loc = np.where(res > threshold)
  44.         width, height = resized.shape
  45.        
  46.         for altceva in zip(*loc[::-1]):
  47.             cv2.rectangle(img_gray, altceva, (altceva[0] + width, altceva[1] + height), (255, 0 ,0), 2)
  48.  
  49.     cv2.imshow("Toate", img_gray)
  50.     cv2.waitKey(0)
  51.     cv2.destroyAllWindows()
  52.  
  53. def scaler(img_gray, template, threshold = 0.533, MinScale = 0.5, MaxScale = 3.5):
  54.  
  55.     """
  56.    Peform template matching with a touch of rescaling
  57.    
  58.    Threshold = between 0 and 1, just play with it
  59.  
  60.    MinScale  = the minimum value of the rescaling percentage. Make it smaller than 1 if you want to detect objects bigger than the one in the template.
  61.  
  62.    MaxScale  = the maximum value of the rescaling percentage. Make it bigger than 1 if you want to detect objects smaller than the one in the template.  
  63.    """
  64.     # img_gray = cv2.Canny(img_gray, 50, 200)
  65.     # template = cv2.Canny(template, 50, 200)
  66.     width, height = template.shape
  67.     for scale in np.linspace(MinScale, MaxScale)[::-1]:
  68.         resized = imutils.resize(img_gray, width = int(img_gray.shape[1] * scale))
  69.         r = img_gray.shape[1] / float(resized.shape[1])
  70.  
  71.         if resized.shape[0] < height or resized.shape[1] < width:
  72.             break
  73.  
  74.         result = cv2.matchTemplate(resized, template, cv2.TM_CCOEFF_NORMED)
  75.         _, maxVal, _, maxLoc = cv2.minMaxLoc(result)
  76.  
  77.         if maxVal > threshold:
  78.             gasit = (maxVal, maxLoc, r)
  79.             _, maxLoc, r = gasit
  80.             (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
  81.             (endX, endY) = (int((maxLoc[0] + width) * r), int((maxLoc[1] + height) * r))
  82.             cv2.rectangle(img_gray, (startX, startY), (endX, endY), (0, 0, 255), 2)
  83.  
  84.     cv2.imshow("CFR", img_gray)
  85.     cv2.waitKey(0)
  86.     cv2.destroyAllWindows()
  87.  
  88. if __name__ == '__main__':
  89.     img_rgb = cv2.imread('SourceIMG.jpeg')
  90.     img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  91.     template = cv2.imread('TemplateIMG.jpeg', 0)
  92.  
  93.     # detect_one(img_gray, template)
  94.     # detect_multe(img_gray, template, 0.5)
  95.     # detect_toate(img_gray, template, .67)
  96.     scaler(img_gray, template)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement