###################################################
### chunk number 1: 
###################################################
#line 32 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
library(CRImage)


###################################################
### chunk number 2: 
###################################################
#line 38 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
f = system.file("extdata", "exImg2.jpg", package="CRImage")
exImgB=readImage(f)
#find white pixels and exclude them from thresholding(if white is background)
indexWhitePixel=which(exImgB[,,1]>0.85 & exImgB[,,2]>0.85 & exImgB[,,3]>0.85)
#convert to grayscale
exImgB=channel(exImgB,"gray")
#calculate threshold
t=calculateThreshold(as.vector(exImgB[-indexWhitePixel]))
#create binary image
exImgB[which(exImgB>=t)]=1
exImgB[which(exImgB<t)]=0


###################################################
### chunk number 3: 
###################################################
#line 60 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
f = system.file("extdata", "exImg2.jpg", package="CRImage")
segmentationValues=segmentImage(f,maxShape=800,minShape=40,failureRegion=2000) 


###################################################
### chunk number 4: 
###################################################
#line 114 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
f = system.file("extdata", "trainingData.txt", package="CRImage")
#read training data
trainingData=read.table(f,header=TRUE)
#create classifier
classifierValues=createClassifier(trainingData,topo=FALSE)
classifier=classifierValues[[1]]
#classifiedCells=classifierValues[[2]]
#display(classifiedCells)


###################################################
### chunk number 5: 
###################################################
#line 127 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
	#classify cells
	f = system.file("extdata", "exImg2.jpg", package="CRImage")
	classValues=classifyCells(classifier,filename=f,KS=TRUE,maxShape=800,minShape=40,failureRegion=2000)


###################################################
### chunk number 6: 
###################################################
#line 134 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
t = system.file("extdata", "trainingData.txt", package="CRImage")
#read training data
trainingData=read.table(t,header=TRUE)
#create classifier
classifier=createClassifier(trainingData,topo=FALSE)[[1]]
#calculation of cellularity
f = system.file("extdata", "exImg2.jpg", package="CRImage")
exImg=readImage(f)
cellularityValues=calculateCellularity(f,classifier=classifier,cancerIdentifier="1")


###################################################
### chunk number 7: 
###################################################
#line 154 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
#create the classifier
t = system.file("extdata", "trainingData.txt", package="CRImage")
trainingData=read.table(t,header=TRUE)
classifier=createClassifier(trainingData,topo=FALSE)[[1]]


###################################################
### chunk number 8:  eval=FALSE
###################################################
## #line 160 "E:/biocbld/bbs-2.7-bioc/tmpdir/Rtmpszdtyb/R.INSTALL12104b77/CRImage/inst/doc/CRImage.Rnw"
## dir.create("AperiOutput")
## f = system.file("extdata",  package="CRImage")
## #the image in CWS format
## f=file.path(f,"8905")
## processAperio(classifier=classifier,inputFolder=f,outputFolder="AperiOutput",identifier="Da",numSections=2,cancerIdentifier="c",maxShape=800,minShape=40,failureRegion=2000)


