任何人都知道如何使用R中的Magick软件包从16位TIFF图像中获取每个通道(RGB)的像素值?目前,我正在使用Mathematica执行此操作,因为我找不到在Mathematica中进行同等的方法。
i试图从image-magick软件包中读取像素值,结果是原始类型(例如“ ff”)。我使用函数Rawtonum(软件包“ PACK”)将原始类型转换为数字,结果接近我在Mathematica中使用ImageDate函数获得的结果,但并不完全相同。
magick
软件包作为数字阵列访问像素。该示例基于包装中的thisvignette
。
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
我对
R
system()
或somesuch。
,如果,也许您可以使用此。首先,让我们制作一个16位TIFF文件,这是红蓝色宽度仅10像素和1像素高的梯度:
convert -size 10x1 gradient:red-blue image.tiff
现在,我们可以使用ImageMagick我们还可以将数据写入to以下:
:
stdout
还用每行1像素(6个字节)看它
convert image.tiff rgb:-
我希望您能在
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:-")
一起lur以溶解整个文件,然后打开包含的未签名短裤,然后每行打印一条:
Perl
样本输出
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
where
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
说数据的类型均不签名。也许ImageMagick的一种稍微简单的方法是使用TXT:输出格式。 使用Mark Setchell的图像:
-tuS
使用txt:as
convert -size 10x1 gradient:red-blue image.tiff
生产:convert image.tiff txt: | sed -n 's/^.*[(]\(.*\)[)].*[#].*$/\1/p'
或使用TXT:包括像素坐标
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
生产:
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)
唯一的问题是函数为。软件包的matrix可能会与基本软件包中的函数混淆,因此可能有必要指定栅格::as.matrix.
@nate_a给出了答案,但不到一美元的三分钱。 After
library(raster)
img <- stack(filename)
x <- as.matrix(raster(img, 1)) # here we specify the layer 1, 2 or 3
dim(tiger_array)
[1] 900 900 4
或如果您喜欢0-255
tiger_array[1,1,1] # red
tiger_array[1,1,2] # green
tiger_array[1,1,3] # blue