2012-02-11 11:47:40 +08:00
|
|
|
#!/usr/bin/env python
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
2012-02-11 11:47:40 +08:00
|
|
|
mouse_and_match.py [-i path | --input path: default ./]
|
|
|
|
|
|
|
|
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.
|
|
|
|
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
|
2013-11-24 22:58:13 +08:00
|
|
|
import cv2
|
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
|
|
|
|
|
|
|
drag_start = None
|
|
|
|
sel = (0,0,0,0)
|
|
|
|
|
|
|
|
def onmouse(event, x, y, flags, param):
|
|
|
|
global drag_start, sel
|
2013-04-12 21:39:16 +08:00
|
|
|
if event == cv2.EVENT_LBUTTONDOWN:
|
2012-02-11 11:47:40 +08:00
|
|
|
drag_start = x, y
|
|
|
|
sel = 0,0,0,0
|
2013-04-12 21:39:16 +08:00
|
|
|
elif event == cv2.EVENT_LBUTTONUP:
|
2012-02-11 11:47:40 +08:00
|
|
|
if sel[2] > sel[0] and sel[3] > sel[1]:
|
|
|
|
patch = gray[sel[1]:sel[3],sel[0]:sel[2]]
|
2013-04-12 21:39:16 +08:00
|
|
|
result = cv2.matchTemplate(gray,patch,cv2.TM_CCOEFF_NORMED)
|
2012-02-11 11:47:40 +08:00
|
|
|
result = np.abs(result)**3
|
2013-04-12 21:39:16 +08:00
|
|
|
val, result = cv2.threshold(result, 0.01, 0, cv2.THRESH_TOZERO)
|
|
|
|
result8 = cv2.normalize(result,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
|
|
|
|
cv2.imshow("result", result8)
|
2012-02-11 11:47:40 +08:00
|
|
|
drag_start = None
|
|
|
|
elif drag_start:
|
|
|
|
#print flags
|
2013-04-12 21:39:16 +08:00
|
|
|
if flags & cv2.EVENT_FLAG_LBUTTON:
|
2012-02-11 11:47:40 +08:00
|
|
|
minpos = min(drag_start[0], x), min(drag_start[1], y)
|
|
|
|
maxpos = max(drag_start[0], x), max(drag_start[1], y)
|
|
|
|
sel = minpos[0], minpos[1], maxpos[0], maxpos[1]
|
2013-04-12 21:39:16 +08:00
|
|
|
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
|
|
|
cv2.rectangle(img, (sel[0], sel[1]), (sel[2], sel[3]), (0,255,255), 1)
|
|
|
|
cv2.imshow("gray", img)
|
2012-02-11 11:47:40 +08:00
|
|
|
else:
|
2015-09-14 00:00:22 +08:00
|
|
|
print("selection is complete")
|
2012-02-11 11:47:40 +08:00
|
|
|
drag_start = None
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-02-11 11:47:40 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images')
|
|
|
|
parser.add_argument("-i","--input", default='./', help="Input directory.")
|
|
|
|
args = parser.parse_args()
|
|
|
|
path = args.input
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-04-12 21:39:16 +08:00
|
|
|
cv2.namedWindow("gray",1)
|
|
|
|
cv2.setMouseCallback("gray", onmouse)
|
2012-02-11 11:47:40 +08:00
|
|
|
'''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 extenstion
|
|
|
|
if ext == "png" or ext == "jpg" or ext == "bmp" or ext == "tiff" or ext == "pbm":
|
2015-09-14 00:00:22 +08:00
|
|
|
print(infile)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-04-12 21:39:16 +08:00
|
|
|
img=cv2.imread(infile,1)
|
2012-02-11 11:47:40 +08:00
|
|
|
if img == None:
|
|
|
|
continue
|
|
|
|
sel = (0,0,0,0)
|
|
|
|
drag_start = None
|
2013-04-12 21:39:16 +08:00
|
|
|
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
cv2.imshow("gray",gray)
|
|
|
|
if (cv2.waitKey() & 255) == 27:
|
2012-02-11 11:47:40 +08:00
|
|
|
break
|
2013-04-12 21:39:16 +08:00
|
|
|
cv2.destroyAllWindows()
|