Rで解析:画像のプロットに便利です「imager」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

画像のプロットに便利なパッケージの紹介です。プロットだけでなく画像の輝度やRGBの情報も取得できるので便利です。記事では最低限な使用方法を紹介しています。他の利用方法はオフィシャルサイトを確認していただければと思います。

参考画像として、劇場版「響け!ユーフォニアム~北宇治高校吹奏楽部へようこそ~」が大変素晴らしかったので、ブルーレイより第8話のシーンを利用しています。ゴールデンウィークが暇ならオススメの映画です。

・「imager」パッケージのオフィシャルサイト
 https://cran.r-project.org/web/packages/imager/vignettes/gettingstarted.html

・劇場版「響け!ユーフォニアム~北宇治高校吹奏楽部へようこそ~」サイト
 http://anime-eupho.com/

パッケージバージョンは0.42.16。実行コマンドはwindows 7およびOS X 10.11.2のR version 3.2.3で確認しています。

スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("imager")

実行コマンドの紹介

詳細はコマンド内を確認ください。

#パッケージの読み込み
library("imager")

#画像ファイルの読み込み
#install.packages("tcltk")
library("tcltk")
#ファイルを選択
PictPath <- paste0(as.character(tkgetOpenFile(title = "&#30011;&#20687;&#12501;&#12449;&#12452;&#12523;&#12434;&#36984;&#25246;",
                                              filetypes = '{"&#30011;&#20687;&#12501;&#12449;&#12452;&#12523;" {".*"}}',
                                              initialfile = "*.*")))

#&#25351;&#23450;&#12375;&#12383;&#12501;&#12449;&#12452;&#12523;&#12434;&#35501;&#12415;&#36796;&#12415;:load.image&#12467;&#12510;&#12531;&#12489;
PlotPic <- load.image(PictPath)
#&#20869;&#23481;&#12434;&#12503;&#12525;&#12483;&#12488;
plot(PlotPic)
###&#21442;&#32771;:ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;&#12391;&#12503;&#12525;&#12483;&#12488;#####
GGplotData <- as.data.frame(PlotPic, wide = "c")
#&#12300;plyr&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12391;&#20966;&#29702;
GGplotData <- mutate(GGplotData, rgb.val = rgb(c.1, c.2, c.3))
#&#12503;&#12525;&#12483;&#12488;
ggplot(GGplotData, aes(x, y)) + geom_raster(aes(fill = rgb.val)) +
  scale_fill_identity() + scale_y_reverse(expand = c(0, 0)) + 
  scale_x_continuous(expand = c(0, 0))

#RGB&#12398;&#24773;&#22577;&#12434;&#12503;&#12525;&#12483;&#12488;
#&#24517;&#35201;&#12394;&#12521;&#12452;&#12502;&#12521;&#12522;&#12434;&#35501;&#12415;&#36796;&#12415;
#&#24517;&#35201;&#12395;&#24540;&#12376;&#12390;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;
#install.packages(c("ggplot2", "plyr"))
library("ggplot2")
library("plyr")
#&#12503;&#12525;&#12483;&#12488;&#12487;&#12540;&#12479;&#12398;&#28310;&#20633;
PlotPicData <- as.data.frame(PlotPic)
#&#12300;plyr&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12391;&#20966;&#29702;
PlotData <- mutate(PlotPicData, channel = factor(cc, labels = c("R", "G", "B")))
#ggplot&#12391;&#12503;&#12525;&#12483;&#12488;
ggplot(PlotData, aes(value, col = channel)) +
  geom_histogram(bins = 30) + facet_wrap(~channel)

#&#12501;&#12449;&#12452;&#12523;&#12434;&#12464;&#12524;&#12540;&#12473;&#12465;&#12540;&#12523;&#12391;&#12503;&#12525;&#12483;&#12488;:grayscale&#12467;&#12510;&#12531;&#12489;
GrayPlotPic <- grayscale(PlotPic)
#&#20869;&#23481;&#12434;&#12503;&#12525;&#12483;&#12488;
plot(GrayPlotPic)
#&#30011;&#20687;&#12398;&#36637;&#24230;&#12398;&#24773;&#22577;&#12434;&#12503;&#12525;&#12483;&#12488;
hist(GrayPlotPic, main = "Luminance")

出力例

・load.imageコマンド

load.image

・load.imageコマンド;ggplot2を利用

load.image_ggplot2

・RGBの情報をプロット

RGBPlot

・grayscaleコマンド

grayscale

少しでも、あなたのウェブや実験の解析が楽になりますように!!

タイトルとURLをコピーしました