# -*- coding: utf-8 -*-"""Created on Fri Jan 17 09:01:01 2014@author: xiaoxia"""# -*- coding: utf-8 -*-"""Created on Wed Jan 15 10:14:43 2014@author: xiaoxia"""import cv2 #将数据存为小图片 def fan() : f=open("picture.txt","r") l_2=open("picture2.txt","w") k = [] u = [] for eachline in f: eachline = eachline.strip() eachline = eachline.split(" ") for i in eachline: i = str(i) k.append(i) if len(k) == 4: u.append(k) k = [] i_mg = cv2.imread("jiangnan.jpg") i=0 for picture in u : x = int(picture[0]) y = int(picture[1]) w = int(picture[2]) h = int(picture[3]) i+=1 I_mg = i_mg[y:h,x:w] c = cv2.imwrite(str(i)+".jpg", I_mg)# return for z in u: for x in z : x=str(x) l_2.write(x) l_2.write(" ") l_2.write("\n") l.close() f.close()##################################################################### def onMouseEvent(event,x,y,flags,param): global startPointx global startPointy global flagDraw global i #鼠标左键按下响应 if(event==1): #第一点 print "Position is: %d,%d" ,x,y startPointx = x print type(x) startPointy = y i=i+1 cv2.putText(image,str(i),(startPointx,startPointy),2,1,(249,99,201)) flagDraw = True a = str(x) b = str(y) l.write(a) l.write(" ") l.write(b) l.write(" ") #鼠标移动响应 if(event==0): #滑动过程 if(flagDraw==True): imgc = cv2.copyMakeBorder(image,0,0,0,0,cv2.BORDER_CONSTANT) cv2.rectangle(imgc, (startPointx,startPointy), (x,y), (249,99,201),2) #cv2.putText(imgc,(x,y),CV_FONT_HERSHEY_PLAIN,2.0,(255,0,0),thickness = 2) cv2.imshow('rectangle', imgc) print "EndPosition is: %d,%d" ,x,y #鼠标左键抬起响应 if(event==4): #第二点 if(flagDraw==True): cv2.rectangle(image, (startPointx,startPointy), (x,y), (249,99,201),2) #cv2.imshow('rectangle_1', image) flagDraw = False c = str(x) d = str(y) l.write(c) l.write(" ") l.write(d) l.write(" ")######################################################################## image = cv2.imread("jiangnan.jpg") global startPointx global startPointy global flagDrawglobal ii=0 startPointx = 0 startPointy = 0 flagDraw = Falsecv2.namedWindow("rectangle",cv2.WINDOW_NORMAL)#cv2.namedWindow("rectangle_1",cv2.WINDOW_NORcv2.imshow('rectangle',image) if __name__ == '__main__': image = cv2.imread("jiangnan.jpg") h, w = image.shape[:2] imgc = cv2.copyMakeBorder(image, 0,0,0,0, cv2.BORDER_CONSTANT) # cv2.imshow('rectangle', imgc) l=open("picture.txt","w") f=open("picture.txt","r") while True: cv2.setMouseCallback('rectangle', onMouseEvent) #调用滑鼠 c = cv2.waitKey() if c == 27 or c == ord('q'): break if c == ord('w') : print "w" l.close() a=[] b=[] for eachline in f: eachline=eachline.strip() eachline=eachline.split(" ") for k in eachline: k=str(k) b.append(k) if len(b)==4: a.append(b) b=[] print a g=a[:-1] print g l=open("picture.txt","w") f=open("picture.txt","r") for t in g: for k in t : k=str(k) l.write(k) l.write(" ") l.write("\n") print "yes:",g image = cv2.imread("jiangnan.jpg") ii=0 for q in g: xa=int(q[0]) ya=int(q[1]) xb=int(q[2]) yb=int(q[3]) ii=ii+1 cv2.putText(image,str(ii),(xa,ya),2,1,(249,99,201)) cv2.rectangle(image, (xa,ya),(xb,yb),(249,99,201),2) cv2.imshow('rectangle', image) l.close() f.close() cv2.destroyAllWindows() fan()
按w可以后退