Advertisement
kalin729

lab3

May 30th, 2024
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | Source Code | 0 0
  1. from __future__ import print_function
  2. import sys
  3. import cv2 as cv
  4.  
  5. ## [global_variables]
  6. use_mask = False
  7. img = None
  8. templ = None
  9. mask = None
  10. image_window = "Source Image"
  11. result_window = "Result window"
  12.  
  13. match_method = 0
  14. max_Trackbar = 5
  15. ## [global_variables]
  16.  
  17. def main(argv):
  18.  
  19.     if (len(sys.argv) < 3):
  20.         print('Not enough parameters')
  21.         print('Usage:\nmatch_template_demo.py <image_name> <template_name> [<mask_name>]')
  22.         return -1
  23.  
  24.     ## [load_image]
  25.     global img
  26.     global templ
  27.     img = cv.imread(sys.argv[1], cv.IMREAD_COLOR)
  28.     templ = cv.imread(sys.argv[2], cv.IMREAD_COLOR)
  29.  
  30.     if (len(sys.argv) > 3):
  31.         global use_mask
  32.         use_mask = True
  33.         global mask
  34.         mask = cv.imread( sys.argv[3], cv.IMREAD_COLOR )
  35.  
  36.     if ((img is None) or (templ is None) or (use_mask and (mask is None))):
  37.         print('Can\'t read one of the images')
  38.         return -1
  39.     ## [load_image]
  40.  
  41.     ## [create_windows]
  42.     cv.namedWindow( image_window, cv.WINDOW_AUTOSIZE )
  43.     cv.namedWindow( result_window, cv.WINDOW_AUTOSIZE )
  44.     ## [create_windows]
  45.  
  46.     ## [create_trackbar]
  47.     trackbar_label = 'Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED'
  48.     cv.createTrackbar( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )
  49.     ## [create_trackbar]
  50.  
  51.     MatchingMethod(match_method)
  52.  
  53.     ## [wait_key]
  54.     cv.waitKey(0)
  55.     return 0
  56.     ## [wait_key]
  57.  
  58. def MatchingMethod(param):
  59.  
  60.     global match_method
  61.     match_method = param
  62.  
  63.     ## [copy_source]
  64.     img_display = img.copy()
  65.     ## [copy_source]
  66.     ## [match_template]
  67.     method_accepts_mask = (cv.TM_SQDIFF == match_method or match_method == cv.TM_CCORR_NORMED)
  68.     if (use_mask and method_accepts_mask):
  69.         result = cv.matchTemplate(img, templ, match_method, None, mask)
  70.     else:
  71.         result = cv.matchTemplate(img, templ, match_method)
  72.     ## [match_template]
  73.  
  74.     ## [normalize]
  75.     cv.normalize( result, result, 0, 1, cv.NORM_MINMAX, -1 )
  76.     ## [normalize]
  77.     ## [best_match]
  78.     _minVal, _maxVal, minLoc, maxLoc = cv.minMaxLoc(result, None)
  79.     ## [best_match]
  80.  
  81.     ## [match_loc]
  82.     if (match_method == cv.TM_SQDIFF or match_method == cv.TM_SQDIFF_NORMED):
  83.         matchLoc = minLoc
  84.     else:
  85.         matchLoc = maxLoc
  86.     ## [match_loc]
  87.  
  88.     ## [imshow]
  89.     cv.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
  90.     cv.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
  91.     cv.imshow(image_window, img_display)
  92.     cv.imshow(result_window, result)
  93.     ## [imshow]
  94.     pass
  95.  
  96. if __name__ == "__main__":
  97.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement