python - Opencv 和 Python 的问题

标签 python opencv

我是 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): imreadimwriteVideoCapture 等...当它可用于 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/

相关文章:

python - 我可以抑制 Sphinx 文档中的变量扩展吗?

python - 用于嘈杂的不可微损失函数的自定义 Tensorflow 优化器

python - 如何在 15 帧的时间窗口的密集光流中找到特征点(特定像素)的速度?

OpenCV resize()结果是否错误?

python - 如何在Flask中异步调用函数?

python - 如何在 django 模板中使用 for 循环将列表转换为 html <ul>

python - 为什么从类实例返回的字符串保持 NoneType 类型,即使 IDLE 说它是一个字符串?

python - 用从索引到另一个的值填充 numpy 矩阵

python - 识别图像中没有可见轮廓的表格的边框和列轮廓

python - 提取二值图像中的最中心区域