我正在对数据库中的图像进行白化。由于代码很大,我只给出代码中出现错误的函数——
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。
任何人都可以解释编译器这种奇怪行为的原因并更正这段代码。