最近在运行论文的开源代码时报错:could not broadcast input array from shape (7,7,16) into shape (7,7)
参考了这篇博客得以解决:https://blog.csdn.net/z704630835/article/details/86525973
def make_mosaic(images, num_rows, num_cols, border=1, class_names=None):
num_images = len(images)
image_shape = images.shape[1:] #(7,7,16,1)
mosaic = ma.masked_all(
(num_rows * image_shape[0] + (num_rows - 1) * border,
num_cols * image_shape[1] + (num_cols - 1) * border),
dtype=np.float32)
paddedh = image_shape[0] + border
paddedw = image_shape[1] + border
for image_arg in range(num_images):
row = int(np.floor(image_arg / num_cols))
col = image_arg % num_cols
image = np.squeeze(images[image_arg]) #(7,7,16)
image_shape = image.shape #(7,7,16)
mosaic[row * paddedh:row * paddedh + image_shape[0],
col * paddedw:col * paddedw + image_shape[1]] = image
return mosaic
原因:图片质量参差不齐,甚至有的图片是4维的,运行该程序后直接报错
解决方法:在对image进行如下操作:
try:
if img.shape[2] == 3:
img = cv2.transform.resize(img, (image.shape[0], image.shape[1])
except:
continue