Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cv::line draws wrong lines when coordinates are very far from image #4555

Closed
opencv-pushbot opened this issue Jul 27, 2015 · 1 comment · Fixed by #7161
Closed

cv::line draws wrong lines when coordinates are very far from image #4555

opencv-pushbot opened this issue Jul 27, 2015 · 1 comment · Fixed by #7161

Comments

@opencv-pushbot
Copy link
Contributor

Transferred from http://code.opencv.org/issues/2988

|| Adi Shavit on 2013-04-25 20:48
|| Priority: Normal
|| Affected: 2.4.0 - 2.4.4
|| Category: core
|| Tracker: Bug
|| Difficulty: None
|| PR: 
|| Platform: None / None

cv::line draws wrong lines when coordinates are very far from image

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

Gurpinder Sandhu on 2013-04-26 14:53
@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
Adi Shavit on 2013-04-30 17:59
@ Gurpinder Sandhu, I don't think it should assert that points are inside the image. For several reasons:
# 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.
Anna Kogan on 2013-05-07 08:16
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":http://www.code.opencv.org/projects/opencv/wiki/How_to_contribute in our GitHub repo would be highly appreciated!
@pcampr
Copy link

pcampr commented Aug 19, 2016

this is caused by #5473

in ThickLine(), which is called by line(), the points p2(262, 23) and q2(91618510, -54756195) are modified to p2(262, 23) and q2(-818, 31901) due to bit shifting (see related issue), which affects coordinates >32768.

When clipping is used, the new points are p2(262, 23) and q2(301, 0). As the new numbers are below 32768, the line drawn for these clipped points is correct.

Similarly for second pair of points, p3(263, 214) and q3(80120779, 11846827) are incorrectly modified to p3(263, 214) and q3(-29749, -15189) by bit shifting.

This corresponds with the observations of the generated image img.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants