我目前正在研究将主成分分析应用于 R 中的可视数据。
在 Matlab 中,可以调用诸如“im2double”和“mat2gray”之类的命令将位图转换为数值矩阵,然后再转换回图像。
我想知道这是否可以在 R 中实现,也许是通过额外的包。
我目前正在研究将主成分分析应用于 R 中的可视数据。
在 Matlab 中,可以调用诸如“im2double”和“mat2gray”之类的命令将位图转换为数值矩阵,然后再转换回图像。
我想知道这是否可以在 R 中实现,也许是通过额外的包。
我使用了 bioconductor 上可用的 EBImage 包(这里是小插图)来处理和操作图像:
# installing package if needed
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")
library(EBImage)
f = readImage(system.file("images", "lena-color.png", package="EBImage"))
str(f)
#Formal class 'Image' [package "EBImage"] with 2 slots
# ..@ .Data : num [1:512, 1:512, 1:3] 0.886 0.886 0.875 0.875 0.886 ...
# ..@ colormode: int 2
我很好奇想试试这个;显然,一个包是一个更好的解决方案,但如果你真的想坚持使用基础 R,这将加载一个 png (尽管颠倒和向后;这可能是可以修复的)。它假定存在 netpbm 工具,因此可能无法在 Windows 系统上开箱即用。
readPng <- function(pngFile) {
contents <- system(paste('pngtopnm',pngFile,'| pnmtoplainpnm'),intern=TRUE)
imgDims <- strsplit(contents[2], ' ')
width <- as.numeric(imgDims[[1]][1])
height <- as.numeric(imgDims[[1]][2])
rawimg <- scan(textConnection(contents),skip=3)
return(list(
x=1:width,
y=1:height,
z=matrix(rawimg,width),
width=width,
height=height))
}
您可以image(img)
直接在此函数返回的列表上运行,或使用 img$z 访问每个像素的值。
安装包的两种方法。
转到可以执行 R 命令的提示。这些是基本的图像处理命令。
执行此命令安装 Bioconductor backage biocLite,这将有助于安装 EBIMage 包(此包广泛用于图像处理)
source("http://bioconductor.org/biocLite.R")
安装 EMImage 包以使用图像处理命令。
biocLite("EBImage")
加载 EBIMage 包以使用图像处理
library("EBImage")
# Reading image from computer
img=readImage(files="~/Desktop/Prog/R/tinago.JPG")
display(img)
img1=img+ 0.2 # increase brightness
img2=img- 0.2 # decrease brightness
display(img1) # Display images in browser or graphical window
display(img2) # Display images in browser or graphical window
img3= img * 0.5 # decrease contrast
img4=img * 2 # increase contrast
display(img3); display(img4) # show result images
img5=img^2 # increase Gamma correction
img6=img^0.7 # decrease Gamma correction
display(img5); display(img6) # Display result images
注意:readImage 读取图像。显示用于在图形窗口中查看图像。
相对较新的软件包tiff
可以很好地读写 TIF 图像。
尽管如此,除了相对简单的图像处理之外,我建议使用来自哈佛-史密森学会的 ImageJ 或 SAOImage9: http ://www.cfa.harvard.edu/resources/software.html 。
我在 R 中编写了工具来进行像素合并、像素分割、Sobel & Hough 变换、脱色等,并取得了巨大的成功。最终,应用程序的选择取决于图像的大小和需要进行的处理类型。