rm(list=ls()) library(ReadImages) library(rimage) library(spatstat) #ramp <- colorRamp(c("white", "black")) #rgb( ramp(seq(0, 1, length = 0.5)), max = 1) imgArray <- read.jpeg('testR.jpg') # Type: "rgb", Class: "array", Dim: "130 x 134 x 3" imgArray.grey<-rgb2grey(imgArray) #plot(imgArray.grey) #M<-normalize(imgArray) M<-imgArray.grey #M<-imgArray M<-round(M,digits=3) # Find the most frequently occuring numbers in image matrix #table(M) names(sort(-table(M)))[c(1,2,3,4)] H<-table(M) #barplot(H,length(H)) altM <- matrix(M, nrow=(nrow(M)*ncol(M)), ncol=1); MeanAltM<-apply(altM,2,mean) #MeanAltM stdDevAltM<-apply(altM,2,sd) #stdDevAltM # Find differences between values in names(sort(-table(M)))[c(1,2,3)] the plot histogram imageMatrix<-as.matrix(imgArray) modes<-(sort(as.numeric(sort(-table(M)))))[c(1,2,3,4,5)] modes<-as.numeric(names(modes)) orderedModes<-(-(sort(-as.numeric(names(sort(-table(M))[c(1,2,3,4)]))))[c(1,2,3,4)]) # Could also probably use "factor()" for the below sorting modeBinMarkers<-c(mean(c(orderedModes[1],orderedModes[2])),mean(c(orderedModes[2],orderedModes[3])),mean(c(orderedModes[3],orderedModes[4]))) discretizedM<-(M[]>(modeBinMarkers[1]-stdDevAltM))*1 discretizedM<-(discretizedM + ((M[]<=modeBinMarkers[1]-stdDevAltM) & M[]>modeBinMarkers[2])*0) discretizedM<-(discretizedM + (M[]<=modeBinMarkers[2] & M[]>modeBinMarkers[3])*0) standardizedM<-discretizedM standardizedM[as.logical(discretizedM)==FALSE]<-1 standardizedM[as.logical(discretizedM)==TRUE]<-0 #plot(imagematrix(standardizedM)) #table(discretizedM) # Find distribution of row pixels rowDist<-rep(1,nrow(standardizedM)) for (row in 1:nrow(standardizedM)) { rowDist[row]<-(sum(standardizedM[(nrow(standardizedM)-row),])/nrow(standardizedM)) } #barplot(rowDist, horiz = TRUE) # Find distribution of column pixels colDist<-rep(1,ncol(standardizedM)) for (col in 1:ncol(standardizedM)) { colDist[col]<-(sum(standardizedM[,col])/ncol(standardizedM)) } #barplot(colDist, horiz = FALSE) # Calculate row and column statistics meanRowDist<-mean(rowDist) medianRowDist<-median(rowDist) modeRowDist<-as.numeric(names(sort(-table(rowDist)))[c(1)]) stdDevRowDist<-sd(rowDist) meanColDist<-mean(colDist) medianColDist<-median(colDist) modeColDist<-as.numeric(names(sort(-table(colDist)))[c(1)]) stdDevColDist<-sd(colDist) # Teach character CharacterSym <- readline("What character is this? ") #CharacterSym<-LETTERS[sample(1:26,replace=TRUE)[7]] #fun <- function() { # CharacterSym <- readline("What character is this? ") #} #if(interactive()) fun() LearnedData<-c(meanRowDist,medianRowDist,modeRowDist,stdDevRowDist,meanColDist,medianColDist,modeColDist,stdDevRowDist,CharacterSym) f <- read.table(file = "test.txt", sep=",", header=TRUE, quote="", col.names=c("LineNum","colDist_Mean","colDist_Median","colDist_Mode","colDist_StdDev","rowDist_Mean","rowDist_Median","rowDist_Mode","rowDist_StdDev","CharacterSymbol"), colClasses=list("integer","numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric","character")) a<-cbind(((dim(f)[1])+1),t(LearnedData)) F<-f[,seq(1,10)] F[dim(f)[1]+1,]<-a #attach(F) write.table(F, file = "test.txt", quote=FALSE, sep=",", col.names=names(f),row.names=FALSE)