opencv
opencv copied to clipboard
cv::line() with thickness might take a very long time for pathological huge segments
System Information
OpenCV 4.12.0
Detailed description
When using cv::line() with a pathological huge segment (the segment points being very far away from the image bounds, but crossing the image anyway), with thickness, FillConvexPoly() will be called.
This FillConvexPoly() will inherit from very distant points and consume a huge amount of (wasted) time.
A solution would be to clip line points within static void ThickLine() (the only trick is to take care of the line width to preserve line caps)
I will submit a PR
Steps to reproduce
cv::Mat src = cv::Mat::zeros(cv::Size(256, 256), CV_8UC3);
cv::Point p1(-1000000000, -1000000000);
cv::Point p2(1000000000, 1000000000);
cv::line(src, p1, p2, cv::Scalar(255, 0, 0, 255), 10);//takes a very long time
Issue submission checklist
- [x] I report the issue, it's not a question
- [x] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [x] I updated to the latest OpenCV version and the issue is still there
- [x] There is reproducer code and related data files (videos, images, onnx, etc)
You're absolutely right—when cv::line() is used with extremely distant endpoints and a non-zero thickness, the internal call to FillConvexPoly() ends up processing an unnecessarily large polygon, which can lead to severe performance degradation. This is especially problematic in scenarios where the endpoints are far outside the image bounds but the line still intersects the visible area.
Clipping the input points within ThickLine() before constructing the filled polygon is a smart and efficient approach. As long as the clipping takes into account the line's thickness (to preserve the intended caps and rendering behavior), this change should help you improve performance without affecting rendering accuracy for valid inputs.
hey @chacha21 can you assign this issue to me