使用C ++从多个网络摄像头捕获图片

问题描述 投票:2回答:2

我需要一个程序来捕获来自多个网络摄像头的图片并在Windows Vista中自动保存。我从this link获得了基本代码。代码在Window XP中运行,但是当我尝试在Vista上使用它时,它说“失败了”。每次执行时都会弹出不同的错误。如果我使用SDK平台会有帮助吗?有没有人有什么建议?

c++ image visual-c++ windows-vista capture
2个回答
2
投票

我不能在多个网络摄像头上测试这个,因为我只有一个,但我确信OpenCV2.0应该能够处理它。这里有一些示例代码(我使用Vista)和一个网络摄像头来帮助您入门。

#include <cv.h>
#include <highgui.h> 

using namespace cv;    

int main()
{
    // Start capturing on camera 0
    VideoCapture cap(0);
    if(!cap.isOpened()) return -1;

    // This matrix will store the edges of the captured frame
    Mat edges;
    namedWindow("edges",1);

    for(;;)
    {
    // Acquire the frame from cap into frame
    Mat frame;
    cap >> frame;

    // Now, find the edges by converting to grayscale, blurring and then Canny edge detection
    cvtColor(frame, edges, CV_BGR2GRAY);
    GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);

    // Display the edges and the frame
    imshow("edges", edges);
    imshow("frame", frame);
    // Terminate by pressing a key
    if(waitKey(30) >= 0) break; 
    }
return 0;
}

注意:

在第一帧处理期间分配矩阵边缘,除非分辨率突然改变,否则将为每个下一帧的边缘图重用相同的缓冲区。

如您所见,代码非常干净且可读!我从OpenCV 2.0文档(opencv.pdf)中解除了这个问题。

代码不仅显示来自网络摄像头的图像(在frame下),还可以进行实时边缘检测!这是我在我的显示器上指向网络摄像头的截图:)

screenshot http://img245.imageshack.us/img245/5014/scrq.png

如果您希望代码只显示来自一个摄像头的帧:

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main()
{
    VideoCapture cap(0);
    if(!cap.isOpened()) return -1;
    for(;;)
    {
    Mat frame;
    cap >> frame;
    imshow("frame", frame);
    if(waitKey(30) >= 0) break;
    }
return 0;
}

0
投票

如果程序与UAC关闭或运行管理员时,请确保您选择保存结果的位置在可写的位置,如用户的我的文档文件夹。一般来说,根文件夹和程序文件文件夹只适用于普通用户。

© www.soinside.com 2019 - 2024. All rights reserved.