0

我正在对数据库中的图像进行白化。由于代码很大,我只给出代码中出现错误的函数——

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。

任何人都可以解释编译器这种奇怪行为的原因并更正这段代码。

4

1 回答 1

0

看起来你搞砸了你的图像尺寸。

代替

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 长度的数组。

于 2017-09-18T13:30:54.050 回答