【问题标题】:Resize image with skimage library without stretching使用 skimage 库调整图像大小而不拉伸
【发布时间】:2015-02-14 02:42:30
【问题描述】:

嘿,我正在尝试调整图像大小而不拉伸它,而是添加白色像素。我环顾四周,但没有发现具体说明如何从 skimage 中实现这一点。因此,我使用 numpy 在调整大小之前将额外的像素添加为 [float(255)] 的数组。

from skimage.io import imread
from skimage.io import imsave
from skimage.transform import resize
from matplotlib import pyplot as plt
from pylab import cm
import numpy as np
from skimage import morphology
from skimage import measure
from scipy import misc
def process(file_):
    im = imread(file_, as_grey=True)
    #im = misc.imread(file_)
    #im=np.fromfile(file_, dtype=np.int64)

    #Filler to avoid stretching
    orig_rows, orig_cols = im.shape
    print orig_rows, orig_cols
    if orig_rows < orig_cols:
        for addition in range(0,orig_cols-orig_rows):
            #adding white rows
            lst = np.array(list(float(255) for x in range(0,orig_cols)))
            im= np.vstack((im,lst))
    if orig_rows > orig_cols:
        for addition in range(0,orig_rows-orig_cols):
            #adding white columns
            lst = np.array(list([float(255)] for x in range(0,orig_rows)))
            im= np.hstack((im,lst))
    image = resize(im, (48, 48))
    imsave('test.jpg',im)
    imsave('test1.jpg',image)
    plt.imshow(im, cmap=cm.gray)
    plt.show()

当我用 pyplot 查看图像时,它看起来像这样 我们可以看到已经添加了列,但是在我保存图像之后

image = resize(im, (48, 48))
    imsave('test.jpg',im)
    imsave('test1.jpg',image)

图像看起来像底片,调整大小的图像看起来完全是白色的(在黑暗的旁边,它在网站背景上是不可见的)。有什么想法吗?

【问题讨论】:

  • 我在尝试您的代码时没有遇到这个问题。您能否提供更多信息(py 版本、操作系统等)
  • @IronManMark20 python 2.7,linux kali,amd64。所以你得到了预期的图像?
  • 是的,我在 Windows 8.1 python 2.7.8 amd64 上做过。两次保存都是白色背景上的黑色。你试过升级 skimage 吗?
  • 你可以使用 numpy.pad 函数来代替

标签: python image numpy


【解决方案1】:

下面的代码应该可以工作。请注意,填充区域的颜色不完全是白色,以便查看上传图像中的图像边界。用于白色填充集fill_cval = np.max(img)

def resize_padded(img, new_shape, fill_cval=None, order=1):
    import numpy as np
    from skimage.transform import resize
    if fill_cval is None:
        fill_cval = np.max(img)
    ratio = np.min([n / i for n, i in zip(new_shape, img.shape)])
    interm_shape = np.rint([s * ratio for s in img.shape]).astype(np.int)
    interm_img = resize(img, interm_shape, order=order, cval=fill_cval)

    new_img = np.empty(new_shape, dtype=interm_img.dtype)
    new_img.fill(fill_cval)

    pad = [(n - s) >> 1 for n, s in zip(new_shape, interm_shape)]
    new_img[[slice(p, -p, None) if 0 != p else slice(None, None, None) 
             for p in pad]] = interm_img

    return new_img

import numpy as np
import matplotlib.pylab as plt
from skimage.data import astronaut
from skimage.color import rgb2gray  # using luminance
from skimage.io import imsave
img = rgb2gray(astronaut())

# set desired image size
out_size = (30, 100)  # height, width

# set the color of the padded area. Here: "95% luminance"
fill_cval = np.max(img) * 0.95

resized_img = resize_padded(img, out_size, fill_cval=fill_cval)

imsave('img.png', img)
imsave('img_res.png', resized_img)

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多