某些内存位置的未处理异常

时间:2015-01-07 04:17:02

标签: c++ opencv visual-c++ video-streaming

我正在一个项目中工作,我必须检测人类的运动。首先,我编写了一个运动检测程序,并使其正常工作。然后我使用HOGDescriptor移动到人工检测,并将两个程序结合起来以提高过程的速度。首先我监视一个动作,如果检测到任何动作,那么我通过表示动作的矩形框裁剪图像,并将裁剪的部分单独发送到人体检测功能,以便快速处理。

但是出现了问题。我有时会得到很好的结果,有时我会在.exe文件中的某个内存位置显示 Unhandled Exception的弹出窗口

我的节目是

#include <iostream>
#include <ctime>
#include<stdlib.h>
#include<vector>
#include"opencv2\opencv.hpp"
#include"opencv2\highgui\highgui.hpp"
#include"opencv2\core\core.hpp"
#include"opencv2\imgproc\imgproc.hpp"
#include<string>
#include<sstream>

using namespace std;
using namespace cv;

Mat detect1(int,VideoCapture,VideoWriter);
vector<Rect> found;
int humandet(Mat,Rect);
BackgroundSubtractorMOG2 bg[5];

int _tmain(int argc, _TCHAR* argv[])
{
    Mat frame[5];
    string win[5]={"Video 0","Video 1","Video 2","Video 3"};
    string ip,user,pass;
    stringstream ss;

    string vid[5]={"D:/Recorded.avi","D:/Recorded1.avi","D:/Recorded2.avi","D:/Recorded3.avi"};
    VideoWriter vidarr[5];
    VideoCapture cap[5];

    int n,type,j;
    cout<<"Enter the no of cameras";
    cin>>n;

    for(int i=0,j=0;i<n;i++)
    {
        cout<<"Enter the camera type\n1.IP camera\n2.Non IP camera";
        cin>>type;
        if(type==2)
        {
            VideoCapture cap1(j++);
            cap[i]=cap1;
            cap[i].set(CV_CAP_PROP_FRAME_WIDTH,320);
            cap[i].set(CV_CAP_PROP_FRAME_HEIGHT,240);
            cap[i].set(CV_CAP_PROP_FPS,2);
        }
        else
        {
            cout<<"Enter the IP add:portno, username and password";
            cin>>ip>>user>>pass;
            ss<<"http://"<<user<<":"<<pass<<"@"<<ip<<"/axis-cgi/mjpg/video.cgi?.mjpg";
            string s(ss.str());
            VideoCapture cap2(s);
            cap[i]=cap2;
            cap[i].set(CV_CAP_PROP_FRAME_WIDTH,320);
            cap[i].set(CV_CAP_PROP_FRAME_HEIGHT,240);
            cap[i].set(CV_CAP_PROP_FPS,2);
        }

        VideoWriter video(vid[i],CV_FOURCC('D','I','V','X'),2,Size(320,240));
        vidarr[i]=video;
    }

    while(9)
    {
        for(int i=0;i<n;i++)
        {
            frame[i]=detect1(i,cap[i],vidarr[i]);
            imshow(win[i],frame[i]);
        }

        if(waitKey(30)==27)
            break;
    }   
    return 0;
}

Mat detect1(int j,VideoCapture cap,VideoWriter vid)
{


    Mat frame;
    Mat diff;

    cap>>frame;

    double large_area=0;
    int large=0;
    Rect bound_rect;
    bg[j].nmixtures=3;
    bg[j].bShadowDetection=true;
    bg[j].nShadowDetection=0;
    bg[j].fTau = 0.5;

    bg[j].operator() (frame,diff);

    vector<vector<Point>> contour;
    findContours(diff,contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);

    for(unsigned int i=0;i<contour.size();i++)
    {
        double area=contourArea(contour[i]);
        if(area>large_area)
        {
            large_area=area;
            large=i;
            bound_rect=boundingRect(contour[i]);
        }
    }
    contour.clear();

    if(large_area/100 > 2)
    {
        humandet(frame,bound_rect);
        rectangle(frame,bound_rect,Scalar(0,0,255),2);
        putText(frame,"Recording",Point(20,20),CV_FONT_HERSHEY_PLAIN,2,Scalar(0,255,0),2);
        vid.write(frame);
        return (frame);
    }
    else
        return (frame);
}

int humandet(Mat frame1,Rect bound)
{


    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    if((bound.height < 100) && (bound.width < 80))
    {
        Mat roi;
        roi.create(Size(80,100),frame1.type());
        roi.setTo(Scalar::all(0));
        Mat fram=frame1(bound);
        fram.copyTo(roi(Rect(0,0,(bound.height-1),(bound.width-1))));
        hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
        roi.release();
        fram.release();
    }

    else if((bound.height < 200) && (bound.width < 160))
    {
        Mat roi;
        roi.create(Size(160,200),frame1.type());
        roi.setTo(Scalar::all(0));
        Mat fram=frame1(bound);
        fram.copyTo(roi(Rect(1,1,(bound.height-1),(bound.width-1))));
        hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
        roi.release();
        fram.release();
    }

    else
    {
        Mat roi;
        roi=frame1;
        hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
        roi.release();
    }



    for(unsigned int i=0;i<found.size();i++)
    {
        rectangle(frame1,found[i], Scalar(255,0,0), 2);
    } 

    if(found.size())
    {
        frame1.release();
        found.clear();
        return 1;
    }
    else
        return 0;
}

在我使用裁剪方法之前,它运作良好。即,当我将框架传递给'humandet'函数而没有任何更改并按原样处理时,没有问题。但它很慢。因此我裁剪图像并使分辨率保持不变并进行处理。由此,处理速度增加到相当大的量。但它经常抛出异常。我认为问题在于内存分配。但我无法理解。

给我一​​个解决方案和方法来调试我犯的错误。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

在try-catch块中调用detectMultiScale。这个try-catch块解决了我的问题。

try{
    hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
}
catch(cv::Exception & e){ 
    return false;
}

我也在尝试用HogDescriptor检测人物。当我调试我的代码时,我意识到只有在裁剪的图像大小很小时才会出现此错误。它与训练数据大小有关。也许这对你有用:HOG detector: relation between detected roi size and training sample size

答案 1 :(得分:0)

开始调试的理想方法是捕获异常并打印堆栈跟踪。 请参阅这篇文章,了解如何生成堆栈跟踪How to generate a stacktrace when my gcc C++ app crashes

这将精确定位产生异常的位置