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

技术问询:在RStudio中生成视觉搜索实验刺激图片

用RStudio生成心理学视觉搜索实验刺激图片

核心思路

用Base R绘图系统替代ggplot,它能更精准控制每个刺激的位置、颜色和样式,完全适配实验需求。我们会实现:

  • 自定义网格位置的刺激布局
  • 控制目标刺激(红色"X")的位置不连续重复,支持随机不出现
  • 批量生成符合要求的刺激图片

完整代码实现

# 1. 定义基础参数
# 画布尺寸(像素,适配电脑屏幕)
canvas_width <- 1920
canvas_height <- 1080
# 刺激位置:生成5x5的网格(可根据需求修改行列数)
grid_rows <- 5
grid_cols <- 5
positions <- expand.grid(
  x = seq(0.1, 0.9, length.out = grid_cols) * canvas_width,
  y = seq(0.1, 0.9, length.out = grid_rows) * canvas_height
)
# 刺激类型映射:符号+颜色
stim_types <- list(
  black_x = list(symbol = "X", col = "black"),
  black_plus = list(symbol = "+", col = "black"),
  red_x = list(symbol = "X", col = "red"),
  red_plus = list(symbol = "+", col = "red")
)

# 2. 生成符合规则的刺激序列函数
generate_stim_sequence <- function(n_trials, target_prob = 0.7) {
  sequence <- list()
  last_target_pos <- NULL  # 记录上一次目标刺激的位置
  
  for (i in 1:n_trials) {
    # 随机决定是否出现目标刺激(红色X)
    has_target <- runif(1) < target_prob
    
    # 生成当前trial的刺激布局
    current_stim <- data.frame(
      x = positions$x,
      y = positions$y,
      symbol = sample(c("X", "+"), nrow(positions), replace = TRUE),
      col = "black"
    )
    
    if (has_target) {
      # 选择目标位置:排除上一次的位置
      available_pos <- if (!is.null(last_target_pos)) {
        setdiff(1:nrow(positions), last_target_pos)
      } else {
        1:nrow(positions)
      }
      target_pos <- sample(available_pos, 1)
      # 更新目标刺激的符号和颜色
      current_stim$symbol[target_pos] <- "X"
      current_stim$col[target_pos] <- "red"
      # 记录本次目标位置
      last_target_pos <- target_pos
    }
    
    sequence[[i]] <- current_stim
  }
  
  return(sequence)
}

# 3. 生成单张刺激图片的函数
draw_stim_image <- function(stim_data, save_path) {
  # 开启png绘图设备
  png(save_path, width = canvas_width, height = canvas_height, res = 96)
  # 设置绘图区域(去掉边距、坐标轴)
  par(mar = c(0, 0, 0, 0), xaxs = "i", yaxs = "i")
  plot(0, 0, type = "n", xlim = c(0, canvas_width), ylim = c(0, canvas_height),
       xaxt = "n", yaxt = "n", xlab = "", ylab = "", bty = "n")
  
  # 绘制每个刺激
  for (j in 1:nrow(stim_data)) {
    text(
      x = stim_data$x[j],
      y = stim_data$y[j],
      labels = stim_data$symbol[j],
      col = stim_data$col[j],
      cex = 5  # 刺激大小,可按需调整
    )
  }
  
  dev.off()
}

# 4. 批量生成示例
# 生成20个trial的刺激序列
stim_sequence <- generate_stim_sequence(n_trials = 20, target_prob = 0.8)
# 创建保存目录
dir.create("stimulus_images", showWarnings = FALSE)
# 批量保存图片
for (i in 1:length(stim_sequence)) {
  save_path <- file.path("stimulus_images", paste0("trial_", i, ".png"))
  draw_stim_image(stim_sequence[[i]], save_path)
}

代码说明

  • 参数自定义:可修改canvas_width/height适配实验屏幕,grid_rows/grid_cols调整刺激的网格密度,target_prob控制红色X出现的概率
  • 位置控制逻辑:generate_stim_sequence函数会记录上一次红色X的位置,下一次出现时自动排除该位置,确保不连续重复
  • 无干扰绘图:绘图时移除了所有坐标轴、边框,保证刺激画面干净,符合心理学实验要求
  • 批量生成:自动创建目录并保存所有trial的图片,可直接用于实验程序调用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:05:19