1. 程式人生 > >ROS下利用Python和OpenCVC分別實現筆記本攝像頭/USB攝像頭/監控IP攝像頭資料的獲取

ROS下利用Python和OpenCVC分別實現筆記本攝像頭/USB攝像頭/監控IP攝像頭資料的獲取

說明:

最近的一個小任務,記錄一下,希望對以後有幫助吧: -————————————————————————————————————————————————

參考帖子

一、先利用python實現筆記本攝像頭/USB攝像頭的資料的獲取:

程式碼如下:

import numpy as np
import cv2
// 這裡的0代表的是膝上型電腦的攝像頭,改成1的話就是USB的攝像頭了
cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here,這裡獲取的是灰度的影象
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

如果想要獲取的是彩色的影象,程式碼如下:

#coding:utf-8
# created by Bingoren
# YunGu ShenZhen
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while (1):
	ret, frame = cap.read()
    cv2.imshow("capture", frame)
    if cv2.waitKey(1) & 0xff == ord( 'q' ):
    	break
cap.release()  
cv2.destroyAllWindows()