Slow line rendering under certain corner cases

I’ve noticed that drawing certain lines with particular thickness values takes several seconds, whereas almost any other combination of line endpoints or thickness take sub 3ms. Minimum reproducible example:

import cv2
import numpy as np
import time

img = np.zeros((240,360,3), dtype=np.uint8);
t_start = time.perf_counter();
img = cv2.line(img, (2147483647, -1624848130), (-2147483648, 714182144), color=(255,255,255), thickness=2)
delta = time.perf_counter() - t_start
print(f"Delta: {delta:0.3f}s")

This takes about 3.3s in two different machines (Mac and Linux). However, the same two line endpoints, with thickness values of 1, 3, 4, etc take 1ms. I have two follow-up questions:

  1. Why is this happening? Why only for that thickness value? I can provide other random points that are also slow, at one point I found a combination that took over 7s to render.
  2. How should I optimize this? I can try calling cv2.clipLine (which returns visible=False in this case) but not sure if there might be other slow cases out there where even clipping before drawing takes this long. Also, this might make one corner case draw faster but all others draw slower.

Note 1: same slowdown happens if using cv2.polylines but I assume the implementation under the hood is similar to cv2.line.

Note 2: I’ve tested with different Python versions (3.9-3.12) and OpenCV versions too (4.8-4.11), and this seems to be happening on all of them.

probably worth a bug report on github, unless it’s been reported already.