1 import cv2 2 import numpy as np 3 def fan() : 4 f=open("picture.txt","r") 5 a = [] 6 b = [] 7 for eachline in f: 8 eachline = eachline.strip() 9 eachline = eachline.split(" ") 10 for i in eachline: 11 i = str(i)12 b.append(i)13 if len(b) == 4:14 a.append(b)15 b = []16 17 i_mg = cv2.imread("jiangnan.jpg")18 i=019 for picture in a :20 21 x = int(picture[0])22 y = int(picture[1])23 w = int(picture[2])24 h = int(picture[3])25 i+=126 I_mg = i_mg[y:h,x:w] 27 c = cv2.imwrite(str(i)+".jpg", I_mg)28 return29 #获取图像数据30 def on_mouse(event ,x,y,flag,param):31 global events32 global img33 34 if events[event]=='CV_EVENT_LBUTTONDOWN':35 print "(%d,%d):" % (y,x) 36 print img[y,x,:]37 c=str(y)38 d=str(x)39 40 cv2.imshow("img", img)41 l.write(c)42 l.write(" ")43 l.write(d)44 l.write(" ") 45 return46 47 l=open("picture.txt","w")48 f=open("picture.txt","r")49 img = cv2.imread("jiangnan.jpg")50 51 events = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN',52 'CV_EVENT_RBUTTONDOWN','CV_EVENT_MBUTTONDOWN', 53 'CV_EVENT_LBUTTONUP','CV_EVENT_RBUTTONUP',54 'CV_EVENT_MBUTTONUP' , 'CV_EVENT_LBUTTONDBLCLK',55 'CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']56 57 #def getdistance(co1,co2):58 #return np.sqrt(sum((co1-co2)*(co1-co2)))59 60 cv2.namedWindow('img')61 cv2.setMouseCallback('img',on_mouse,1)62 cv2.imshow("img", img)63 64 cv2.waitKey(0)65 l.close()66 cv2.destroyAllWindows()67 fan()