有誰知道如何使用R中的magick包從16位tiff圖像中獲取每個通道(RGB)的像素值?目前我使用Mathematica來執行這個操作,因爲我找不到在mathematica中做同樣的方法。使用R中的magick包從16位tiff圖像中獲取像素值
我試圖從image-magick包中讀取像素值,結果是原始類型(例如「ff」)。我使用函數rawToNum(包「包」)將原始類型轉換爲數字,結果接近我在Mathematica中使用ImageDate函數獲得的結果,但不完全相同。
有誰知道如何使用R中的magick包從16位tiff圖像中獲取每個通道(RGB)的像素值?目前我使用Mathematica來執行這個操作,因爲我找不到在mathematica中做同樣的方法。使用R中的magick包從16位tiff圖像中獲取像素值
我試圖從image-magick包中讀取像素值,結果是原始類型(例如「ff」)。我使用函數rawToNum(包「包」)將原始類型轉換爲數字,結果接近我在Mathematica中使用ImageDate函數獲得的結果,但不完全相同。
我不知道太多關於R
可言,但我想你可以「掏出」並執行一個外部命令,使用system()
或諸如此類。
如果,那麼,也許你可以使用它。首先,讓我們做一個16位TIFF文件,是由紅,藍只有10個像素寬1個像素高梯度:
convert -size 10x1 gradient:red-blue image.tiff
現在我們可以轉儲像素使用文件ImageMagick的:
convert image.tiff rgb:image.rgb
# Now check its length - yes, 60 bytes = 10 pixels with 2 bytes each for RG &B
ls -l image.rgb
-rw-r--r-- 1 mark staff 60 11 Jul 10:32 image.rgb
我們還可以將數據寫入stdout
這樣的:
convert image.tiff rgb:-
,也看它每行1個像素(6個字節)
convert image.tiff rgb:- | xxd -g 3 -c 6
00000000: ffff00 000000 ...... # Full Red, no Green, no Blue
00000006: 8de300 00721c ....r. # Lots of Red, no Green, a little Blue
0000000c: 1cc700 00e338 .....8
00000012: aaaa00 005555 ....UU
00000018: 388e00 00c771 8....q
0000001e: c77100 00388e .q..8.
00000024: 555500 00aaaa UU....
0000002a: e33800 001cc7 .8....
00000030: 721c00 008de3 r.....
00000036: 000000 00ffff ...... # No Red, no Green, full Blue
我希望你可以做這樣的事情在R
,有:
system("convert image.tif rgb:-")
另一個傾倒像素的方法可能與Perl
一起sl the整個文件,然後解壓縮包含的無符號短褲並每行打印一行:
convert image.tiff rgb: | perl -e 'my $str=do{local $/; <STDIN>}; print join("\n",unpack("v*",$str)),"\n";'
樣本輸出
65535 # Full Red
0 # No Green
0 # No Blue
58253 # Lots of Red
0 # No Green
7282 # A little Blue
50972 # Moderate Red
0
14563
43690
0
21845
36408
0
29127
29127
0
36408
21845
0
43690
14563
0
50972
7282
0 # No Green
58253 # Lots of Blue
0 # No Red
0 # No Green
65535 # Full Blue
看到數據的另一種方式可以使用od
和awk
這樣的:
convert image.tiff rgb: | od -An -tuS | awk '{for(i=1;i<=NF;i++){print $i}}'
65535
0
0
58253
0
7282
50972
0
14563
43690
0
21845
36408
0
29127
29127
0
36408
21845
0
43690
14563
0
50972
7282
0
58253
0
0
65535
其中-An
抑制地址的印刷,並且-tuS
表示數據的類型未簽名爲short。
也許在ImageMagick中稍微簡單一些的方法是使用txt:output格式。
用標誌瑟特查的形象:
convert -size 10x1 gradient:red-blue image.tiff
使用TXT:作爲
convert image.tiff txt: | sed -n 's/^.*[(]\(.*\)[)].*[#].*$/\1/p'
產地:
65535,0,0
58253,0,7282
50972,0,14563
43690,0,21845
36408,0,29127
29127,0,36408
21845,0,43690
14563,0,50972
7282,0,58253
0,0,65535
或使用TXT:包括像素座標
convert image.tiff txt: | sed -n 's/^\(.*[)]\).*[#].*$/\1/p'
產地:
0,0: (65535,0,0)
1,0: (58253,0,7282)
2,0: (50972,0,14563)
3,0: (43690,0,21845)
4,0: (36408,0,29127)
5,0: (29127,0,36408)
6,0: (21845,0,43690)
7,0: (14563,0,50972)
8,0: (7282,0,58253)
9,0: (0,0,65535)
謝謝大家。最佳答案我發現這是由我的學生使用包光柵給出:
library(raster)
img <- stack(filename)
x <- as.matrix(raster(img, 1)) # here we specify the layer 1, 2 or 3
唯一的問題是,包光柵的功能as.matrix可從基礎包的一個困惑,所以可能需要指定raster :: as.matrix。
您還可以使用magick
包作爲數字陣列訪問像素。該示例基於包中的this vignette。
library(magick)
tiger <- image_read('http://jeroen.github.io/images/tiger.svg')
tiger_tiff <- image_convert(tiger, "tiff")
# Access data in raw format and convert to integer
tiger_array <- as.integer(tiger_tiff[[1]])
然後,如果檢查的尺寸和類型,你可以:
dim(tiger_array)
[1] 900 900 4
is.numeric(tiger_array)
[1] TRUE
您是否從'tiff'包中的R試過'readTIFF'?我用它來獲取tiff圖像的RGB值。 –