#!/usr/bin/env python#-*- coding: utf-8 -*-#code:myhaspl@qq.comimport cv2import numpy as npfn="test1.jpg"print 'http://blog.csdn.net/myhaspl'print 'myhaspl@qq.com'printprint 'loading %s ...' % fnprint 'working',img = cv2.imread(fn)w=img.shape[1]h=img.shape[0]sz1=wsz0=hEVENTS = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN', 'CV_EVENT_RBUTTONDOWN', 'CV_EVENT_MBUTTONDOWN', 'CV_EVENT_LBUTTONUP', 'CV_EVENT_RBUTTONUP', 'CV_EVENT_MBUTTONUP' , 'CV_EVENT_LBUTTONDBLCLK','CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']def callback_function(event,x,y,flag,param): global EVENTS global img if EVENTS[event]=='CV_EVENT_LBUTTONDOWN': print "(%d,%d):"%(y,x) print img[y,x,:] cv2.imshow('img', img) returndef getdistance(color1,color2): return np.sqrt(sum((color1-color2)*(color1-color2)))cv2.namedWindow('img') cv2.setMouseCallback('img', callback_function)cv2.imshow('img', img) cv2.waitKey() cv2.destroyAllWindows()
python opencv 滑鼠事件