gpt4 book ai didi

c++ - findContours 中的段错误

转载 作者:行者123 更新时间:2023-11-28 05:46:15 26 4
gpt4 key购买 nike

我在以下代码查找轮廓的运行时遇到段错误。我提到了this在此表格上发帖,但对我帮助不大。我知道 findContours 有一些问题这是 findContours 的另一期.请检查这两个链接并帮助我解决此错误。我不知道为什么会出现段错误。

 #include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

string window_name = "Captured rectangle block";
RNG rng(12345);
double fps;
int thresh = 100;

int main( int argc, const char** argv )
{
VideoCapture cap("video.mp4"); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size S(dWidth,dHeight);

while(1)
{

Mat frame;
Mat threshold_output;
int skip_frame = 4;
while(skip_frame)
{
printf("inside while loop\n");
bool bSuccess = cap.read(frame); // read a new frame from video
skip_frame--;
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
}

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

std::vector<Rect> faces;
Mat frame_gray;

cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray);

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
printf("before finding countrs\n");
threshold( frame_gray, threshold_output, thresh, 255, THRESH_BINARY );
findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

contours.resize(contours.size());
printf("after finding countrs\n");
for( unsigned int i = 0; i < contours.size(); i++ )
{
printf("inside for loop\n");
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
printf("after poly\n");
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
printf("after bondrec\n");
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
}
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( unsigned int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
}

/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
int c = waitKey(10);
if( (char)c == 'c' ) { break; }
}
return 0;
}

错误:

Frame per seconds : 15
inside while loop
inside while loop
inside while loop
inside while loop
before finding countrs
after finding countrs
inside for loop
Segmentation fault (core dumped)

最佳答案

我不知道 openCv,但是......我想你的 contour vector 是由 findContours() 提供的。

所以,如果我没记错的话,说明

vector<vector<Point> > contours;

创建一个零元素 vector ;以下说明

vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );

创建其他零元素 vector (contours.size() 为零)。

然后 findContours() 被调用并且 contours 改变大小。

下面的指令没有用

contours.resize(contours.size());

但真正的问题从这里开始

approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );

当您尝试访问零大小 vector (contours_poly) 的第一个元素时

建议:在调用 findContours()

后声明(或调整)其他 vector

p.s.: 抱歉我的英语不好

关于c++ - findContours 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36138094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com