find.roi: ROI identification through thresholding and segmentation

Description Usage Arguments Details Value See Also Examples

View source: R/find.R

Description

Finds regions of interests (ROIs) within images according to search parameters by thresholding, binarization, and segmenting connected regions. Returns contour coordinates of ROI(s) and position and size descriptors.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
find.roi(
  img = NULL,
  thr = "otsu",
  search.for = "largest",
  size.min = 0.01,
  bg = "white",
  edges = FALSE,
  border = 5,
  results = FALSE
)

Arguments

img

character, the image file path or vector of paths.

thr

numeric or character ('otsu'); the threshold to determine binary image. See Details.

search.for

character, the search parameter. See Details.

size.min

numeric, indicating the minimum size of ROIs as a proportion of the pixel field to be considered in analysis. May be useful if smaller unimportant ROIs appear in the frame(s). Default is 0.01.

bg

character, the background color of the binary images (either 'white' or 'black'). Ignored if results=FALSE.

edges

logical, should ROIs on image edges be evaluated. See Details.

border

integer, if edges=TRUE, size of border to add in pixels. Otherwise ignored. Dee details.

results

logical, should a binary image be printed to the graphics device. Value is forced to FALSE if the length of 'img' is >1.

Details

Thresholding operations can be performed with an arbitrary (user defined, 0-1) numeric value or with Otsu's method ('thr="otsu"'). The latter chooses a threshold value by minimizing the combined intra-class variance. See otsu.

search.for determines how ROIs are chosen:

These choices will be made on ROI sets that are not on the edge of the field if 'edges=FALSE'.

edges is set by default to 'FALSE'. It is not advisable to include shapes that are on the edge of any frame and are therefore incomplete. Yet, if set to 'TRUE', the border adds a border of the background color to the image so that the intended ROI may be distinguished from the edge.

Value

A named list or list of lists containing the following:

best a data table consisting x,y coordinates of the best contour according to 'search.for' best.class the name the best ROI found in the image(s)

classes a data table containing the following for all ROIs detected:

dim the x and y dimensions of the image(s) analyzed.

See Also

kin.search, kin.simple,kin.free, bwlabel

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
library(data.table)
#acces example video of a sunfish swimming and establish directories

v <-system.file("extdata/vid", "sunfish_BCF.avi", package = "trackter")

dir.create(paste0(tempdir(),"/images"))
dir.create(paste0(tempdir(),"/out"))

#extract images
vid.to.images(v, out.dir = paste0(tempdir(),"/images"))

#find ROIs and extract contours

f <- list.files(paste0(tempdir(),"/images"),full.names=TRUE)
rois <- find.roi(img=f,search.for="largest",size.min=0.10,thr=0.7)

#see results of a few

conts <- do.call(rbind,lapply(rois[1:10], function(x) data.table(img=x$img,x$best)))

library(ggplot2)
qplot(data=conts,x,y,col=img)

#clean up
unlink(paste0(tempdir(),"/images"),recursive=TRUE)
unlink(paste0(tempdir(),"/out"),recursive=TRUE)

ckenaley/trackter documentation built on Feb. 11, 2022, 6:43 a.m.