You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R生成U-Net训练图像:掩膜匹配及透明度调控问题

U-Net训练图像生成:匹配性与透明度修复

问题说明

  • 黑白掩膜图中的斑块与彩色图中位置、尺寸不匹配
  • 部分多边形因透明度为0在彩色图中完全不可见,需将透明度限制在10%-100%区间

解决思路与代码修改

问题1修复:统一绘图区域参数

彩色图生成时未设置边距,而掩膜图设置了par(mar = c(0,0,0,0)),导致两者绘图区域不一致。需在彩色图绘图前添加相同的边距设置,确保坐标系完全对齐。

问题2修复:限制透明度范围

原代码中fade_factors最小值为0,直接导致边缘透明度为0。通过将fade_factors线性映射到0.1-1区间,保证所有多边形至少有10%的透明度,同时保留中心到边缘的渐变效果。

修改后的完整代码

num.cores <- detectCores() - 1
registerDoParallel(cores = num.cores)

num.sample <- 10  # 生成图像数量
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)  # 随机选择带渐变效果的斑块数量
  fade_indices <- sample(num.spots, num_faded)  # 选定哪些斑块应用渐变
  
  # 生成彩色图
  png(file_name, width = 1024, height = 768, res = 200, type = "cairo-png")
  par(mar = c(0, 0, 0, 0))  # 统一边距设置,与掩膜图一致
  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)  # 斑块直径
      if ((x + d - 5)^2 + (y + d - 5)^2 < 4.5^2) {  # 检查是否在蓝色圆形内
        plot <- 1
      }
    }
    # 生成不规则多边形顶点
    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)
    
    # 应用渐变效果,限制透明度在10%-100%
    if (k %in% fade_indices) {
      distances <- sqrt((x_vertices - x)^2 + (y_vertices - y)^2)
      # 将渐变因子映射到0.1-1区间
      fade_factors <- 0.1 + 0.9 * (1 - distances / max(distances))
      col <- rgb(1, 1, 1, fade_factors)
    } else {
      col <- "white"
    }
    
    polygon(x_vertices, y_vertices, col = col, border = NA)
  }
  
  dev.off()
  
  # 生成掩膜图
  png(mask_file_name, width = 1024, height = 768, res = 200, type = "cairo-png")
  par(mar = c(0, 0, 0, 0), bg = "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()

示例图像

彩色示例图
掩膜示例图

内容的提问来源于stack exchange,提问作者Arnoneel Sinha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:24:52