2015-06-07 116 views
0

我想弄清楚運動檢測如何在opencv中工作。OpenCV cpp運動檢測

我可以看到那裏的視頻分析參考,但我沒有找到足夠的信息如何使用它。

我也看到了一些人使用absdiff我試了一下這樣的,但它給了我在memore錯誤異常

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'a rray op array' (where arrays have the same size and the same number of channels) , nor 'array op scalar', nor 'scalar op array') in cv::arithm_op, file C:\builds \2_4_PackSlave-win32-vc12-shared\opencv\modules\core\src\arithm.cpp, line 1287

代碼

#include <iostream> 
#include <sstream> 
#include <string> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/highgui/highgui.hpp> 

using namespace cv; 
using namespace std; 

int main() 
{ 
    //create matrix for storage 
    Mat image; 
    Mat image2; 
    Mat image3; 
    Mat image4; 

    //initialize capture 
    VideoCapture cap; 
    cap.open(0); 

    //create window to show image 
    namedWindow("window", 1); 

    while (1){ 

     //copy webcam stream to image 
     cap >> image; 
     cap >> image2; 

     absdiff(image, image2, image3); 
     threshold(image3, image4, 128, 255, THRESH_BINARY); 


     //print image to screen 
     if (!image.empty()) { 

      imshow("window", image3); 

     } 

     //delay33ms 

     waitKey(10); 

     // 
    } 

} 

IM顯然不使用它right

回答

1

在使用圖像之前,您需要確認VideoCapture已成功。此外,您還想在使用之前測試圖像是否被成功捕獲。試試這個:

VideoCapture cap(0); 

if(!cap.isOpened()) { 
    std::cerr << "Failed to open video capture" << std::endl; 
    return -1; 
} 

namedWindow("window"); 

while(true) { 
    cap >> image; 
    cap >> image2; 

    if(image.empty() || image2.empty()) { 
     std::cerr << "failed to capture images\n"; 
     return -1; 
    } 

    absdiff(image, image2, image3); 
    threshold(image3, image4, 128, 255, THRESH_BINARY); 

    imshow("window", image); 
    int k = waitKey(30) & 0xff; 
    if('q' == k || 27 == k) 
     break; 

}