How to remove a polyline from an RGB image using numpy

I am not sure what would be the best way to remove a vertical polyline from an image. This line starts from the very top of an image to the bottom. The input is an image with three channels and array of y indices of the line. The output would be the same input image but without the polyline. For example, if the first image shape is (5, 10,3), then the final image shape should be (5, 9, 3) after removing the polyline.

Input example:

Image:

np.array([[[0, 20, 0, 0, 255, 0, 0, 0, 0, 0],
           [0, 20, 0, 0, 255, 0, 0, 0, 0, 0],
           [0, 56, 0, 0, 0, 255, 0, 0, 0, 0],
           [0, 58, 0, 0, 0, 255, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 255, 0, 0, 0]],
          [[0, 0, 0, 0, 255, 0, 0, 0, 30, 0],
           [0, 0, 0, 0, 255, 0, 0, 0, 31, 0],
           [0, 0, 0, 0, 0, 255, 0, 0, 32, 0],
           [0, 0, 0, 0, 0, 255, 0, 0, 31, 0],
           [0, 0, 0, 0, 0, 0, 255, 0, 0, 0]],
          [[1, 0, 0, 0, 255, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 255, 0, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 255, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 255, 0, 0, 0, 0],
           [1, 0, 0, 0, 0, 0, 255, 0, 0, 0]]])

Note: the size here is (5, 10, 3) and we wanna remove the white polyline

Polyline:

np.array([4, 4, 5, 5, 6])

My code

def remove_polyline(image, polyline):
    h, w = image.shape[:2]
    res_image = np.zeros((h, w - 1))

    for i in range(h):
        left_section = image[i, :polyline[i]]
        right_section = image[i, polyline[i]+1:]
        res_image[i, :] = np.dstack((left_section, right_section))

    return res_image

I know this would work for grayscale images but not sure about 3 channel images



Read more here: https://stackoverflow.com/questions/64403607/how-to-remove-a-polyline-from-an-rgb-image-using-numpy

Content Attribution

This content was originally published by Andrew Fakhry at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: