Created
September 4, 2018 18:32
-
-
Save tractatus/59bba696a194814651c430dd344af80e to your computer and use it in GitHub Desktop.
RUn this code to get access to experimental imgSWT function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
imgSWT <- function(input, filter=NULL, scales=6, cell.bodies = 3, processes = 5, family='db2', sigma=10, processLength=12, alim=c(200, 500), pch=21, bg="white", cex=2, lwd=2, illustrator=F, output='waveletoutput') { | |
file <- as.character(input)[1] | |
family <- as.character(family)[1] | |
outputfile <- as.character(output)[1] | |
scales<-as.integer(scales) | |
cellBodies<-as.integer(cell.bodies) | |
processes <- as.integer(processes) | |
sigmaR<-as.integer(sigma) | |
processLength<-as.integer(processLength) | |
areaSize<-alim | |
# initialize the filter parameter list. | |
if(is.null(filter)){ | |
noFilter<-1 | |
filter_minArea<-as.integer(0) | |
filter_maxArea<-as.integer(0) | |
filter_minThresh<-as.integer(0) | |
filter_maxThresh<-as.integer(0) | |
filter_eccentricity<-as.integer(0) | |
filter_eccentricity<-as.integer(0) | |
filter_alpha<-as.numeric(1.0) | |
filter_beta<-as.integer(0) | |
}else{ | |
noFilter<-0 | |
filter_minArea<-as.integer(filter$alim[1]) | |
filter_maxArea<-as.integer(filter$alim[2]) | |
filter_minThresh<-as.integer(filter$threshold.range[1]) | |
filter_maxThresh<-as.integer(filter$threshold.range[2]) | |
filter_eccentricity<-as.integer(filter$eccentricity) | |
filter_alpha<-as.numeric(filter$alpha) | |
filter_beta<-as.integer(filter$beta) | |
filter_alpha<-as.numeric(1.0) | |
filter_beta<-as.integer(0) | |
filter_Max<-as.numeric(filter$Max) | |
filter_Min<-as.integer(filter$Min) | |
} | |
## check for existence | |
if(!file.exists(file)) | |
stop(file, "not found") | |
create.output.directory('approximations_coefficents') | |
lapply(1:scales-1, function(x){create.output.directory( paste('d',x, sep='') ) } ) | |
create.output.directory('trace') | |
create.output.directory('coherency') | |
create.output.directory('orientation') | |
create.output.directory('scharr') | |
create.output.directory('cellbodies') | |
create.output.directory('processes_labeled') | |
create.output.directory('processes_anisotropic') | |
create.output.directory('output') | |
create.output.directory('blended') | |
create.output.directory('analyzed') | |
create.output.directory('rangecorrected') | |
## expand path | |
file <- path.expand(file) | |
############################# | |
# CALL # | |
############################# | |
a <- .Call("filterImage", file, | |
noFilter, | |
filter_minArea, | |
filter_maxArea, | |
filter_minThresh, | |
filter_maxThresh, | |
filter_eccentricity, | |
filter_alpha, | |
filter_beta, | |
filter_Max, | |
filter_Min, | |
scales, | |
cellBodies, | |
processes, | |
family, | |
sigmaR, | |
areaSize, | |
processLength, | |
outputfile) | |
cat("IMAGE PROCESSED\n") | |
############################# | |
# PLOT # | |
############################# | |
confocal.image<-readPNG(paste('output/d', cell.bodies, '_output_', output, '.png', sep='') ) | |
confocal.image<-as.raster(confocal.image[,,1:3]) | |
process.color<-rgb(a$process.r, a$process.g, a$process.b, 255, maxColorValue = 255) | |
if(illustrator){ | |
if(dim(confocal.image)[2]==dim(confocal.image)[1]){ | |
quartz(width=8, height=8) | |
par(xaxs='i', yaxs='i', pty='s') | |
}else{ | |
if(dim(confocal.image)[2]<dim(confocal.image)[1]){ | |
quartz(width=8*dim(confocal.image)[2]/dim(confocal.image)[1], height=8) | |
}else{ | |
quartz(width=8, height=8*dim(confocal.image)[1]/dim(confocal.image)[2]) | |
} | |
par(xaxs='i', yaxs='i', pty='m') | |
} | |
par(mar=c(0,0,0,0)) | |
plot(c(0,dim(confocal.image)[2]), c(0,dim(confocal.image)[1]), axes=F, ylab='', xlab='', col=0) | |
rasterImage(confocal.image,0,0,dim(confocal.image)[2],dim(confocal.image)[1]) | |
points(a$process.x, dim(confocal.image)[1] - a$process.y, col=process.color, pch=16, cex=0.35) #cex=0.15 | |
dev.copy(png, width = dim(confocal.image)[2], height = dim(confocal.image)[1], paste('analyzed/', output, '_illustrator.png', sep='')) | |
dev.off() | |
} | |
if(dim(confocal.image)[2]==dim(confocal.image)[1]){ | |
quartz(width=8, height=8) | |
par(xaxs='i', yaxs='i', pty='s') | |
}else{ | |
if(dim(confocal.image)[2]<dim(confocal.image)[1]){ | |
quartz(width=8*dim(confocal.image)[2]/dim(confocal.image)[1], height=8) | |
}else{ | |
quartz(width=8, height=8*dim(confocal.image)[1]/dim(confocal.image)[2]) | |
} | |
par(xaxs='i', yaxs='i', pty='m') | |
} | |
par(mar=c(0,0,0,0)) | |
plot(c(0,dim(confocal.image)[2]), c(0,dim(confocal.image)[1]), axes=F, ylab='', xlab='', col=0) | |
rasterImage(confocal.image,0,0,dim(confocal.image)[2],dim(confocal.image)[1]) | |
points(a$process.x, dim(confocal.image)[1] - a$process.y, col=process.color, pch=16, cex=0.15) | |
points(a$centroid.x[a$area>areaSize[1]], dim(confocal.image)[1] - a$centroid.y[a$area>areaSize[1]], bg=bg, pch=pch, cex=cex, lwd=lwd) | |
dev.copy(png, width = dim(confocal.image)[2], height = dim(confocal.image)[1], paste('analyzed/', output, '_analyzed.png', sep='')) | |
dev.off() | |
if(illustrator){ | |
confocal.image<-readPNG(paste('analyzed/', output, '_illustrator.png', sep='') ) | |
confocal.image<-as.raster(confocal.image[,,1:3]) | |
plot(c(0,dim(confocal.image)[2]), c(0,dim(confocal.image)[1]), axes=F, ylab='', xlab='', col=0) | |
rasterImage(confocal.image,0,0,dim(confocal.image)[2],dim(confocal.image)[1]) | |
points(a$centroid.x[a$area>areaSize[1]], dim(confocal.image)[1] - a$centroid.y[a$area>areaSize[1]], bg=bg, pch=pch, cex=cex*1.1, lwd=lwd) | |
dev.copy2pdf(file=paste('analyzed/', output, '_illustrator.pdf', sep='')) | |
} | |
dev.off() | |
a$process.type[which(a$process.type==1)]<-"slab" | |
a$process.type[which(a$process.type==2)]<-"end.point" | |
a$process.type[which(a$process.type==3)]<-"joint3" | |
a$process.type[which(a$process.type==4)]<-"joint4" | |
a$process.type[which(a$process.type==5)]<-"jointNA" | |
a$eccentricity[which(a$eccentricity==0)]<-NA #contours with less than 5 pixels cannot have eccenticity therefore lets put them to NA instead of 0. | |
#reorganize output | |
somas<-list(centroid.x=a$centroid.x, centroid.y=a$centroid.y, area=a$area, intensity=a$intensity, eccentricity=a$eccentricity, id=a$id, contour.x=a$x, contour.y=a$y) | |
processes<-list(id=a$process.id, type=a$process.type, orientation=a$process.theta, x=a$process.x, y=a$process.y, col= process.color, coherence= a$process.coherence) | |
b<-list(somas=somas, processes=processes) | |
return(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment