有没有人能帮上忙呢?我想通过使用随机采样来减小图像大小,但无法确定如何设置输入补丁的限制。
# New smaller image
img_small = np.zeros((img.shape[0] // factor, img.shape[1] // factor),
dtype=np.int64)
# Loop over the rows of the smaller image
for i in range(img_small.shape[0]):
# Loop over the columns of the smaller image
for j in range(img_small.shape[1]):
# The input patch should consist of rows from factor * i to
# factor * (i + 1) - 1, and columns from factor * j to
# factor * (j + 1) - 1
# input_patch = img[ # Extract the input patch
# Can use np.random.choice(input_patch.flatten(), ...) to choose random
# pixels from input_patch
# img_small[i, j] = # Set the output pixel
img_small[i, j] = 发布于 2020-12-05 00:23:49
限制在注释中给出,只需将它们应用于数组即可。使用示例图像

并使用您的代码(添加图像加载并将其转换为灰度-如果需要颜色,您将需要添加颜色处理):
from PIL import Image
import numpy as np
from matplotlib.pyplot import imshow
# load the image and convert to greyscale
image = Image.open('imglrg0.jpg').convert('LA')
# convert image to numpy array
img_lrg = np.asarray(image)
#imshow(img_lrg)
factor = 8
# New smaller image
img_small = np.zeros((img_lrg.shape[0] // factor, img_lrg.shape[1] // factor),
dtype=np.int64)
# Loop over the rows of the smaller image
for i in range(img_small.shape[0]):
# Loop over the columns of the smaller image
for j in range(img_small.shape[1]):
# The input patch should consist of rows from factor * i to
# factor * (i + 1) - 1, and columns from factor * j to
# factor * (j + 1) - 1
# input_patch = img[ # Extract the input patch
input_patch = img_lrg[i * factor:(i+1) * factor - 1, j * factor:(j+1) * factor - 1]
# Can use np.random.choice(input_patch.flatten(), ...) to choose random
# pixels from input_patch
# img_small[i, j] = # Set the output pixel
img_small[i, j] = np.random.choice(input_patch.flatten())
imshow(np.asarray(img_small))这将导致(对于factor=8。这不是最好的结果,但很容易辨认。也许可以尝试一下采样,以提高性能。我只是简单地使用matplotlib来快速显示结果,所以它是非彩色的。):

就像在采样上增加一样:像这样选择三个点的平均值img_small[i, j] = np.average(np.random.choice(input_patch.flatten(), 3))会带来实质性的改进:

https://stackoverflow.com/questions/65144804
复制相似问题