查找轮廓中的分割错误
segmentation fault in findContours
我在运行时遇到分段错误,以便以下代码查找轮廓。我已经在这个表格上提到了这篇文章,但对我没有多大帮助。我知道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 loopn");
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 countrsn");
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 countrsn");
for( unsigned int i = 0; i < contours.size(); i++ )
{
printf("inside for loopn");
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
printf("after polyn");
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
printf("after bondrecn");
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
载体是由findContours()
喂养的.
所以,如果我没记错的话,结构
vector<vector<Point> > contours;
创建零元素的向量;以下结构
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
创建零元素的其他向量(contours.size()
为零)。
然后调用findContours()
并contours
更改大小。
以下结构没有用
contours.resize(contours.size());
但真正的问题从这里开始
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
当您尝试访问零大小向量的第一个元素时 ( contours_poly
)
建议:在调用findContours()
后声明(或调整大小)其他向量
PS:对不起,我的英语不好
相关文章:
- C++映射分割错误(核心转储)
- 由cin中的字符串中未捕获空白引起的分割错误
- 删除映射和分割错误中的一个过去结束元素
- 在指向函数中读取变量时出现分割错误
- 在链表中的第 n 位插入显示分割错误
- 较高值 n 的分割错误(例如 n=999997)
- 尝试通过memcpy复制大尺寸浮点向量时的分割错误
- 分割错误:向量中的擦除功能
- 向量向量的分割错误
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 在尝试使用递归查找集合子集的总数时,我遇到了分割错误
- 分割错误 11:尝试使用 cin 输入 B[1] 时
- 集合布局上的 Qt 分割错误
- 高达20亿的筛子会产生分割错误
- 对向量使用推回函数时的分割错误
- 绘制精灵会导致分割错误
- 将矢量的整数内容打印为字符串会导致分割错误
- 分割错误:使用向量时为 11 c++
- 结构的分割错误错误