brain cells and tree cores – r image2

Post on 14-Feb-2017

111 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Brain Slices and Tree Cores – R Image Analysis

Darren DaviesDarren.Davies@swan.ac.uk

Stained Brain Slice

Example microscopic image of stained brain tissue slide.

Isolating Blue and Brown Regions

• i <- Brick(“Sample_1.tif”)

• names(i) <- c(“r”, “g”, “b”)

• Thresholding values:Blue <- (i$r < 125 & i$g < 150 & i$b > 10)*1 Brown <- (i$r > 0 & i$g > 0 & i$b < 165)*2

Blue Regions

Extracted Blue Regions Original Image

Brown Regions

Original ImageExtracted Brown Regions

Isolating Blue and Brown Patches

• i <- Brick(j)

• names(i) <- c(“r”, “g”, “b”)

• Thresholding values:Blue <- (i$r < 170 & i$g < 140 & i$b > 150)*1 Brown <- (i$r > 0 & i$g > 0 & i$b < 180)*2

• Combining two Raster Layers:Blue <- as.matrix(Blue)

Brown <- as.matrix(Brown)

Combined <- Blue+Brown Combined[Combined==1] <- 0 Combined[Combined==2] <- 0 Combined[Combined==3] <- 1 Combined <- raster(Combined)

Blue and Brown Regions

Extracted Blue Regions Extracted Blue + Brown Regions

Minimum Size Filter

Pixel <- 2500

Patches <- clump(Combined, directions = 8)

F <- as.data.frame(freq(Patches)) ExcludeID <- F$Value[which(F$Count < Pixel)]

PixelsRemoved<- Combined

PixelsRemoved[Combined %in% ExcludeID] <- NA

Value Count

1 1 1227

2 2 1

3 3 1

4 4 219

5 5 1

6 6 47

7 7 7275

Sample output of freq()

Size Filtered Image

Filtered Image Extracted Blue + Brown Regions

Output Image

Patch_Stats <- PatchStat(PixelsRemoved)

Total_Patches <- nrow(Patch_Stats)

extent(PixelsRemoved) <- extent(i)

plotRGB(i)

plot(PixelsRemoved, col="red", add=TRUE, legend=FALSE)

Example image output. Red regions represent counted patches

Batch ModeImage_Names <- as.data.frame(list.files())dir.create("ANALYSED_IMAGES")wd <- getwd()Output <- data.frame(matrix(nrow=0, ncol=2))

For (j in Image_Names[,1]){i <- brick(j)…Output [nrow(Output) + 1,] <- c(j , Total_Patches) Path <- file.path(wd,"ANALYSED_IMAGES", paste(j, ".pdf", sep=""))

pdf(Path) plotRGB(j)

plot(PixelsRemoved, col="red", add=TRUE, legend=FALSE) dev.off()}

Data_Path <- file.path(wd,"ANALYSED_IMAGES", paste("ANALYSED_DATA.txt", sep=""))write.table(Output, Data_Path, sep="\t")

Measuring Oak Vessel Area

Example scanned oak tree core, with infilled vessels and coloured surface.

Unaltered oak tree core surface.

Vessels filled with chalk.

Vessels filled with chalk and core surface coloured.

Identifying Vessels

Identified vessels (right; green) from a prepared oak core sample (left).

Identifying Ring Boundariesi <- brick("realvessels.tif")dflist <- list()plotRGB(i)

repeat {

Year<- readline(prompt = "What Year? ") coordinates <- data.frame(locator(, type = "o", col = "red")) dflist[[Year]] <- coordinates if (Year == "FINISH"){ break }}

User defined annual ring boundaries (red)

Sorting Measured VesselsPoly_list <- list()Vessel_year_List <- list()

for (j in 1:length(dflist)){if (j == length(dflist)) {

break} if (j < length(dflist)) { U_boundary <- as.data.frame(dflist[j]) L_boundary <- as.data.frame(dflist[(j+1)]) L_boundary <- L_boundary[order(-L_boundary[,1]),] C_boundary <- rbind(U_boundary, L_boundary) Combined <- rbind(C_boundary,U_boundary[1,]) Poly_list[[names(dflist[j])]] <- SpatialPolygons(list(Polygons(list(Polygon(Combined)), j))) Vessel_year_List[[names(dflist[j])]] <- list(extract(("realvessels.tif", poly_list[[j]], unique)) }}

Upper boundary

Lower boundary

1. 2.

3.4.

Plotting Vessel ID’s

Vessel_Num <- (i)

Clump_ID <- getValues(Vessel_Num)

xy <- xyFromCell(Vessel_Num, 1:ncell(Vessel_Num))

Vessel_ID <- data.frame(xy, Clump_ID, is_clump = Vessel_Num [] %in% freq(Vessel_Num, useNA = 'no')[,1])

Vessel_Coordinates <- sapply(split(Vessel_ID[, c("x", "y")], Vessel_ID$Clump_ID), colMeans)

text(Vessel_Coordinates, labels = row.names(Patch_Stats), col="red")

Output

Example data output

Example output image

References

Thresholding and counting images:http://stackoverflow.com/questions/32074992/using-multi-spec-and-r-for-batch-image-analysis

Size filter:http://gis.stackexchange.com/questions/130993/remove-clumps-of-pixels-in-r

Numbering cells:http://stackoverflow.com/questions/15632630/get-coordinates-of-a-patch-in-a-raster-map-raster-package-in-r

top related