How to deal with segmentation fault error in opencv c++?

asked 2018-04-22 15:13:09 -0600

I am trying to code a program on opencv to decide whether a human has approached ahead the camera. After I run the execution file, I get the captured video for few seconds and encounter the segmentation fault error.

The code is like this Here are headers:

enter code here
#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";

here is the main function

enter code here
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;
}

Here is the detectAndDisplay function

enter code here

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 );
}

Here is the computeArea function

enter code here
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;
}

Here is runningAverage function

enter code here

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);
}

I have searched many opencv segmentation fault questions, some said this segmentation fault was caused by wrong array used, but my case has little use of array. Others said misused of function characters could also cause this kind of errors, I agree with this, some of my characters could be wrong here.

edit retag flag offensive close merge delete

Comments

1

You should learn about GDB and build OpenCV and your program in debug mode.

Eduardo gravatar imageEduardo ( 2018-04-22 17:37:58 -0600 )edit

Check all array size : Point center( upperbodys[0].x Are you sure that upperbodys is not empty?

LBerger gravatar imageLBerger ( 2018-04-23 02:00:14 -0600 )edit