I’ve tried to implement the example from the book named OpenCV 2 Computer Vision Application Programming Cookbook, which shows how to find a line with the help of the Hough Probabilistic Algorithm (page 170). (A very good book btw!)
the book in pdf
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 :
Unhandled exception at 0x00e274b7 in LineTracking_v02.exe: 0xC0000005:
Access violation reading location 0x00000000.
When I debug, it seems to come from the line 57 (‘return lines;’ in ‘findLines’ function).
Have you got any idea why ?
Here is my code :
//v2
#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>
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<cv::Vec4i> 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<cv::Vec4i> findLines(cv::Mat& 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 &image, cv::Scalar color=cv::Scalar(255,255,255)){
std::vector<cv::Vec4i>::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<<"Video capture failed, please check the camera."<<std::endl;
return -1;
}else{
std::cout<<"Video camera capture successful!"<<std::endl;
}
for(;;) {
cv::Mat frame;
cv::Mat grayFrame;
cv::Mat gaussGrayFrame;
cv::Mat edges;
LineFinder finder;
capture >> 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<cv::Vec4i> lines = finder.findLines(edges);
//Draw the detected lines
finder.drawDetectedLines(frame);
cv::imshow("Camera Preview", frame);
if(cv::waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Thank u in advance for your help !
I have just successfully compiled and executed your program under Ubuntu Linux. My current random theory right now is that the
edgesis not initialised properly in your environment.I would insert a few
cv::imwriteorcv::imshow(for frame, grayFrame, edges) to see if all images are containing reasonable values.