python - 使用 patchify 库创建补丁时出现问题
问题描述
我正在使用 patchify 库来创建更大的 .jpg 图像的补丁。我正在使用以下代码,取自此 YT 视频: https ://www.youtube.com/watch?v=7IL7LKSLb9I&ab_channel=DigitalSreeni
当 YT 人读取他的图像(12 个 tiff 图像)时,他会为 large_image_stack 变量获得以下大小:(12, 768, 1024),即 12 个图像,每个图像是 768x1024。
我有一个 3000x4000 的 jpg 图像,我为 large_image_stack 变量获得的大小是 (3000, 4000, 3)。然后我运行代码......
import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
import cv2
large_image_stack = cv2.imread("test.jpg")
for img in range(large_image_stack.shape[0]):
large_image = large_image_stack[img]
patches_img = patchify(large_image, (224,224), step=224)
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i,j,:,:]
cv2.imwrite('patches/images/' + 'image_' + str(img)+ '_' + str(i)+str(j)+ '.jpg', single_patch_img)
但我收到以下错误:
值错误:window_shape
太大
查看 patchify 库中的 view_as_windows.py 我看到以下内容:
arr_shape = np.array(arr_in.shape)
window_shape = np.array(window_shape, dtype=arr_shape.dtype)
if ((arr_shape - window_shape) < 0).any():
raise ValueError("`window_shape` is too large")
而且由于我对这些东西很陌生,我无法解决这个错误。
任何帮助将不胜感激!
解决方案
我想出了如何解决这个问题,因为这是一个简单的错误。for
基本上,我只有一张图片,所以用循环浏览图片是没有意义的。
然后,对于图像本身,因为它是 BGR,有必要修改表示补丁大小的数组,因此它应该是(224,224,3)
.
最后,为了保存补丁,我在我提出的另一个问题中使用了@Rotem 提供的更正代码。
这是最终结果的样子:
img = cv2.imread("test.jpg")
patches_img = patchify(img, (224,224,3), step=224)
for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i, j, 0, :, :, :]
if not cv2.imwrite('patches/images/' + 'image_' + '_'+ str(i)+str(j)+'.jpg', single_patch_img):
raise Exception("Could not write the image")
推荐阅读
- amazon-s3 - 策略中的 S3 存储桶名称,terraform
- c - GTK+3 大小分配事件不像 GTK+2 那样实时
- flutter - 如何使用 Streambuilder 和 firebase QuerySnapshot 使用 GroupedListView
- javascript - 如何在 CSS 中自动将 div 大小调整为其中的图像大小?
- javascript - 使用 Mongoose 从 MongoDB 中的对象中删除元素
- javascript - 如何创建阿拉伯键盘的脚本?
- azure - Azure 函数 - local.settings.json 与 launchSettings.json
- serenity-platform - 我在 Endpoint 中收到 system.string 值错误?
- php - PHPMailer 接受原始文件名作为附件,但不作为变量
- r - 嵌套类实例 (OOP) 上的 R 方法