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

Unhandled Exception at some memory location

本文关键字:未处理 异常 位置 内存      更新时间:2023-10-16

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

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

我的程序是

#include <iostream>
#include <ctime>
#include<stdlib.h>
#include<vector>
#include"opencv2opencv.hpp"
#include"opencv2highguihighgui.hpp"
#include"opencv2corecore.hpp"
#include"opencv2imgprocimgproc.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 typen1.IP cameran2.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"函数而不进行任何更改并按原样处理时,没有问题。但它很慢。因此,我裁剪了图像并使分辨率恒定并进行了处理。因此,处理速度提高到相当可观的数量。但它经常抛出异常。我认为问题出在内存分配上。但是我想不通。

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

在 try-catch 块中调用检测多尺度。这个尝试捕获块解决了我的问题。

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

我也在尝试用HogDescriptor检测人。当我调试代码时,我意识到仅当裁剪的图像大小较小时才会发生此错误。它与训练数据大小有关。也许这对你有用:HOG检测器:检测到的ROI大小与训练样本大小之间的关系

开始调试的理想方法是捕获异常并打印堆栈跟踪。请参考这篇文章,了解如何生成堆栈跟踪 当我的 gcc C++ 应用程序崩溃时如何生成堆栈跟踪

这将查明生成异常的位置