今天配置了下Eclipse下的Python,做了个Opencv在图片上画rectangle的小程序:
Python还不太熟悉,感觉变量用的还是不适应,之后看下有没有更好点的方式
最开始用的cv2实现的,但是没找到类似cvCopy的函数,非常囧,后来改成用cv实现
import cv2.cv as cv global startPointx global startPointy global flagDraw startPointx = 0 startPointy = 0 flagDraw = False if __name__ == '__main__': image = cv.LoadImage("E:/lena.jpg",cv.CV_LOAD_IMAGE_COLOR) imgc = cv.CreateImage((image.width,image.height), 8, 3) cv.Copy(image, imgc) def onMouseEvent(event,x,y,flags,param): global startPointx global startPointy global flagDraw #鼠标左键按下响应 if(event==1): print "Position is: %d,%d" ,x,y startPointx = x startPointy = y flagDraw = True #鼠标移动响应 if(event==0): if(flagDraw==True): cv.Rectangle(imgc, (startPointx,startPointy), (x,y), (255,0,0),3) cv.ShowImage('rectangle', imgc) cv.Copy(image, imgc) print "EndPosition is: %d,%d" ,x,y #鼠标左键抬起响应 if(event==4): if(flagDraw==True): cv.Rectangle(image, (startPointx,startPointy), (x,y), (255,0,0),3) cv.ShowImage('rectangle', image) flagDraw = False cv.ShowImage('rectangle', imgc) while True: cv.SetMouseCallback('rectangle', onMouseEvent) c = cv.WaitKey() if c == 27 or c == ord('q'): break cv.DestroyAllWindows()