2013-02-27 78 views
4

当我这样做时,是否有将图像插入到图中并设置其颜色的方法?我想插入给定数据集的轮廓,并将其设置为与我选择用于绘制相应数据点的颜色相匹配。我对如何管理图形没有深入的了解 - 一般是在计算机系统中,而在R中 - 可能会告知这个问题的答案。如何在r中设置插入图像的颜色

下面的代码将插入图像,但我不知道改变颜色的方法。

require(jpeg) 
thiscolor <- "red" 
plot(x=c(1, 4), y=c(1, 2), main="test plot", col=thiscolor) 
thispic <- readJPEG(<insert path for any image here>) 
rasterImage(thispic, 
     xleft=3, xright=3.5, 
     ytop=2, ybottom=1.8, 
) 
+1

嗨,如果您的图像已经是剪影,您可能会发现。很容易。只需在照片编辑器中打开图像(如果您在Mac上,则可以使用“预览”),然后根据需要调整“饱和度”并调整“色调”。 – 2013-02-27 20:14:20

回答

3

我不明白这里的轮廓究竟是什么意思。但对我来说,光栅是一种颜色矩阵。所以你可以改变它的颜色。这里是一个示范。我正在使用grid.rastergrid包。但它与rasterImage

这里的例子同样的事情:

library(png) 
library(grid) 
img <- readPNG(system.file("img", "Rlogo.png", package="png")) 
## convert it to a raster, interpolate =F to select only sample of pixels of img 
img.r <- as.raster(img,interpolate=F) 
## Change all the white to a blanck 
img.r[img.r == "#00000000"] <- 'red' 
plot.new() 
grid.raster(img.r) 

enter image description here

1

太谢谢你了!

因为我使用R颜色名称,所以必须进行一些(不合格的)颜色转换。但是你的代码正是我所需要的!谢谢!

#convert image to raster 
thispic.r <- as.raster(thispic) 
#get the hex color 
rgbratios <- col2rgb(thiscolor)/255 
thiscolorhex <- rgb(red=rgbratios[1], 
         green=rgbratios[2], 
         blue=rgbratios[3]) 
#convert the non-white part of the image to the hex color 
thispic.r[thispic.r!="#FFFFFF"] <- thiscolorhex 
#plot it 
grid.raster(thispic.r, x=xval, y=yval, just="center") 
+0

很高兴我的答案(几个月前)帮助你:) – agstudy 2013-07-05 14:55:21

相关问题