我是 python 和 Opencv 的新手,我尝试输入以下代码以将图像从我的网络摄像头保存到我的计算机:
import cv
if __name__=='__main__':
pCapturedImage = cv.CaptureFromCAM(1)
rospy.sleep(0.5)
pSaveImg=cv.QueryFrame(pCapturedImage)
cv.SaveImage("test.jpg", pSaveImg)
import cv
if __name__=='__main__':
cv.NamedWindow("camera",1)
capture=cv.CaptureFromCAM(0)
while True:
img=cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10)==27:
break
cv.DestroyedWindow("camera")
最佳答案
你试过演示程序吗?他们展示了如何使用网络摄像头等。
对于第一个问题,我不熟悉在 opencv 中使用摄像头,但我通过打开捕获让它工作(下面的代码中的 capture.open(device_id)
)
这是一个工作 python 示例(我使用较新的 c++ 接口(interface): imread 、 imwrite 、 VideoCapture 等...当它可用于 python 时,您可以在 the OpenCV docs 中找到列为“cv2”。 ):
import cv2
capture = cv2.VideoCapture() # this is the newer c++ interface
capture.open(0) # Use your device id; I think this is what you are missing.
image = capture.read()[1]
cv2.imwrite("test.jpg", image)
import cv2
cv2.namedWindow("camera", 1) # this is where you will put the video images
capture = cv2.VideoCapture()
capture.open(0) # again, use your own device id
while True:
img = capture.read()[1]
cv2.imshow("camera", img)
if cv2.waitKey(10) == 27: # waiting for the esc key
break
cv2.destroyWindow("camera")
https://stackoverflow.com/questions/6657743/