make_background: Computes background image from video.

Description Usage Arguments Examples

Description

make_backround

Usage

1
make_background(video, n = 10, type = "mean")

Arguments

video
n
type

Examples

 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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (video, n = 10, type = "mean") 
{
    frames <- seq(1, video$length(), length.out = n)
    if (type == "mean") {
        mat <- array(0, dim = c(video$dim(), 3))
        print("Loading images:")
        pb <- startpb(0, n - 1)
        for (i in 1:length(frames)) {
            mat <- mat + video$get_frame(frames[i])
            setpb(pb, i)
        }
        closepb(pb)
        print("Computing average image.")
        print("Done.")
        return(mat/n)
    }
    else if (type == "median") {
        mat <- array(0, dim = c(video$dim(), 3))
        mat.r <- array(NA, dim = c(video$dim(), n))
        mat.g <- array(NA, dim = c(video$dim(), n))
        mat.b <- array(NA, dim = c(video$dim(), n))
        print("Loading images:")
        pb <- startpb(0, n - 1)
        for (i in 1:length(frames)) {
            mat <- video$get_frame(frames[i])
            mat.r[, , i] <- mat[, , 1]
            mat.g[, , i] <- mat[, , 2]
            mat.b[, , i] <- mat[, , 3]
            setpb(pb, i)
        }
        closepb(pb)
        print("Computing median image. This is a slow process, please be patient.")
        print("   Median red:")
        mat[, , 1] <- pbapply(mat.r, c(1, 2), median.default)
        print("   Median green:")
        mat[, , 2] <- pbapply(mat.g, c(1, 2), median.default)
        print("   Median blue:")
        mat[, , 3] <- pbapply(mat.b, c(1, 2), median.default)
        print("Done.")
        return(mat)
    }
    else {
        stop("'type' should be 'mean' or 'median'")
    }
  }

swarm-lab/videotrackR documentation built on May 30, 2019, 9:37 p.m.