0

I was working on whitening of images in a database. Since the code is huge, I will give only the function in the code where there is an error -

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

The error message I get in this is something like this -

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)

I tried changing the value of the variable patch_size to 6 and I got the following error -

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)

I went another step and changed the value to 1. The compiler too went another step to give the following error -

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)

The databases I was working on was well established ones like orl faces and faces 95.

Could anyone explain the reason for this weird behavior of compiler and give a correction to this code.

1 Answers1

0

Looks you're messing up your image dimensions.

Replace

image_size = image_data.shape[0]

with

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

And then replace these lines

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

with

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

Your current code is trying to access slices outside the image dimensions (unless width == height), giving you a 0-length array.

alkanen
  • 636
  • 6
  • 16