我是 python 和 Opencv 的新手,我尝试输入以下代码以将图像从我的网络摄像头保存到我的计算机:
import cv
if __name__=='__main__':
pCapturedImage = cv.CaptureFromCAM(1)
rospy.sleep(0.5)
pSaveImg=cv.QueryFrame(pCapturedImage)
cv.SaveImage("test.jpg", pSaveImg)
但是当我尝试打开它时,
我发现jpeg是空的。
有人可以帮忙吗?
另外,我尝试了一个程序来显示我的网络摄像头看到的内容:
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)
我让你的第二个样本也可以通过在捕获对象上使用 open 来工作:
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")
关于python - Opencv 和 Python 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6657743/