首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像处理中的随机采样

图像处理中的随机采样
EN

Stack Overflow用户
提问于 2020-12-04 21:51:45
回答 1查看 37关注 0票数 0

有没有人能帮上忙呢?我想通过使用随机采样来减小图像大小,但无法确定如何设置输入补丁的限制。

代码语言:javascript
复制
# 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] = 
EN

回答 1

Stack Overflow用户

发布于 2020-12-05 00:23:49

限制在注释中给出,只需将它们应用于数组即可。使用示例图像

并使用您的代码(添加图像加载并将其转换为灰度-如果需要颜色,您将需要添加颜色处理):

代码语言:javascript
复制
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))会带来实质性的改进:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65144804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档