2014-01-20 170 views
0

我想玩我的摄像头和OpenCV。 我关注此tuto:http://mateuszstankiewicz.eu/?p=189。 但我唯一的结果是一个红色的边框,我不明白为什么。任何人都可以帮助我做出正确的解决方案吗?运动检测与OpenCV c + +

这里是我的代码:

#include "mvt_detection.h" 


Mvt_detection::Mvt_detection() 
{ 

} 

Mvt_detection::~Mvt_detection() 
{ 
} 

cv::Mat Mvt_detection::start(cv::Mat frame) 
{ 
    cv::Mat back; 
    cv::Mat fore; 
    cv::BackgroundSubtractorMOG2 bg(5,3,true) ; 
    cv::namedWindow("Background"); 
    std::vector<std::vector<cv::Point> > contours; 

    bg.operator()(frame,fore); 
    bg.getBackgroundImage(back); 
    cv::erode(fore,fore,cv::Mat()); 
    cv::dilate(fore,fore,cv::Mat()); 
    cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 
    cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2); 
    return frame; 
} 

这里是我们的凸轮回报的截图: enter image description here

我试过其他两个视频从therethere和有同样的问题。

感谢您的帮助:)。

+0

你可以添加一个'cv :: namedWindow(“foreground”); CV :: imshow( “前景”,前); cv :: waitKey(30);'?在那里你可以看到图像中是否可以检测到运动。如果前景不可见,背景减法不适合您的问题。示例图像/视频也不错! – Micka

+1

嗨, 我在同一个项目(我们是学生,这是一个学校项目)。 我试图展示“前”形象。 图像全黑。那么,我想我们不能使用这种方法? 是否因为网络摄像头质量不足? 谢谢 – ogdabou

+0

@Micka:当cv :: BackgroundSubtractorMOG2 bg(5,3,true);'为true时,我得到一个白色窗口,当它是'false'时,我得到一个白色窗口 – Lenjyco

回答

0

正如@Lenjyco所说,我们解决了这个问题。

@Micka有一个好主意:

首先以仅进行一次instancied的BackgroundSubtractorMOG2。

我们初始化它在构造函数中,并与Hystory和Threashold玩:

Mvt_detection::Mvt_detection() 
{ 
    bg = new cv::BackgroundSubtractorMOG2(10, 16, false); 
} 

10 : the number of image the backgound look back to compare.

16 : the threshold level (blur)

通过这种方式,我们现在能够探测运动。

谢谢!

0

我已经使用了类似于你的以下代码,它运行良好。我也从我的摄像头获取输入。在你的代码中,我没有找到任何imshow()和waitkey。尝试使用它们。我的代码如下:

#include "opencv2/core/core.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/video/background_segm.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include <stdio.h> 

#include <iostream> 
#include <vector> 

using namespace std; 
using namespace cv; 

int main() 
{ 

    VideoCapture cap; 
    bool update_bg_model = true; 

    cap.open(0); 
    cv::BackgroundSubtractorMOG2 bg;//(100, 3, 0.3, 5); 
    bg.set ("nmixtures", 3); 
    std::vector < std::vector <cv::Point> >contours; 

    cv::namedWindow ("Frame"); 
    cv::namedWindow ("Background"); 

    Mat frame, fgmask, fgimg, backgroundImage; 

    for(;;) 
    { 
     cap >> frame; 
     bg.operator()(frame, fgimg); 
     bg.getBackgroundImage (backgroundImage); 
     cv::erode (fgimg, fgimg, cv::Mat()); 
     cv::dilate (fgimg, fgimg, cv::Mat()); 

     cv::findContours (fgimg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 
     cv::drawContours (frame, contours, -1, cv::Scalar (0, 0, 255), 2); 

     cv::imshow ("Frame", frame); 
     cv::imshow ("Background", backgroundImage); 


     char k = (char)waitKey(30); 
     if(k == 27) break; 

    } 

    return 0; 
} 
+0

我跟着你的代码。但是我得到了这个错误..“ld:1架构armv7的重复符号 clang:错误:linker命令失败,退出代码1(使用-v查看调用)” –

0

问题修正,把BackgroundSubtractorMOG2在我对象的字段和初始化它的构造使他工作得很好。