-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.R
217 lines (163 loc) · 5.04 KB
/
Script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
Ruthger Righart
Blogs, tutorials & videos: https://rrighart.github.io
Email: rrighart@googlemail.com
# LOAD DATA
library(jpeg)
myurl <- "https://raw.githubusercontent.com/RRighart/Digits/master/HandwrittenDigits.JPG"
z <- tempfile()
download.file(myurl,z,mode="wb")
img <- readJPEG(z)
file.remove(z)
# DISPLAY A4 SHEET OF DIGITS
par(mfrow=c(1,1),
oma = c(0.5,0.5,0.5,0.5) + 0.1,
mar = c(0,0,0,0) + 0.1)
image(t(apply(img[c(1:dim(img)[1]), c(1:dim(img)[2]), 1], 2, rev)), col=grey.colors(255), axes=F, asp=1)
mtext("Whole image of handwritten digits", cex=0.6, col="red")
# RESIZING DATA
library(EBImage)
ximg<-img[c(1:dim(img)[1]), c(1:dim(img)[2]), 1]
nhsq=42
pix=28
nimg <- resize(ximg, h = nhsq*pix)
dim(nimg)
# SELECTION OF DATA
nimg<-nimg[1:1568, ]
dim(nimg)
# SEGMENTING DATA
matsplitter<-function(M, r, c) {
rg <- (row(M)-1)%/%r+1
cg <- (col(M)-1)%/%c+1
rci <- (rg-1)*max(cg) + cg
N <- prod(dim(M))/r/c
cv <- unlist(lapply(1:N, function(x) M[rci==x]))
dim(cv)<-c(r,c,N)
cv
}
nimg<-nimg[c(1:dim(nimg)[1]), ]
dat<-matsplitter(nimg, 28, 28)
class(dat)
dim(dat)
# ADDING LABELS
labels=rep(c(NA, rep(seq.int(0, 9, by=1),4), NA), 56)
table(labels)
# REMOVAL NA
ndat<-dat[,,which(!is.na(labels))]
nlabels<-labels[which(!is.na(labels))]
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(ndat[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# CREATING NEGATIVE IMAGES
neg <- function(M,i){
apply(M, 3, max)[i]-M[,,i]
}
mmat<-array(0,dim=dim(ndat))
for(i in 1:dim(ndat)[3]){
mmat[,,i]<-neg(ndat,i)
}
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(mmat[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# AVERAGE AND STANDARD DEVIATION (SD) OF DIGIT INTENSITIES (AT THIS POINT...)
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 0:9){
tm<-apply(mmat[,,which(nlabels==i)], c(1,2), mean)
image(t(apply(tm, 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(i, cex=0.8, col="red", side=3, line=-1)
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 0:9){
tm<-apply(mmat[,,which(nlabels==i)], c(1,2), sd)
image(t(apply(tm, 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(i, cex=0.8, col="red", side=3, line=-1)
}
# HISTOGRAMS OF IMAGE INTENSITIES
par(mfrow=c(2,5),
oma = c(2,2,2,2) + 0.1,
mar = c(2,2,2,2) + 0.1)
for(i in 0:9){
tm<-apply(mmat[,,which(nlabels==i)], c(1,2), mean)
hist(tm, labels=FALSE, axes=TRUE, freq=FALSE, col="black", xlim=c(0,1), ylim=c(0,16), main=i)
}
# SCALE IMAGES
range01 <- function(M){(M-min(M))/(max(M)-min(M))}
scmat<-array(0,dim=dim(mmat))
for(i in 1:dim(mmat)[3]){
scmat[,,i]<-range01(mmat[,,i])
}
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(scmat[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# SUMMARY STATS
apply(scmat[,,c(1:10)], 3, min)
apply(scmat[,,c(1:10)], 3, max)
# THRESHOLD IMAGES
thresh <- function(M){ifelse(M<0.2, 0, M)}
thmat<-thresh(scmat)
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(thmat[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# CENTRALIZE IMAGES
bmat<-array(0,dim=dim(thmat))
for(i in 1:dim(thmat)[3]){
temp<-thmat[,,i]
w<-temp[apply(temp,1,mean)>0,apply(temp,2,mean)>0]
if(is.null(dim(w))) next
if(dim(w)[1]<4) next
if(dim(w)[2]<4) next
if(dim(w)[1]>26) next
if(dim(w)[2]>26) next
bim<-matrix(rep(0,28*28),nrow=28)
ly=floor(((dim(bim)[1]-dim(w)[1])/2)+0.5)
uy=ly+dim(w)[1]-1
lx=floor(((dim(bim)[2]-dim(w)[2])/2)+0.5)
ux=lx+dim(w)[2]-1
bim[c(ly:uy),c(lx:ux)]<-w
bmat[,,i]<-bim
}
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(bmat[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# SELECT A FRAME OF 24X24 PIXELS
sfr<-bmat[c(3:26), c(3:26), ]
# DISPLAY DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 1:10){
image(t(apply(sfr[, ,i], 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(nlabels[i], cex=0.8, col="red", side=3, line=-1)
}
# DISPLAY AVERAGE DIGITS
par(mfrow=c(2,5),
oma = c(3,3,3,3) + 0.1,
mar = c(0,0.1,0,0.1) + 0.1)
for(i in 0:9){
tm<-apply(sfr[,,which(nlabels==i)], c(1,2), mean)
image(t(apply(tm, 2, rev)), col=grey.colors(255), axes=F, asp=1); mtext(i, cex=0.8, col="red", side=3, line=-1)
}
# BRING ARRAY TO MATRIX
ownset<-aperm(sfr, c(3,2,1))
dim(ownset)<-c(dim(sfr)[3],576)
ownset<-data.frame(ownset)