I have the following R code where I am generating two types of images. One with the blue circle in the middle with some white polygons inside the circle, with different opacities, and the black and white one where the polygons are of exact same shape and size as the colored image, but it’s just in black and white.
My goal is to utilize these images to train an U-Net model (which I don’t know anything about, but trying to learn). Then if I input a colored image it will be able to identify the white polygons in the image.
Issue is the plaques in black and white images seem to be larger than the colored images. The shapes are similar. I want the white plaques in the same area in the B&W image as in the colored image. I am assuming if I am trying to use the ‘masked’ file it would need to be similar co-ordinates.
Also, I see some of the polygons appearing in the black and white images where as they are not showing up on the colored image. I tried having different opacities for the polygons, I am assuming some of them have 0% opacity some how. Is there a way to have regulate that between 10% to 100%?
num.cores <- detectCores() - 1
registerDoParallel(cores = num.cores)
num.sample <- 10 # Number of images to generate
min_plaque.count <- 5
max_plaque.count <- 20
foreach(i = 1:num.sample, .packages = c("plotrix", "grDevices")) %dopar% {
num.spots <- sample(min_plaque.count:max_plaque.count, 1)
file_name <- paste0("./data_raw/image_id_", i, "_", num.spots, ".png")
mask_file_name <- paste0("./data_mask/mask_id_", i, "_", num.spots, ".png")
polygons <- list()
blue_start <- rgb(0, 0, runif(1, 0.4, 1))
blue_end <- rgb(0, 0, runif(1, 0.4, 1))
gradient_col <- colorRampPalette(c(blue_start, blue_end))(100)
num_faded <- sample(num.spots, 1) # Number of spots to have fading effect, randomly chosen between 3 and 10
fade_indices <- sample(num.spots, num_faded) # Randomly choose which spots will fade
# Generating the raw image and mask image within the same loop
png(file_name, width = 1024, height = 768, res = 200, type = "cairo-png")
plot(0:10, 0:10, type = "n", asp = 1, xlab = "", ylab = "", xaxt = 'n', yaxt = 'n', bty = 'n')
draw.circle(5, 5, 5, col = gradient_col, border = NA)
for (k in 1:num.spots) {
plot <- 0
while (plot == 0) {
x <- runif(1, 0, 10)
y <- runif(1, 0, 10)
d <- runif(1, 0.1, 0.3) # diameter of the spot
if ((x + d - 5)^2 + (y + d - 5)^2 < 4.5^2) { # Check if inside the larger circle
plot <- 1
}
}
# Create random vertices for irregular shape
vertices <- 10
angles <- sort(runif(vertices, 0, 2 * pi))
radii <- rnorm(vertices, mean = d, sd = 0.03)
x_vertices <- x + radii * cos(angles)
y_vertices <- y + radii * sin(angles)
polygons[[k]] <- list(x = x_vertices, y = y_vertices)
# Applying fading effect conditionally
if (k %in% fade_indices) {
distances <- sqrt((x_vertices - x)^2 + (y_vertices - y)^2)
fade_factors <- 1 - distances / max(distances) # normalize and invert to fade from center to edge
col <- rgb(1, 1, 1, fade_factors) # apply fade based on distance
} else {
col <- "white"
}
polygon(x_vertices, y_vertices, col = col, border = NA)
}
dev.off()
# Generate the mask image
png(mask_file_name, width = 1024, height = 768, res = 200, type = "cairo-png")
par(mar = c(0, 0, 0, 0), bg = "black") # Set background to black
plot(0:10, 0:10, type = "n", asp = 1, xlab = "", ylab = "", xaxt = 'n', yaxt = 'n', bty = 'n', col.axis = "white", col.lab = "white", col.main = "white", col.sub = "white")
for (k in 1:num.spots) {
polygon(polygons[[k]]$x, polygons[[k]]$y, col = "white", border = NA)
}
dev.off()
}
stopImplicitCluster()
One example of an output images are:
1