Rstudio Crashes whiles running imaging codes

I have to count and differentiate viable cells from dead cells in an image. I tried to clean the image with the following code and RStudio crashes. Anytime I run the code, I am prompted to start a new session.
Has anyone encounter similar problem?

cols <- rainbow(3, alpha=0.3)[2] # Initialize the green color
pred <- ifelse(mask1, cols, "#00000000")

fixed.pred <- fillHull(mask1)
fixed.pred <- bwlabel(fixed.pred)
temp.rm <- names(which(table(fixed.pred)<2000))
fixed.pred <- rmObjects(fixed.pred, temp.rm)

fixed.pred2 <- ifelse(fixed.pred, cols, "#00000000")

squares.x <- c(4, 4, 4, 5, 4, 5)-1
squares.y <- c(2, 3, 6, 6, 7, 7)-1

mask2 <- matrix("#00000000", dim(pic1)[1], dim(pic1)[2])

for(i in 1:length(squares.x)){
mask2[(squares.y[i]*240+24):(squares.y[i]*240+240+24), (squares.x[i]*240+258):(squares.x[i]*240 + 240 + 258)] <- cols
}

disjoint.grid.x <- seq(258, 1698, by=240)/dim(pic1)[2]+1
disjoint.grid.y <- seq(24, 2184, by=240)/dim(pic1)[1]+1

It would be easier to help with a minimal reproducible example (reprex). See here for instructions.

Building a reprex using the "nuclei.tif" example file provided by EBImage, which is a stack with 4 frames, I get the same problem as you at the rmObjects() step. Since this image has 4 frames, this error is solved by having temp.rm be a list of length 4.

library(EBImage)

x <- readImage(system.file('images', 'nuclei.tif', package='EBImage'))
mask1 <- thresh(x, 10, 10, 0.05)

cols <- rainbow(3, alpha=0.3)[2] # Initialize the green color
pred <- ifelse(mask1, cols, "#00000000")

fixed.pred <- fillHull(mask1)
fixed.pred <- bwlabel(fixed.pred)
temp.rm <- replicate(4,names(which(table(fixed.pred)<200)), simplify = FALSE)
fixed.pred <- rmObjects(fixed.pred, temp.rm)

fixed.pred2 <- ifelse(fixed.pred, cols, "#00000000")

Created on 2022-04-17 by the reprex package (v2.0.1)

If your mask1 is also a stack, this is a likely explanation.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.