HoughLinesP not able to detect lines
Hi,
I have been struggling with detecting straight lines with HoughLines()
after I used the canny edge detector.
The canny edge detector works fine I believe (rectangle with triangle). But from the houghtransform I'm not been able to detect those lines. Underneath the pictures you will find the code I have been using and tried to play with the thershold values etc. I'm not really sure on how to improve the houghlines. Anyone who can help me?
Thanks
Mat img = this->frame.clone();
cv::cvtColor(img,img,CV_BGR2GRAY);
cv::Size size(3,3);
cv::GaussianBlur(img,img,size,0);
adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,249,10);
cv::imwrite("threshold.jpg", img);
cv::bitwise_not(img, img);
cv::imwrite("bitwise.jpg", img);
int lowThreshold = 100;
cv::Canny( img, img, lowThreshold, lowThreshold*3, 3 );
cv::imwrite("canny.jpg", img);
vector<Vec4i> lines;
HoughLinesP(img, lines, 1, CV_PI/180, 40, 5, 50);
cout << lines.size() << endl;
for( size_t i = 0; i < lines.size(); i++ )
{
line( this->frame, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 2, 8 );
}
cv::imwrite("hough.jpg", frame);
cv::imshow("frame", this->frame);
cv::waitKey(1);
EDIT I don't have the original image anymore, but I still have the image after the gaussian filter
Can you also include your original input image?
I edited the post
houghlines is working on gradients, so please don't binarize it before.
also, is is using it's own Canny, so drop that, too.
If your purpose is detecting (rectangle with triangle) then doing contour analysis and detecting shapes is better for this case.
@Ziri that's what I'm trying now. Do you know a way to detect if a boundingrect has boundingrects inside?
CV_PI/180, 40, 5, 50 The 50 is too high. Needed to be lower. And 5 could be increased too. You can play around with 3 parameters.
If you detect contours as a tree with RETR_TREE your triangle becomes a hole. (check contour Hierarchy) you'll also need to approximate contours to detect their shape (approxPolyDP).
you'll be able to do the same with Hough but tuning parameters for different cases is difficult.