title: Line Finder OpenCV
body: <p>I've tried to implement the example from the book named <strong>OpenCV 2 Computer Vision Application Programming Cookbook</strong>, which shows how to find a line with the help of the Hough Probabilistic Algorithm (page 170). (A very good book btw!)
  <a href="http://seabee3-ros-pkg.googlecode.com/files/1849513244__OpenCV.2.Computer.Vision.Application.Programming.Cookbook.pdf" rel="nofollow">the book in pdf</a></p>
  
  <p>But I've encountered some problem with C++, I'm not a guru in programming this why I need your help.
  Visual Studio shows me this following error :</p>
  
  <blockquote>
    <p>Unhandled exception at 0x00e274b7 in LineTracking_v02.exe: 0xC0000005:
    Access violation reading location 0x00000000.</p>
  </blockquote>
  
  <p>When I debug, it seems to come from the line 57 ('return lines;' in 'findLines' function).</p>
  
  <p>Have you got any idea why ?</p>
  
  <p>Here is my code :</p>
  
  <pre><code>//v2
  
  #include "opencv\cv.h"
  #include "opencv\highgui.h"
  #include &lt;iostream&gt;
  using namespace cv;
  using namespace std;
  #define PI 3.1415926535898
  
  
  class LineFinder{
  private :
      //original image
      cv::Mat img;
      //vector containing the end points of the detected lines
      std::vector&lt;cv::Vec4i&gt; lines;
      //accumulator resolution parameters
      double deltaRho;
      double deltaTheta;
      //minimum number of votes that a lines must receive before
      //being considered
      int minVote;
      //min length for a line
      double minLength;
      //max allowed gap along the line
      double maxGap;
  
  public:
      //default accumulator resolution is 1 pixel by 1 degree,
      //no gap, no minimum length
      LineFinder() :  deltaRho(1), 
          deltaTheta(PI/180), 
          minVote(10),
          minLength(0.) {}
  
      //Set the resolution of the accumulator
      void setAccResolution(double dRho, double dTheta){
          deltaRho = dRho;
          deltaTheta = dTheta;
      }
  
      //Set the minimum number of votes
      void setMinVote(int minv){
          minVote = minv;
      }
  
      //Set line length and gap
      void setLineLengthAndGap(double length, double gap){
          minLength = length;
          maxGap = gap;
      }
  
      //Apply probabilistic Hough Transform
      std::vector&lt;cv::Vec4i&gt; findLines(cv::Mat&amp; binary){
          lines.clear();
          cv::HoughLinesP(binary, lines, deltaRho, deltaTheta, minVote, minLength, maxGap);
          return lines;
      }
  
      //Draw the detected lines on an image
      void drawDetectedLines(cv::Mat &amp;image, cv::Scalar color=cv::Scalar(255,255,255)){
          std::vector&lt;cv::Vec4i&gt;::const_iterator it2 = lines.begin();
          while(it2!=lines.end()){
              cv::Point pt1((*it2)[0], (*it2)[1]);
              cv::Point pt2((*it2)[2], (*it2)[3]);
              cv::line(image, pt1, pt2, color);
              ++it2;
          }
      }
  };
  
  int main(int, char**)
  {
      // Open the default camera
      cv::VideoCapture capture(0); 
      // Check if we succeeded
      if(!capture.isOpened()) 
      {
          std::cout&lt;&lt;"Video capture failed, please check the camera."&lt;&lt;std::endl;
          return -1;
      }else{
          std::cout&lt;&lt;"Video camera capture successful!"&lt;&lt;std::endl;
      }
  
      for(;;) {
          cv::Mat frame;
          cv::Mat grayFrame;
          cv::Mat gaussGrayFrame;
          cv::Mat edges;
          LineFinder finder;
  
          capture &gt;&gt; frame; // get a new frame from camera
  
          //Convert the frame into a gray Frame
          cv::cvtColor(frame, grayFrame, CV_BGR2GRAY);
  
          //Apply a Gaussian Blur on the gray-level Frame
          cv::GaussianBlur(grayFrame, gaussGrayFrame, cv::Size(7,7), 1.5, 1.5);
  
          //Apply Canny Algorithm
          cv::Canny(
              gaussGrayFrame, // gray-level source image
              edges,          // output contours
              0,              // low threshold
              30,             // high threshold
              3);             // aperture size
          //End Canny Algorithm
  
          //Detect lines
          std::vector&lt;cv::Vec4i&gt; lines = finder.findLines(edges);
          //Draw the detected lines
          finder.drawDetectedLines(frame);
  
          cv::imshow("Camera Preview", frame);
          if(cv::waitKey(30) &gt;= 0) break;
      }
  
      // the camera will be deinitialized automatically in VideoCapture destructor
      return 0;
  }
  </code></pre>
  
  <p>Thank u in advance for your help !</p>