Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)

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 Answers

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....

like image 163
MohammadHosseinZiyaaddini Avatar answered Sep 07 '25 17:09

MohammadHosseinZiyaaddini