Channel error of cv2.copyMakeBorder

 def pad(img, pad_size=96):
    """
    Load image from a given path and pad it on the sides, so that eash side is divisible by 80 (network requirement)
    if pad = True:
        returns image as numpy.array, tuple with padding in pixels as(x_min_pad, y_min_pad, x_max_pad, y_max_pad)
    else:
        returns image as numpy.array
    """
    if pad_size == 0:
        return img
    height, width = img.shape[:2]

    if height % pad_size == 0:
        y_min_pad = 0
        y_max_pad = 0
    else:
        y_pad = pad_size - height % pad_size
        y_min_pad = int(y_pad / 2)
        y_max_pad = y_pad - y_min_pad
    if width % pad_size == 0:
        x_min_pad = 0
        x_max_pad = 0
    else:
        x_pad = pad_size - width % pad_size
        x_min_pad = int(x_pad / 2)
        x_max_pad = x_pad - x_min_pad
    print(img.shape, y_min_pad, y_max_pad, x_min_pad, x_max_pad)
    img = cv2.copyMakeBorder(img, y_min_pad, y_max_pad, x_min_pad, x_max_pad, cv2.BORDER_REFLECT_101)
    return img, (x_min_pad, y_min_pad, x_max_pad, y_max_pad)

img = np.ones((256, 242,513)) # just change the channel number: 513
new_img = pad(img, 256)

When the channel is 512 or smaller, it runs well.

However, when channel is bigger than 512, an error occur.

1 Like

Np.ones limited to 512. Anything over 512, u will get an error.

Hi.
I have faced the same problem.

Np.ones limited to 512.

Could you explain more precisely?
I can call np.ones(513,513,513)and get value

@noymer , supra got it slightly wrong,

opencv’s Mat is limited to <= 512 channels, not numpy

however, the error msg from copyMakeBorder is weird / broken here, so thanks for highlighting it again ! :wink:

Mat is limited to <= 512 channels

I understand it well, Thank you!