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

C++ 某些内存位置的未处理异常,c++,opencv,visual-c++,video-streaming,C++,Opencv,Visual C++,Video Streaming,我在一个项目中工作,在这个项目中我必须检测人体的运动。首先,我编写了一个运动检测程序,并使其正常工作。然后,我使用HOGDescriptor进行人体检测,并将这两个程序结合起来以提高过程的速度。首先我监视一个运动,如果检测到任何运动,然后我通过表示运动的矩形框裁剪图像,并将裁剪部分单独发送给人体检测功能,以便快速处理 但是出现了一个问题。有时我会得到一个很好的结果,有时我会在.exe文件的某个内存位置出现一个弹出窗口,显示未处理的异常 我的节目是 #include <iostream>

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

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

我的节目是

#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;
}
#包括
#包括
#包括
#包括
#包括“opencv2\opencv.hpp”
#包括“opencv2\highgui\highgui.hpp”
#包括“opencv2\core\core.hpp”
#包括“opencv2\imgproc\imgproc.hpp”
#包括
#包括
使用名称空间std;
使用名称空间cv;
Mat detect1(int、VideoCapture、VideoWriter);
发现病媒;
int humandet(Mat,Rect);
背景:mog2-bg[5];
int _tmain(int argc,_TCHAR*argv[]
{
垫架[5];
字符串win[5]={“视频0”、“视频1”、“视频2”、“视频3”};
字符串ip,user,pass;
细流ss;
字符串vid[5]={“D:/Recorded.avi”、“D:/Recorded1.avi”、“D:/Recorded2.avi”、“D:/Recorded3.avi”};
视频写手维达尔[5];
视频捕获上限[5];
int n,类型,j;
coutn;
对于(inti=0,j=0;i>user>>pass;

开始调试的理想方法是捕获异常并打印堆栈跟踪。 请参考这篇关于如何生成堆栈跟踪的文章


这将精确定位它在try-catch块中生成异常的位置。这个try-catch块解决了我的问题

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

我还尝试使用HogDescriptor检测人员。在调试代码时,我意识到只有当裁剪的图像大小很小时才会发生此错误。这与训练数据大小有关。这可能对您有用:

在_-tmain中,由于cin>>n;,变量“n”可以保存任何值。当n的值超过5时,所有数组变量都将我会越界,导致异常。