4

I have a bunch of (greyscale) images of different sizes that I resize to ensure one dimension is the same and pad the other dimension (a la this answer). Yet, I get the error ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2) on the 4th line (second to last line) of the code below. How would I solve this?

I tried running on non-greyscale images (as suggested here), yet this still doesn't work.

My code:

image = cv2.imread(filepath)
width = int((height / image.shape[1]) * image.shape[0])
image = cv2.resize(image, (width, height), interpolation = cv2.INTER_AREA)
image = np.pad(image,((0,0), (0,1028 - image.shape[1])), mode = 'constant')
data.append(image)
  • 1
    The images may be grayscale on the disk, but since you're using `imread` with the second argument unspecified, they're loaded as BGR. That means the numpy array that holds it has 3 axes, yet in `np.pad` you only specify 2 tuples. – Dan Mašek Jul 14 '20 at 19:00
  • @DanMašek How would I change it to add 0s everywhere there is no data currently? –  Jul 14 '20 at 20:13

1 Answers1

3

Your code should work unless you are loading RGB images. So make sure that the images are in Grayscale mode

You can load an image in grayscale mode:

image = cv2.imread('./image.tif',0)

Or simply convert it:

image = cv2.imread('./image.tif')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Otherwise, as it is written in numpy docs you need to specify the pad_width for each axis.

pad_width : .... unique pad widths for each axis....