1 | initial version |
I think the example was intended for something smaller than your image. Try this.
Mat dst = imread("lines.jpg");
Mat cdst = dst.clone();
cvtColor(dst, dst, COLOR_BGR2GRAY);
threshold(dst, dst, 1, 255, THRESH_BINARY);
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI / 180, dst.cols*0.5, 0, 0, 89 * CV_PI / 180, 91 * CV_PI / 180);
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 5000 * (-b));
pt1.y = cvRound(y0 + 5000 * (a));
pt2.x = cvRound(x0 - 5000 * (-b));
pt2.y = cvRound(y0 - 5000 * (a));
line(cdst, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
}
imshow("source", dst);
imshow("detected lines", cdst);
waitKey();
2 | No.2 Revision |
I think the example was intended for something smaller than your image. Try this.
EXPLANATION EDIT: I extended the size of the lines it draws to 5000 pixels, cause it's a big image. The most important changes are to the HoughLines call.
.
Mat dst = imread("lines.jpg");
Mat cdst = dst.clone();
cvtColor(dst, dst, COLOR_BGR2GRAY);
threshold(dst, dst, 1, 255, THRESH_BINARY);
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI / 180, dst.cols*0.5, 0, 0, 89 * CV_PI / 180, 91 * CV_PI / 180);
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 5000 * (-b));
pt1.y = cvRound(y0 + 5000 * (a));
pt2.x = cvRound(x0 - 5000 * (-b));
pt2.y = cvRound(y0 - 5000 * (a));
line(cdst, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
}
imshow("source", dst);
imshow("detected lines", cdst);
waitKey();