18
Brain Slices and Tree Cores – R Image Analysis Darren Davies [email protected]

Brain Cells and Tree Cores – R Image2

Embed Size (px)

Citation preview

Page 1: Brain Cells and Tree Cores – R Image2

Brain Slices and Tree Cores – R Image Analysis

Darren [email protected]

Page 2: Brain Cells and Tree Cores – R Image2

Stained Brain Slice

Example microscopic image of stained brain tissue slide.

Page 3: Brain Cells and Tree Cores – R Image2

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

Page 4: Brain Cells and Tree Cores – R Image2

Blue Regions

Extracted Blue Regions Original Image

Page 5: Brain Cells and Tree Cores – R Image2

Brown Regions

Original ImageExtracted Brown Regions

Page 6: Brain Cells and Tree Cores – R Image2

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)

Page 7: Brain Cells and Tree Cores – R Image2

Blue and Brown Regions

Extracted Blue Regions Extracted Blue + Brown Regions

Page 8: Brain Cells and Tree Cores – R Image2

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()

Page 9: Brain Cells and Tree Cores – R Image2

Size Filtered Image

Filtered Image Extracted Blue + Brown Regions

Page 10: Brain Cells and Tree Cores – R Image2

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

Page 11: Brain Cells and Tree Cores – R Image2

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")

Page 12: Brain Cells and Tree Cores – R Image2

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.

Page 13: Brain Cells and Tree Cores – R Image2

Identifying Vessels

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

Page 14: Brain Cells and Tree Cores – R Image2

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)

Page 15: Brain Cells and Tree Cores – R Image2

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.

Page 16: Brain Cells and Tree Cores – R Image2

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")

Page 17: Brain Cells and Tree Cores – R Image2

Output

Example data output

Example output image

Page 18: Brain Cells and Tree Cores – R Image2

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