2009-11-01 74 views
2

我需要一個程序來捕捉來自多個網絡攝像頭的圖片,並將它們自動保存在Windows Vista中。我從this link獲得了基本代碼。代碼在Window XP中運行,但是當我在Vista上使用它時,它說「失敗」。每次執行時都會彈出不同的錯誤。如果我使用SDK平臺會有幫助嗎?有沒有人有什麼建議?使用CPP捕捉圖片

回答

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關閉或在運行管理員時,請確保您選擇保存結果的位置位於用戶的我的文檔文件夾等可寫入位置。一般來說,根文件夾和程序文件文件夾只能爲普通用戶讀取。