如何从网络摄像头捕获单个图像并在OpenCV中进一步处理?

时间:2019-05-27 06:03:30

标签: python opencv image-processing

我希望我的代码从网络摄像头捕获单个图像并进行进一步处理,例如稍后检测颜色和sobel边缘等等。 简而言之,我想进行图像采集。

3 个答案:

答案 0 :(得分:0)

import cv2
cap = cv2.VideoCapture(0) # Usually if u have only 1 camera, then it's 0, if u have multiple camera then it's may be 0,1,2 ...
ret, frame = cap.read() # ret is True or False status which shows if you are success reading frame from web cam, frame is an array
# If u want to loop to read continously
ret = True
while ret:
    ret, frame = cap.read()
    if frame is None:
        continue # this will stop the loop if we failed to read frame, because ret will be False

如果这是您想要的答案,则已被多次询问。询问之前,请确保已尝试搜索答案

答案 1 :(得分:0)

要使用网络摄像头,可以使用VideoCapture

import cv2
cap = cv2.VideoCapture(0) # use 0 if you only have front facing camera
ret, frame = cap.read() #read one frame
print(frame.shape)
cap.release() # release the VideoCapture object. 

您启动网络摄像头,读取一张图像并立即释放它。框架是图像,您可以根据需要对其进行预处理。您可以使用imshow查看图像:

cv2.imshow('image', frame)
if cv2.waitKey(0) & 0xff == ord('q'): # press q to exit
    cv2.destroyAllWindows()

答案 2 :(得分:0)

cam = cv2.VideoCapture(0)

image = cam.read()[1]

cv2.imshow("image", image)

cv2.waitKey(0)
cv2.destroyAllWindows()