图像处理中的值错误



我正在研究数据库中图像的白化。由于代码很大,我只会给出代码中存在错误的函数 -

def sample_images_raw(fname):
image_data = cv2.imread(fname)
patch_size = 12
n_patches = 10000
image_size = image_data.shape[0]
n_images = image_data.shape[2]
patches = np.zeros(shape=(patch_size * patch_size, n_patches))
for i in range(n_patches):
image_id = np.random.randint(0, n_images)
image_x = np.random.randint(0, image_size - patch_size)
image_y = np.random.randint(0, image_size - patch_size)
img = image_data[:, :, image_id]
patch = img[image_x:image_x + patch_size, image_y:image_y + patch_size].reshape(-1)
patches[:, i] = patch
return patches

我收到的错误消息是这样的 -

Traceback (most recent call last):
File "/home/moron/Project/pca/pca_gen.py", line 37, in <module>
x = sample_images_raw(sys.argv[1])
File "/home/moron/Project/pca/sample_images.py", line 70, in sample_images_raw
patches[:, i] = patch
ValueError: could not broadcast input array from shape (0) into shape (144)

我尝试将变量 patch_size 的值更改为 6,但出现以下错误 -

Traceback (most recent call last):
File "/home/moron/Project/pca/pca_gen.py", line 37, in <module>
x = sample_images_raw(sys.argv[1])
File "/home/moron/Project/pca/sample_images.py", line 70, in sample_images_raw
patches[:, i] = patch
ValueError: could not broadcast input array from shape (30) into shape (36)

我又走了一步,将值更改为 1。编译器也又走了一步,给出了以下错误 -

Traceback (most recent call last):
File "/home/moron/Project/pca/pca_gen.py", line 37, in <module>
x = sample_images_raw(sys.argv[1])
File "/home/moron/Project/pca/sample_images.py", line 70, in sample_images_raw
patches[:, i] = patch
ValueError: could not broadcast input array from shape (0) into shape (1)

我正在研究的数据库是完善的数据库,如orl faces和faces 95。

谁能解释编译器这种奇怪行为的原因并更正此代码。

看起来你弄乱了你的图像尺寸。

取代

image_size = image_data.shape[0]

image_width = image_data.shape[0]  # These might be the other
image_height = image_data.shape[1] # way round with width == 1

然后替换这些行

image_x = np.random.randint(0, image_size - patch_size)
image_y = np.random.randint(0, image_size - patch_size)

image_x = np.random.randint(0, image_width - patch_size)
image_y = np.random.randint(0, image_height - patch_size)

您当前的代码正在尝试访问图像尺寸之外的切片(除非宽度 == 高度(,为您提供一个长度为 0 的数组。

最新更新