2017-07-11 53 views
0

有谁知道如何使用R中的magick包从16位tiff图像中获取每个通道(RGB)的像素值?目前我使用Mathematica来执行这个操作,因为我找不到在mathematica中做同样的方法。使用R中的magick包从16位tiff图像中获取像素值

我试图从image-magick包中读取像素值,结果是原始类型(例如“ff”)。我使用函数rawToNum(包“包”)将原始类型转换为数字,结果接近我在Mathematica中使用ImageDate函数获得的结果,但不完全相同。

+0

您是否从'tiff'包中的R试过'readTIFF'?我用它来获取tiff图像的RGB值。 –

回答

1

我不知道太多关于R可言,但我想你可以“掏出”并执行一个外部命令,使用system()或诸如此类。

如果,那么,也许你可以使用它。首先,让我们做一个16位TIFF文件,是由红,蓝只有10个像素宽1个像素高梯度:

convert -size 10x1 gradient:red-blue image.tiff 

enter image description here

现在我们可以转储像素使用文件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 

看到数据的另一种方式可以使用odawk这样的:

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。

1

也许在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) 
0

谢谢大家。最佳答案我发现这是由我的学生使用包光栅给出:

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。

0

您还可以使用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