OpenCV+python實現網路攝影機的調用

來源:互聯網
上載者:User
本篇文章給大家分享的內容是關於OpenCV+python實現網路攝影機的調用 ,有著一定的參考價值,有需要的朋友可以參考一下

  • 使用opencv內建的VideoCapture()函數定義網路攝影機對象,其參數0表示第一個網路攝影機,一般就是筆記本的內建網路攝影機。

    cap = cv2.VideoCapture(0)
  • 在while迴圈中,利用網路攝影機對象的read()函數讀取視頻的某幀,並顯示,然後等待1個單位時間,如果期間檢測到了鍵盤輸入q,則退出,即關閉視窗。

    while(1):    # get a frame    ret, frame = cap.read()    # show a frame    cv2.imshow("capture", frame)    if cv2.waitKey(1) & 0xFF == ord('q'):        break
  • 調用release()釋放網路攝影機,調用destroyAllWindows()關閉所有映像視窗。

    cap.release()cv2.destroyAllWindows()
  • 完整代碼

    import cv2import numpy as npcap = cv2.VideoCapture(0)while(1):    # get a frame    ret, frame = cap.read()    # show a frame    cv2.imshow("capture", frame)    if cv2.waitKey(1) & 0xFF == ord('q'):        breakcap.release()cv2.destroyAllWindows()


對擷取的映像進行初步處理


#網路攝影機並顯示輪廓import cv2cap = cv2.VideoCapture(0)i=0while(1):    ret, frame = cap.read()    img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)    img_gb = cv2.GaussianBlur(img_gray, (5, 5), 0)    edges = cv2.Canny(img_gb, 100 , 200)    cv2.imshow("capture", edges)    if cv2.waitKey(1) & 0xFF == ord('q'):               breakcap.release()cv2.destroyAllWindows()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.