2013-02-27 80 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

相關問題