简体   繁体   English

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

[英]How do I capture a single image from webcam and process it further in OpenCV?

I want my code to capture a single image from webcam and process it further, like later detect colours and sobel edges and a lot more.我希望我的代码从网络摄像头捕获单个图像并进一步处理它,例如稍后检测颜色和 sobel 边缘等等。 In short, I want to do image acquisition.总之,我想做图像采集。

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

If this is the answer that u wanted, then it have been asked multiple times.如果这是您想要的答案,那么它已被多次询问。 Make sure you have tried to search the answer before you asked确保您在提问之前已尝试搜索答案

To use your webcam, you can use VideoCapture :要使用网络摄像头,您可以使用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. 

You start the webcam, read one image and immediately release it.您启动网络摄像头,读取一张图像并立即释放它。 The frame is the image and you can preprocess it however you want.框架就是图像,您可以根据需要对其进行预处理。 You can view the image using imshow :您可以使用imshow查看图像:

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

image = cam.read()[1]

cv2.imshow("image", image)

cv2.waitKey(0)
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM