本文介绍了findContours中的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在运行时获取分段错误错误以下代码查找轮廓。我已经引用了此表单上的此帖子,但没有帮助我。我知道 findContours 有一些问题这是 findContours 。请检查这两个链接,并帮助我解决此错误。我不知道为什么我得到分段错误错误。 #includeopencv2 / objdetect / objdetect.hpp #includeopencv2 / highgui / highgui.hpp #includeopencv2 / imgproc / imgproc.hpp #include< iostream> #include< stdio.h> using namespace std; using namespace cv; string window_name =捕获的矩形块; RNG rng(12345); double fps; int thresh = 100; int main(int argc,const char ** argv) { VideoCapture cap(video.mp4); //打开用于阅读的视频文件 if(!cap.isOpened())//如果不成功,退出程序 { cout< 无法打开视频文件<< endl; return -1; } fps = cap.get(CV_CAP_PROP_FPS); // get the frames per seconds of the video cout<< 每秒帧数:< 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 threshold_output; int skip_frame = 4; while(skip_frame) { printf(inside while loop\\\); bool bSuccess = cap.read(frame); //从视频读取一个新帧 skip_frame--; if(!bSuccess)//如果不成功,断开循环 { cout< 无法从视频文件中读取帧<< endl; break; } } // - 3.将分类器应用于框架 if(frame.empty()) {printf - (!)No captured frame - Break!);打破; } std :: vector< Rect>面孔; Mat frame_gray; cvtColor(frame,frame_gray,CV_BGR2GRAY); equalizeHist(frame_gray,frame_gray); vector< vector< Point> >轮廓; vector< Vec4i>层次; vector< vector< Point> > contours_poly(contours.size()); vector< Rect> boundRect(contours.size()); vector< Point2f> center(contoururs.size()); 矢量< float> radius(contours.size()); printf(before finding countrs \\\); threshold(frame_gray,threshold_output,thresh,255,THRESH_BINARY); findContours(threshold_output,contour,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0)); contours.resize(contours.size()); printf(found found countrs\); for(unsigned int i = 0; i { printf(inside for loop\\\); approxPolyDP(Mat(contoururs [i]),contours_poly [i],3,true); printf(poly \\\之后); boundRect [i] = boundingRect(Mat(contoururs_poly [i])); printf(bondrec\\\之后); minEnclosingCircle((Mat)contoururs_poly [i],center [i],radius [i]); } Mat drawing = Mat :: zeros(threshold_output.size(),CV_8UC3); for(unsigned int i = 0; i { Scalar color = Scalar(rng.uniform(0,255),rng.uniform ),rng.uniform(0,255)); drawContours(drawing,contoururs_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); } ///在窗口中显示 namedWindow(Contours,CV_WINDOW_AUTOSIZE); imshow(Contours,drawing); int c = waitKey(10); if((char)c =='c'){break; } } return 0; } 错误: 每秒帧数:15 在while循环中在while循环中在while循环中在while循环中在查找countrs之前在找到countrs之后里面for循环分段错误(内核转储) 轮廓向量由 findContours()。 所以,如果我没有错,istruction vector< vector< Point> >轮廓; 创建零元素的向量; vector< vector< Point> > contours_poly(contours.size()); vector< Rect> boundRect(contours.size()); vector< Point2f> center(contoururs.size()); 矢量< float> radius(contours.size()); 创建零元素的其他向量( contours.size 调用findContours()并调用轮廓 以下istruction无用 c $ c> contoururs.resize(contours.size()); 但真正的问题从这里开始 approxPolyDP(Mat(contoururs [i]),contours_poly [i],3,true);当您尝试访问零大小向量的第一个元素( contours_poly)时, ) 建议:在调用 findContours() ps:对不起我的英文版 I am getting segmentation fault error during run time for the below code to find contours. I have referred this post on this form but didn't help me much. I got to know there are some issues with findContours This is another issue of findContours. Please check both the links and help me to resolve this error. I don't know why I am getting segmentation fault error. #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; }Error:Frame per seconds : 15inside while loopinside while loopinside while loopinside while loopbefore finding countrsafter finding countrsinside for loopSegmentation fault (core dumped) 解决方案 I don't know openCv, but... I suppose that your contour vector is feeded by findContours().So, if I'm not wrong, the istructionvector<vector<Point> > contours;creates a vector of zero elements; the following istructionsvector<vector<Point> > contours_poly( contours.size() );vector<Rect> boundRect( contours.size() );vector<Point2f>center( contours.size() );vector<float>radius( contours.size() );create other vectors of zero elements (contours.size() is zero).Then findContours() is called and contours change size.The following istruction is unusefulcontours.resize(contours.size());but the real problem start hereapproxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );when you try to access the first element of a zero size vector (contours_poly)Suggestion: declare (or resize) the other vectors after the call to findContours()p.s.: sorry for my bad english 这篇关于findContours中的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!