3

How to add breakline(dotted line/dashed line) in OpenCV drawing functions like cv2.line(),cv2.rectangle() ?

Is there a line type for break lines?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
user119o
  • 387
  • 2
  • 7
  • 14

1 Answers1

1

If the line is horizontal or vertical, you can do something like this. Kind of hacky, but gets the job done in just a few lines if you don't need anything fancy.

y = 100  # vertical position of the line
thickness = 2  # thickness of the line
x0 = 0  # leftmost part of the line
x1 = img.shape[1]  # rightmost part of the line
on_time = 10  # pixels out of period that are solid
period = 20  # period between dashes
colour = (0, 0, 255)
img[
    y - thickness // 2:y + thickness // 2,
    [x for x in range(x0, x1) if x % period < on_time]
] = colour
Marcel
  • 1,034
  • 1
  • 17
  • 35