2016-03-21 5 views
0

Ich bekomme Segmentierungsfehlerfehler während der Laufzeit für den folgenden Code, um Konturen zu finden. Ich habe auf dieses Formular this Beitrag verwiesen, aber hat mir nicht viel geholfen. Ich habe erfahren, dass es einige Probleme gibt mit findContours Dies ist eine andere Ausgabe von findContours. Bitte überprüfen Sie beide Links und helfen Sie mir, diesen Fehler zu beheben. Ich weiß nicht, warum ich einen Segmentierungsfehler erhalte.Segmentierungsfehler in 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; 
} 

Fehler:

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) 
+1

Sie sollten versuchen, es mit gdb zu debuggen und herauszufinden, welche Zeile den Segmentierungsfehler gibt, dann können Sie es leichter beheben. Obwohl ich denke, dass es wegen der Tatsache ist, dass Sie contents_poly [i] indizieren, während es nicht gefüllt worden ist (es ist leer) – Wajahat

+0

@Wajahat habe ich GDB, das debugging und unten Ergebnis erzielt. 'Programm empfangenes Signal SIGSEGV, Segmentierungsfehler. 0xb7b4534b in cv :: _ OutputArray :: erstellen (int, int const *, int, int, bool, int) const() aus/usr/local/lib/libopencv_core.so.3.1' – pradeep

Antwort

1

Ich weiß nicht, OpenCV, aber ... Ich nehme an, dass Ihr contour Vektor von findContours() feeded wird.

Also, wenn ich mich nicht falsch, die istruction

vector<vector<Point> > contours; 

schafft ein Vektor von Null-Elementen; die folgenden WARTUNGSANLEITUNGEN

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

erzeugen andere Vektoren von Null-Elementen (contours.size() Null ist).

Dann wird findContours() aufgerufen und contours Größe ändern.

Die folgende istruction ist unuseful

contours.resize(contours.size()); 

aber das eigentliche Problem hier starten

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

, wenn Sie versuchen, das erste Element einer Größe Null Vektor zuzugreifen (contours_poly)

Vorschlag : Deklarieren (oder Ändern der Größe) der anderen Vektoren nach dem Aufruf an findContours()

ps.s .: sorry für mein schlechtes Englisch

+0

Vielen Dank. Es hat mir geholfen. – pradeep

+0

@predeep: überhaupt nicht. – max66