Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Opencv c+中的分段错误+;_C++_Opencv - Fatal编程技术网

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

C++ Opencv c+中的分段错误+;,c++,opencv,C++,Opencv,我正试图在opencv上编写一个程序,以确定是否有人在摄像机前面靠近。在我运行执行文件之后,我获得了几秒钟的捕获视频,并遇到了分割错误 代码是这样的 以下是标题: #include "opencv2/objdetect.hpp" #include "opencv2/videoio.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream> #include <std

我正试图在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;
}
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 );
}
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;
}
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);
}
下面是
runningAverage
函数:

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;
}
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 );
}
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;
}
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);
}
双运行平均值(int M){
//M是度量
//#定义LM_尺寸5
静态int-LM[5];
静态int指数=0;
静态长和=0;
静态整数计数=0;
//保持sum更新以提高速度
sum=sum-LM[索引];
LM[指数]=M;
sum=sum+LM[索引];
索引++;
索引=索引%5;
如果(计数小于5){
计数++;
}
返回(双倍)(总和/(双倍)计数);
}

我搜索了很多opencv分割错误的问题,有人说这个分割错误是由于使用了错误的数组造成的,但我的案例很少使用数组。其他人说函数字符的误用也会导致这种错误,我同意这一点,我的一些字符可能在这里是错误的。

实际上我发现我不应该在代码中使用大写字母[0],因为有时根本没有检测到任何对象,所以可能会发生一些内存读取错误,我使用大写字母[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 );
}
void检测和显示(垫架)
{
std::向量上半身;
垫子框架为灰色;
CVT颜色(框架、框架灰、颜色灰);
均衡器历史(帧灰,帧灰);
//--上身
上层级联。检测多尺度(帧灰度,上层体,1.05,3,0 |级联级联级联尺度图像,大小(30,30));
int size=upperbodys.size();
双新面积=-1;
对于(int i=0;i难道你没有试着使用调试器来查看是哪一行导致了错误吗?顺便说一句,“分段错误”是一个非常普遍的错误,只是意味着“没有权限对内存执行特定的读或写操作”我没有,我在实验室使用的机器是一台raspberry pi 3,它没有任何调试器,我没有安装调试器的权限。在这种环境中我可以使用哪种调试器?我可以在笔记本电脑上试试如果你在pi上有gdb,使用它或使用gdbserver进行远程调试。你可以在代码中添加一些代码限制segfault出现的代码范围。例如,在每个函数的开头和结尾处。@Mika谢谢,这是一个查找错误的简单方法。实际上我发现我不应该使用Upperbody[0]在代码中,因为有时根本没有检测到任何对象,所以可能会发生一些内存读取错误,所以我改用upperbodys[I]。