Skip to content
Advertisement

1 px thick line cv2

I need to draw a line in an image where no pixel is thicker than 1 pixel in the horizontal dimension.

Despite I use thickness=1 in poly lines,

JavaScript

in the resulting plot there may be 2 pixels horizontally adjacent set to 255, like in this pic:

enter image description here

How can I prevent to have adjacent pixels set to 255? Or equivalently: what is an efficient way to set to 0 one of the 2?

I thought to Erosion but then, in those lines where there is only 1 255 pixel, such a pixel would be set to 0 as well.

Advertisement

Answer

It looks like we need to use for loops.

Removing one pixel out of two horizontally adjacent pixels is an iterative operation.
I can’t see a way to vectorize it, or use filtering or morphological operations.
There could be something I am missing, but I think we need to use a loop.

In case the image large, you may use Numba (or Cython) for accelerating the execution time.

JavaScript

src_img:
src_img

dst_img:
dst_img

I wouldn’t call the result a “1 px thick line”, but it meats the condition of “prevent to having adjacent pixels”.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement