2012-02-11 11:47:40 +08:00
|
|
|
#!/usr/bin/env python
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
2015-12-13 09:43:58 +08:00
|
|
|
mouse_and_match.py [-i path | --input path: default ../data/]
|
2012-02-11 11:47:40 +08:00
|
|
|
|
|
|
|
Demonstrate using a mouse to interact with an image:
|
|
|
|
Read in the images in a directory one by one
|
|
|
|
Allow the user to select parts of an image with a mouse
|
|
|
|
When they let go of the mouse, it correlates (using matchTemplate) that patch with the image.
|
2015-12-13 09:43:58 +08:00
|
|
|
|
|
|
|
SPACE for next image
|
2012-02-11 11:47:40 +08:00
|
|
|
ESC to exit
|
|
|
|
'''
|
2015-09-14 00:00:22 +08:00
|
|
|
|
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-02-11 11:47:40 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# built-in modules
|
2012-02-11 11:47:40 +08:00
|
|
|
import os
|
2013-03-06 14:41:02 +08:00
|
|
|
import sys
|
2012-02-11 11:47:40 +08:00
|
|
|
import glob
|
|
|
|
import argparse
|
2013-03-06 14:41:02 +08:00
|
|
|
from math import *
|
|
|
|
|
2012-02-11 11:47:40 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
class App():
|
|
|
|
drag_start = None
|
|
|
|
sel = (0,0,0,0)
|
2012-02-11 11:47:40 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def onmouse(self, event, x, y, flags, param):
|
|
|
|
if event == cv.EVENT_LBUTTONDOWN:
|
|
|
|
self.drag_start = x, y
|
|
|
|
self.sel = (0,0,0,0)
|
|
|
|
elif event == cv.EVENT_LBUTTONUP:
|
|
|
|
if self.sel[2] > self.sel[0] and self.sel[3] > self.sel[1]:
|
|
|
|
patch = self.gray[self.sel[1]:self.sel[3], self.sel[0]:self.sel[2]]
|
|
|
|
result = cv.matchTemplate(self.gray, patch, cv.TM_CCOEFF_NORMED)
|
|
|
|
result = np.abs(result)**3
|
|
|
|
_val, result = cv.threshold(result, 0.01, 0, cv.THRESH_TOZERO)
|
|
|
|
result8 = cv.normalize(result, None, 0, 255, cv.NORM_MINMAX, cv.CV_8U)
|
|
|
|
cv.imshow("result", result8)
|
|
|
|
self.drag_start = None
|
|
|
|
elif self.drag_start:
|
|
|
|
#print flags
|
|
|
|
if flags & cv.EVENT_FLAG_LBUTTON:
|
|
|
|
minpos = min(self.drag_start[0], x), min(self.drag_start[1], y)
|
|
|
|
maxpos = max(self.drag_start[0], x), max(self.drag_start[1], y)
|
|
|
|
self.sel = (minpos[0], minpos[1], maxpos[0], maxpos[1])
|
|
|
|
img = cv.cvtColor(self.gray, cv.COLOR_GRAY2BGR)
|
|
|
|
cv.rectangle(img, (self.sel[0], self.sel[1]), (self.sel[2], self.sel[3]), (0,255,255), 1)
|
|
|
|
cv.imshow("gray", img)
|
|
|
|
else:
|
|
|
|
print("selection is complete")
|
|
|
|
self.drag_start = None
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def run(self):
|
|
|
|
parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')
|
|
|
|
parser.add_argument("-i","--input", default='../data/', help="Input directory.")
|
|
|
|
args = parser.parse_args()
|
|
|
|
path = args.input
|
|
|
|
|
|
|
|
cv.namedWindow("gray",1)
|
|
|
|
cv.setMouseCallback("gray", self.onmouse)
|
|
|
|
'''Loop through all the images in the directory'''
|
|
|
|
for infile in glob.glob( os.path.join(path, '*.*') ):
|
|
|
|
ext = os.path.splitext(infile)[1][1:] #get the filename extension
|
|
|
|
if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":
|
|
|
|
print(infile)
|
2015-12-13 09:43:58 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
img = cv.imread(infile,1)
|
|
|
|
if img is None:
|
|
|
|
continue
|
|
|
|
self.sel = (0,0,0,0)
|
|
|
|
self.drag_start = None
|
|
|
|
self.gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
|
|
|
cv.imshow("gray", self.gray)
|
|
|
|
if cv.waitKey() == 27:
|
|
|
|
break
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
print('Done')
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|
|
|
|
App().run()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|