2016-06-20 19 views
0

EBImageパッケージを使用して、画像をRの数字の行列に変更したいと考えています。私はこのコードを試みたが、それだけで出力全て1のき:EBImageを使ってrgbイメージを行列に変換するには?

library(EBImage) 
img<-readImage("test.jpg") 
imageData(img)[1:50,1:60,1] 

これはイメージにすべての enter image description here

+0

は、rgb jpgのチャームのように機能します。あなたはあなたの.jpgを共有できますか? –

答えて

0

まずあり、それは、JPEG画像ではないですが、PNG。 第2に、RGBではなく、グレースケール+アルファ(ImageMagickによると少なくとも)ですが、アルファチャンネルは完全に不透明であるため、実際のデータは保持されません。 第三に、あなたはすべてのものを得ている理由は、あなたが選択している画像の部分は、1

imageData(img)[51:100,1:60,1]のようなものを試してみて、それがdoesnのかどうかを確認した値で表されているすべての白、すなわち最大強度、ですので、別の結果を出す。

+0

はい、別の結果が得られます。画像のエッジで最大強度値を切り取る方法があります。 EBImageの20X20のマトリックスに合わせて画像のサイズを変更することができます。 – user3266023

1

次の例では、アルファチャンネルを含むグレースケール画像を読み込んでシングルチャンネルのグレースケール画像に変換し、後処理を行います。境界を切り抜いてサイズを変更します。

library(EBImage) 

img <- readImage("http://i.stack.imgur.com/9VTWx.png") 

# grayscale images containing an alpha channel are represented in EBImage as 
# RGBA images by replicating the grayscale intensities over the red, green and 
# blue channels 

print(img, short=TRUE) 
## Image 
## colorMode : Color 
## storage.mode : double 
## dim   : 819 460 4 
## frames.total : 4 
## frames.render: 1 

# convert to grayscale 
img <- channel(img, "gray") 

# collect matrix indices of non-white pixles 
ind <- which(img < 1, arr.ind=TRUE) 

# find min/max indices across rows/columns 
ind <- apply(ind, 2L, range) 
rownames(ind) <- c("min", "max") 

ind 
##  row col 
## min 17 7 
## max 819 413 

# crop the image 
img <- img[ind["min","row"]:ind["max","row"], ind["min","col"]:ind["max","col"]] 

# resize to specific width and height 
img <- resize(img, w=128, h=128) 

display(img) 

enter image description here

基礎となるマトリックス利用imageData(img)を抽出します。

関連する問題