-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
78 lines (56 loc) · 1.88 KB
/
server.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
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(imager)
library(dplyr)
library(png)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
im <- load.image("./coins.png")
output$preImage <- renderImage({
# Return a list containing the filename and alt text
imOut <- t(im[,,1,1])
outfile <- tempfile(fileext = ".png")
writePNG(imOut, target = outfile)
list(src = outfile,
contentType = "image/png",
alt = "This is alternate text")
}, deleteFile = FALSE)
output$thresh <- renderPlot({
d <- as.data.frame(im)
##Subsamble, fit a linear model
m <- sample_n(d,input$samples) %>% lm(value ~ x*y,data=.)
##Correct by removing the trend
im.c <- im-predict(m,d)
out <- threshold(im.c)
out <- clean(out,3) %>% imager::fill(7)
plot(im,main="Thresholding",axes=FALSE)
highlight(out)
})
output$water <- renderPlot({
d <- as.data.frame(im)
##Subsamble, fit a linear model
m <- sample_n(d,input$samples) %>% lm(value ~ x*y,data=.)
##Correct by removing the trend
im.c <- im-predict(m,d)
bgPercent <- paste(toString(input$bgValue),"%",sep="")
fgPercent <- paste(toString(100-input$bgValue),"%",sep="")
bg <- (!threshold(im.c,bgPercent))
fg <- (threshold(im.c,fgPercent))
#Build a seed image where fg pixels have value 2, bg 1, and the rest are 0
seed <- bg+2*fg
edges <- imgradient(im,"xy") %>% enorm
p <- 1/(1+edges)
ws <- (watershed(seed,p)==1)
ws <- bucketfill(ws,1,1,color=2) %>% {!( . == 2) }
plot(im,main="Watershed",axes=FALSE)
out2 <- clean(ws,5)
highlight(out2,col="green")
})
})