Opencv c ++中的分段错误错误

时间:2018-04-22 20:00:03

标签: c++ opencv

我正在尝试在opencv上编写一个程序来决定一个人是否已经接近了相机。运行执行文件后,我获取捕获的视频几秒钟,并遇到分段错误错误。

代码是这样的 这是标题:

#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;

//define static variable
static int cApp = 0;//number of approached frame
static double last = 0;

//define functions
void detectAndDisplay( Mat frame );
bool computeArea( double width, double height, double lastArea);
double runningAverage(int M);

//define opencv function and classifier
String upperbody_cascade_name = "home/pi/opencv- 3.0.0/data/haarcascades/haarcascade_upperbody.xml";
CascadeClassifier upper_cascade;
String window_name = "Capture - upper body detection";

这是主要功能:

int main( void )
{
//define variable
VideoCapture capture;
Mat frame;

//-- 1. Load the cascades
upper_cascade.load("/home/pi/opencv-3.0.0/data/haarcascades/haarcascade_upperbody.xml");

//-- 2. Read the video stream
capture.open( -1 );
if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

while ( capture.read(frame) )
{
    if( frame.empty() )
    {
        printf(" --(!) No captured frame -- Break!");
        break;
    }
    //-- 3. Apply the classifier to the frame
    detectAndDisplay( frame );

    char c = (char)waitKey(10);
    if( c == 27 ) { break; } // escape
}

capture.release();
return 0;
}

以下是detectAndDisplay功能:

void detectAndDisplay( Mat frame )
{
std::vector<Rect> upperbodys;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect upperbodys
upper_cascade.detectMultiScale( frame_gray, upperbodys, 1.05, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

Point center( upperbodys[0].x + upperbodys[0].width/2, upperbodys[0].y + upperbodys[0].height/2 );
ellipse( frame, center, Size( upperbodys[0].width/2, upperbodys[0].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
bool ifApproached = computeArea(upperbodys[0].width/2, upperbodys[0].height/2, last);

if (ifApproached == true) {
    cApp++;
}

if (cApp == 3) {
    cout << "have approached" << endl;
    cApp = cApp - 3;
}

//-- Show what you got
imshow( window_name, frame );
}

以下是computeArea功能:

bool computeArea( double width, double height, double lastArea) {
double newArea = width * height;
bool ifApproached = false;
//double presentArea = newArea;
double presentArea = runningAverage(newArea);
double DifferenceBewteenAreas = presentArea - lastArea;

if (DifferenceBewteenAreas > 1) {//threshold
    ifApproached = true;
}

last = presentArea;
return ifApproached;
}

这是runningAverage函数:

double runningAverage(int M) {
//M is measurement
//#define LM_SIZE 5
static int LM[5];
static int index =0;
static long sum = 0;
static int count =0;

//keep sum updated to improve speed

sum = sum - LM[index];
LM[index] = M; 
sum = sum + LM[index];
index++;
index = index % 5;

if (count < 5) {
    count++;
}

return (double)(sum / (double)count);
}

我搜索过很多opencv分段错误问题,有人说这个分段错误是由于使用了错误的数组引起的,但是我的情况很少使用数组。其他人说滥用功能字符也可能导致这种错误,我同意这一点,我的一些角色可能在这里错了。

1 个答案:

答案 0 :(得分:1)

实际上我发现我不应该在代码中使用upperbodys [0],因为有时根本没有检测到任何对象,所以可能会发生一些内存读取错误,我使用了upperbodys [i]代替它工作那么。

void detectAndDisplay( Mat frame )
{
std::vector<Rect> upperbodys;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );

//-- Detect upperbodys

upper_cascade.detectMultiScale( frame_gray, upperbodys, 1.05, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
int size = upperbodys.size();
double newArea = -1;

for (int i = 0 ; i < size; i++) {
    Point center( upperbodys[i].x + upperbodys[i].width/2, upperbodys[i].y + upperbodys[i].height/2 );

    ellipse( frame, center, Size( upperbodys[i].width/2, upperbodys[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

    //bool ifApproached = computeArea(upperbodys[i].width/2, upperbodys[i].height/2, last);
    //////////////////////////////////////////


    newArea = upperbodys[i].width/2 * upperbodys[i].height/2;

    if (newArea != -1) {
        cout << "UpperBodys has value, index = " << i << endl; 
        break;
    }

}

bool ifApproached = false;
//double presentArea = runningAverage(newArea);
double presentArea = newArea;
double DifferenceBewteenAreas = presentArea - last;    

if (DifferenceBewteenAreas > 1) {//threshold
    ifApproached = true;
}

last = presentArea;
    //////////////////////////////////////////  
if (ifApproached == true) {
   cApp++;
}

if (cApp == 3) {
   cout << "have approached" << endl;
   cApp = cApp - 3;
}



//-- Show what you got
imshow( window_name, frame );
}