舉個例子ide
import cv2 clicked =False def onMouse(event,x,y,flags,param): global clicked if event == cv2.EVENT_LBUTTONUP: clicked=True cameraCapture =cv2.VideoCapture(0) cv2.namedWindow("MyWindow",0) cv2.resizeWindow("MyWindow",640,480) cv2.setMouseCallback("MyWindow",onMouse) print "Showing camera feed.Click window or press any key to stop." success,frame=cameraCapture.read() while success and cv2.waitKey(1)==-1 and not clicked: cv2.imshow("MyWindow",frame) success,frame=cameraCapture.read() cv2.destroyWindow("MyWindow") cameraCapture.release()
1、namedWindow(const String,ing flag)函數
Flag=0,表示按原圖尺寸顯示spa
Flag=1,表示窗口能夠自適應code
2、waitKey()視頻
waitKey()與waitKey(0)表示無線等待,針對視頻流來講,cv2.waitKey(0)表示只顯示當前幀圖像,至關於暫停blog
waitKey(n)表示等待n毫秒關閉窗口,針對視頻流來講,cv2.waitKey(1)表示延時1ms切換到下一幀it
當等待時間內無任何操做,等待結束返回-1event
若是等待時間內有輸入字符那麼返回該字符的ASCII值class
e.g. while(waitKey(1)!='q'): 或者如上程序while waitKey(1)== -1:test
針對第一個:waitKey(1)表示延遲一秒;while waitKey(1):表示一直延遲;while(waitKey(1)!='q'):表示一直延遲直到鍵入q時
針對第二個:前面同理;while waitKey(1)== -1:表示一直延遲,不鍵入字符。
3、VideoCapture()和read()
cameraCapture=cv2.VideoCapture(0)
success,frame = cameraCapture.read()
VideoCapture(0)表示打開筆記本內置攝像頭,還能夠VideoCapture("../test.avi")
read()函數表示按幀讀取視頻,success,frame是read()的兩個返回值,success是布爾值——若是讀取幀是正確的則返回True,若是文件讀取到結尾則返回False,Frame表示的是每一幀的圖像,是一個三維矩陣