cv::line draws wrong lines when coordinates are very far from image (Bug #2988)
Description
There seems to be a bug with cv::line related to clipping.
Sometimes, when it gets points outside and very far from the image it draws completely wrong lines.
Here's some code to demonstrate this:
{
cv::Mat img = cv::Mat::zeros(cv::Size(480,360), CV_8UC3);
// The points
Point p2(262, 23), q2(91618510, -54756195);
Point p3(263, 214), q3(80120779, 11846827);
// The bad lines
line(img, p2, q2, cv::Scalar(255,255,255));
line(img, p3, q3, cv::Scalar(255,255,255));
// Clip BEFORE drawing
cv::clipLine(img.size(), p2, q2);
cv::clipLine(img.size(), p3, q3);
// Good lines
line(img, p2, q2, cv::Scalar(0,255,255));
line(img, p3, q3, cv::Scalar(0,255,255));
imshow("img", img);
waitKey();
}
This also happens with CV_AA.
History
Updated by Gurpinder Sandhu almost 12 years ago
@Vadim I think this can solved simply by adding an assertion in the line function:
CV_Assert(pt1.x < img.cols && pt1.y < img.rows);
CV_Assert(pt2.x < img.cols && pt2.y < img.rows);
where pt1, pt2 are the input points and img is the input image.
Please give your comments
Updated by Adi Shavit almost 12 years ago
- First, it already works with internal clipping for many cases.
- Sometimes, you need to draw lines to points outside the image. Auto clipping is very helpful for making clean code.
- Since the Bresenham algorithm discretizes the line. A line to the clipped point will generally not be the same as the line to the point outside the image.
Updated by Anna Kogan almost 12 years ago
Hello Gurpinder,
Thank you for reporting the issue. I agree with Adi that asserts are not needed in the case. If you could find another solution on your side, a pull request in our GitHub repo would be highly appreciated!
Updated by Maksim Shabunin over 9 years ago
Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/4555