mouse_and_match.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. '''
  3. mouse_and_match.py [-i path | --input path: default ../data/]
  4. Demonstrate using a mouse to interact with an image:
  5. Read in the images in a directory one by one
  6. Allow the user to select parts of an image with a mouse
  7. When they let go of the mouse, it correlates (using matchTemplate) that patch with the image.
  8. SPACE for next image
  9. ESC to exit
  10. '''
  11. # Python 2/3 compatibility
  12. from __future__ import print_function
  13. import numpy as np
  14. import cv2 as cv
  15. # built-in modules
  16. import os
  17. import sys
  18. import glob
  19. import argparse
  20. from math import *
  21. class App():
  22. drag_start = None
  23. sel = (0,0,0,0)
  24. def onmouse(self, event, x, y, flags, param):
  25. if event == cv.EVENT_LBUTTONDOWN:
  26. self.drag_start = x, y
  27. self.sel = (0,0,0,0)
  28. elif event == cv.EVENT_LBUTTONUP:
  29. if self.sel[2] > self.sel[0] and self.sel[3] > self.sel[1]:
  30. patch = self.gray[self.sel[1]:self.sel[3], self.sel[0]:self.sel[2]]
  31. result = cv.matchTemplate(self.gray, patch, cv.TM_CCOEFF_NORMED)
  32. result = np.abs(result)**3
  33. _val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)
  34. result8 = cv.normalize(result, None, 0, 255, cv.NORM_MINMAX, cv.CV_8U)
  35. cv.imshow("result", result8)
  36. self.drag_start = None
  37. elif self.drag_start:
  38. #print flags
  39. if flags & cv.EVENT_FLAG_LBUTTON:
  40. minpos = min(self.drag_start[0], x), min(self.drag_start[1], y)
  41. maxpos = max(self.drag_start[0], x), max(self.drag_start[1], y)
  42. self.sel = (minpos[0], minpos[1], maxpos[0], maxpos[1])
  43. img = cv.cvtColor(self.gray, cv.COLOR_GRAY2BGR)
  44. cv.rectangle(img, (self.sel[0], self.sel[1]), (self.sel[2], self.sel[3]), (0,255,255), 1)
  45. cv.imshow("gray", img)
  46. else:
  47. print("selection is complete")
  48. self.drag_start = None
  49. def run(self):
  50. parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')
  51. parser.add_argument("-i","--input", default='../data/', help="Input directory.")
  52. args = parser.parse_args()
  53. path = args.input
  54. cv.namedWindow("gray",1)
  55. cv.setMouseCallback("gray", self.onmouse)
  56. '''Loop through all the images in the directory'''
  57. for infile in glob.glob( os.path.join(path, '*.*') ):
  58. ext = os.path.splitext(infile)[1][1:] #get the filename extension
  59. if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":
  60. print(infile)
  61. img = cv.imread(infile,1)
  62. if img is None:
  63. continue
  64. self.sel = (0,0,0,0)
  65. self.drag_start = None
  66. self.gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  67. cv.imshow("gray", self.gray)
  68. if cv.waitKey() == 27:
  69. break
  70. print('Done')
  71. if __name__ == '__main__':
  72. print(__doc__)
  73. App().run()
  74. cv.destroyAllWindows()