2015-07-09 118 views
-1

我不認爲我理解功能readPNG。在幫助我們可以發現這一段代碼readPNG函數的結果是什麼?

img <- readPNG(system.file("img", "Rlogo.png", package="png")) 

img是numieric向量NUM [1:76 1:100 1:4](但它是在四個矩陣形式dispalyed)

當我讀了另一個PNG文件(25 x 25,邏輯[只有黑色或白色像素]),我得到了矢量數[1:25 1:25 1:3]。看起來這三個邏輯矩陣是相同的。你能解釋一下readPNG函數的結果嗎?爲什麼一旦它顯示爲4個矩陣,一次是三個?

+1

你沒有提到該函數來自包** png **。您是否閱讀過'readPNG'文檔的Value部分?你能說出特別令人困惑的解釋嗎? – joran

+0

是不是因爲文件解壓縮成RGB(3通道)? – daniel

回答

1

雖然我會承認許多R手冊頁是鈍的,這一個是相當不錯的。

Value 

If native is FALSE then an array of the dimensions height x width x 
channels. If there is only one channel the result is a matrix. The 
values are reals between 0 and 1. If native is TRUE then an object of 
the class nativeRaster is returned instead. The latter cannot be 
easily computed on but is the most efficient way to draw using 
rasterImage. 

Most common files decompress into RGB (3 channels), RGBA (4 channels), 
Grayscale (1 channel) or GA (2 channels). Note that G and GA images 
cannot be directly used in rasterImage unless native is set to TRUE 
because rasterImage requires RGB or RGBA format (nativeRaster is 
always 8-bit RGBA). 

As of png 0.1-2 files with 16-bit channels are converted in full 
resolution to the array format, but the nativeRaster format only 
supports 8-bit and therefore a truncation is performed (eight least 
significant bits are dropped) with a warning if native is TRUE. 
+0

謝謝,但我仍然有一個問題。我的圖像是grauscale(實際上它只有兩種顏色,黑色或白色),所以R爲什麼看到它與3 chanells而不是1的圖像?我得到值爲0-1的矩陣(它們完全相同)。有什麼方法可以改變它嗎? – jjankowiak

相關問題