调整R中的图像大小 [英] Resizing image in R
问题描述
我正在尝试使用R中的一些图像数据,并且无法弄清楚如何调整图像大小以确保它们的大小相同。
I am trying to work with some image data in R, and cannot figure out how to resize the images I have to ensure they are all the same size.
在Python中,我解决了这个问题如下:
In Python, I approached this problem as follows:
from PIL import Image
import numpy as np
size = (100, 100)
img = Image.open(filename)
img = img.resize(size)
img = np.array(img.getdata())
在R中,我无法找到能够完成同样事情的库。
我能得到的最远的是:
In R, I've been unable to locate a library that would accomplish the same thing. The farthest I've been able to get is:
library(jpeg)
img <- readJPEG(filename)
# Need something here to resize
img <- as.matrix(img)
最简单的方法是像Pillow这样的库我可以打电话,但正如我所说,我似乎无法找到任何东西。
The easiest approach would be a library like Pillow that I could call on, but as I said, I can't seem to find anything.
谢谢,
推荐答案
您可以在Bioconductor包的帮助下轻松完成此操作 EBImage , R 的图像处理和分析工具箱。要安装软件包,请使用:
You can easily accomplish this with the help of the Bioconductor package EBImage, an image processing and analysis toolbox for R. To install the package use:
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")
然后,您可以使用 EBImage 加载和缩放图像,如下例所示。
You can then use the functionality provided by EBImage to load and scale the image, as in the following example.
library("EBImage")
x <- readImage(system.file("images", "sample-color.png", package="EBImage"))
# width and height of the original image
dim(x)[1:2]
# scale to a specific width and height
y <- resize(x, w = 200, h = 100)
# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)
# show the scaled image
display(y)
# extract the pixel array
z <- imageData(y)
# or
z <- as.array(y)
有关 EBImage提供的功能的更多示例请参阅插图。
这篇关于调整R中的图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!