2014-01-20 71 views
0

我一直在試圖顯示來自灰度我的筆記本電腦的網絡攝像頭攝像頭輸入,我已經用下面的代碼完成它:顯示灰度攝像頭輸入的蟒蛇與OpenCV的

import cv2 
    import numpy as np 
    clicked = False 
    def onMouse(event, x, y, flags, param): 
     global clicked 
     if event == cv2.cv.CV_EVENT_LBUTTONUP: 
      clicked = True 

    cv2.namedWindow('image capture', cv2.WINDOW_NORMAL) 
    cv2.setMouseCallback('image capture', onMouse) 
    #initialize the camera object with VideoCapture 
    camera = cv2.VideoCapture(0) 
    sucess, frame = camera.read() 
    cv2.imwrite('snapshot.png', frame) 
    gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE) 
    while sucess and cv2.waitKey(1) == -1 and not clicked: 
     cv2.imwrite('snapshot.png', frame) 
     gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE) 
     cv2.imshow('image capture', gray) 
     sucess, frame = camera.read() 

    cv2.imwrite('snapshot.png', frame) 
    print 'photo taken press any key to exit' 
    cv2.waitKey() 
    cv2.destroyAllWindows() 

這裏什麼我已經完成了將該幀保存在'snapshot.png'中,並再次以灰度重新加載並顯示該灰度圖像。有沒有什麼方法可以直接讀取灰度的相機畫面,而不是經歷這一切。提前致謝。

回答

3

哇,什麼亂七八糟;)

你只是想:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)