Skip to content

Commit 431e3d5

Browse files
authored
Add files via upload
1 parent 00733a6 commit 431e3d5

File tree

3 files changed

+476
-0
lines changed

3 files changed

+476
-0
lines changed

Example_6_2/LDA1.R

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#install.packages("lda")
2+
#install.packages("jpeg")
3+
4+
# expand lib on memory
5+
library( MASS ) # MASS package
6+
library( jpeg ) # jpeg画像の読み込み
7+
8+
# set options
9+
options( digits=7 ) # 表示桁数
10+
11+
# load image data (jpeg)
12+
jpeg_NekoSensei <- readJPEG( "nekosensei_greyscale.jpg" ) # 560*420 = 235200 pixel(3data<rgb> per 1 pixel)
13+
class( jpeg_NekoSensei ) # 3次元配列 um[1:420, 1:560, 1:3]
14+
attributes( jpeg_NekoSensei )
15+
16+
########################################
17+
# convert to thresholding/binary image #
18+
########################################
19+
dfNekoSensei <- as.data.frame( expand.grid(jpeg_NekoSensei) ) # convert to data frame
20+
dat_levels <- c(
21+
rep( min(dfNekoSensei$Var1), length(dfNekoSensei$Var1)/2 ),
22+
rep( max(dfNekoSensei$Var1), length(dfNekoSensei$Var1)/2 )
23+
) # binary data set (0 or 1)
24+
25+
dat_lda <- lda( dat_levels ~ . , data = dfNekoSensei )
26+
print( dat_lda )
27+
28+
# 線形判別係数 [coefficients of linear discriminants] から線形識別関数を求める
29+
linerC <- apply( dat_lda$means%*%dat_lda$scaling, 2, mean ) # 定数項C
30+
cat("\nConstant term:\n")
31+
print(linerC)
32+
33+
# print idification result in table
34+
result <- predict( dat_lda )
35+
tbl <- table( dat_levels, result$class )
36+
cat("\nIdification result:")
37+
print(tbl)
38+
39+
# result$class convert to vector
40+
imgNekosensei1 <- array(
41+
data = rep( 0,length(dfNekoSensei$Var1) ),
42+
dim = c(420,560,3)
43+
)
44+
imgNekosensei2 <- array(
45+
data = rep( 0,length(dfNekoSensei$Var1) ),
46+
dim = c(420,560,3)
47+
)
48+
49+
for( i in 1:length(imgNekosensei) )
50+
{
51+
if( result$class[i] == 1 )
52+
{
53+
imgNekosensei1[i] <- 1
54+
imgNekosensei2[i] <- 0
55+
}
56+
else
57+
{
58+
imgNekosensei1[i] <- 0
59+
imgNekosensei2[i] <- 1
60+
}
61+
}
62+
63+
############################
64+
# set graphics parameters #
65+
############################
66+
# 軸に関してのデータリスト
67+
lstAxis <- list(
68+
xMin = 0.0, xMax = 1.0, # x軸の最小値、最大値
69+
yMin = 0.0, yMax = 1.0, # y軸の最小値、最大値
70+
zMin = 0.0, zMax = 1.0, # z軸の最小値、最大値
71+
xlim = range( c(0.0, 1.0) ),
72+
ylim = range( c(0.0, 1.0) ),
73+
zlim = range( c(0.0, 1.0) ),
74+
mainTitle = "mainTitle", # 図のメインタイトル(図の上)
75+
subTitle = "subTitle", # 図のサブタイトル(図の下)
76+
xlab = "x", # x軸の名前
77+
ylab = "y", # y軸の名前
78+
zlab = "z" # z軸の名前
79+
)
80+
lstAxis$xMin <- 0
81+
lstAxis$xMax <- 560
82+
lstAxis$yMin <- 0
83+
lstAxis$yMax <- 420
84+
lstAxis$xlim = range( c(lstAxis$xMin, lstAxis$xMax) )
85+
lstAxis$ylim = range( c(lstAxis$yMin, lstAxis$yMax) )
86+
lstAxis$zlim = range( c(lstAxis$zMin, lstAxis$zMax) )
87+
lstAxis$xlab <- "x1"
88+
lstAxis$ylab <- "x2"
89+
lstAxis$mainTitle <- "ねこ先生(グレースケール)[Greyscale]" # 図のメインタイトル(図の上)
90+
91+
# plot frame only
92+
par(new=F)
93+
plot.new() # clear
94+
plot( c(), type='n',
95+
main = lstAxis$mainTitle,
96+
xlim=lstAxis$xlim, ylim=lstAxis$ylim,
97+
xlab=lstAxis$xlab, ylab=lstAxis$ylab
98+
)
99+
#grid() #グリッド線を追加
100+
101+
############################
102+
# Draw Image and figure #
103+
############################
104+
# draw original image
105+
rasterImage(
106+
image = jpeg_NekoSensei,
107+
xleft = lstAxis$xMin, xright = lstAxis$xMax,
108+
ybottom = lstAxis$yMin, ytop = lstAxis$yMax
109+
)
110+
111+
# draw converted image
112+
lstAxis$mainTitle <- "ねこ先生(2値化処理後)" # 図のメインタイトル(図の上)
113+
plot( c(), type='n',
114+
main = lstAxis$mainTitle,
115+
xlim=lstAxis$xlim, ylim=lstAxis$ylim,
116+
xlab=lstAxis$xlab, ylab=lstAxis$ylab
117+
)
118+
rasterImage(
119+
image = imgNekosensei1, # 変換後のデータ
120+
xleft = lstAxis$xMin, xright = lstAxis$xMax,
121+
ybottom = lstAxis$yMin, ytop = lstAxis$yMax
122+
)
123+
124+
lstAxis$mainTitle <- "ねこ先生(2値化処理後<反転>)" # 図のメインタイトル(図の上)
125+
plot( c(), type='n',
126+
main = lstAxis$mainTitle,
127+
xlim=lstAxis$xlim, ylim=lstAxis$ylim,
128+
xlab=lstAxis$xlab, ylab=lstAxis$ylab
129+
)
130+
rasterImage(
131+
image = imgNekosensei2, # 変換後のデータ
132+
xleft = lstAxis$xMin, xright = lstAxis$xMax,
133+
ybottom = lstAxis$yMin, ytop = lstAxis$yMax
134+
)

Example_6_2/nekosensei_greyscale.jpg

33.4 KB
Loading

0 commit comments

Comments
 (0)