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

如何为256*256图像的16*16区块添加Bounding Box

Adding Bounding Boxes to 16x16 Image Blocks & Mapping Targets to Blocks

Hey there! I see you've already got the code to split your 256x256 grayscale image into 256 16x16 blocks. Now let's tackle adding bounding boxes to these blocks and figuring out which block a target belongs to. Here's a practical, step-by-step solution:

Step 1: Calculate Bounding Box Coordinates for Each Block

First, we need to map each 16x16 block to its corresponding bounding box in the original 256x256 image. Since the image is divided into a 16x16 grid (16 blocks wide, 16 blocks tall), each block's position can be translated to pixel coordinates easily.

For a block at row block_row (1 to 16) and column block_col (1 to 16):

  • Top-left corner (x1, y1): x1 = (block_col - 1) * 16 + 1, y1 = (block_row - 1) * 16 + 1
  • Bottom-right corner (x2, y2): x2 = block_col * 16, y2 = block_row * 16

Step 2: Integrate Bounding Boxes with Your Existing Block Data

Let's modify your code to store each block along with its bounding box info. We'll create a data frame that holds the block's flattened pixel values and its bounding box coordinates:

library(jpeg)
library(EBImage) # Assuming you're using EBImage for rgb_2gray and resize

filenames <- list.files("C:/Users/Desktop/imag", pattern = "*.jpeg", full.names = TRUE)
result_list <- list() # Use a list to store block data + bounding boxes

for (file_idx in 1:length(filenames)) {
  # Read and preprocess the image
  x <- readJPEG(filenames[file_idx])
  x <- rgb_2gray(x)
  x <- resize(x, w = 256, h = 256)
  
  # Split image into 16x16 blocks (your existing function)
  matsplitter <- function(M, r, c) {
    rg <- (row(M)-1)%/%r+1
    cg <- (col(M)-1)%/%c+1
    rci <- (rg-1)*max(cg) + cg
    N <- prod(dim(M))/r/c
    cv <- unlist(lapply(1:N, function(x) M[rci==x]))
    dim(cv) <- c(r,c,N)
    cv
  }
  
  asa <- matsplitter(x, 16, 16)
  
  # Calculate bounding boxes for each block and build result data
  num_blocks <- dim(asa)[3]
  for (block_idx in 1:num_blocks) {
    # Convert linear block index to row/column in the 16x16 grid
    block_col <- ((block_idx - 1) %% 16) + 1
    block_row <- ((block_idx - 1) %/% 16) + 1
    
    # Compute bounding box coordinates
    x1 <- (block_col - 1) * 16 + 1
    y1 <- (block_row - 1) * 16 + 1
    x2 <- block_col * 16
    y2 <- block_row * 16
    
    # Flatten the block pixels (your existing logic)
    block_pixels <- as.vector(t(asa[,,block_idx]))
    
    # Store everything in a data frame row
    block_data <- data.frame(
      file_name = basename(filenames[file_idx]),
      block_index = block_idx,
      block_row = block_row,
      block_col = block_col,
      bbox_x1 = x1,
      bbox_y1 = y1,
      bbox_x2 = x2,
      bbox_y2 = y2
    )
    
    # Add pixel columns (we'll name them pixel_1 to pixel_256)
    pixel_cols <- data.frame(t(block_pixels))
    colnames(pixel_cols) <- paste0("pixel_", 1:256)
    
    # Combine bounding box info with pixel data
    block_data <- cbind(block_data, pixel_cols)
    
    # Add to result list
    result_list[[length(result_list)+1]] <- block_data
  }
}

# Combine all blocks into a single data frame
final_result <- do.call(rbind, result_list)

Step 3: Map a Target to Its Corresponding Block

Suppose you have a target with coordinates (e.g., the center point target_x, target_y or its own bounding box). To find which block contains this target:

# Example: Target center at (100, 150)
target_x <- 100
target_y <- 150

# Calculate which block column/row the target falls into
target_block_col <- floor((target_x - 1)/16) + 1
target_block_row <- floor((target_y - 1)/16) + 1

# Find the block index (if needed)
target_block_idx <- ((target_block_row - 1)*16) + target_block_col

# Get the block's bounding box and pixel data from final_result
target_block_data <- final_result[final_result$block_index == target_block_idx, ]

Quick Notes:

  • If your target has its own bounding box (e.g., target_x1, target_y1, target_x2, target_y2), you can check overlap with each block's bounding box, or use the target's center to find the primary block.
  • Double-check your image coordinate system: some libraries count y from the top (like most image viewers), while matrix rows count from the bottom. If needed, adjust y1/y2 by reversing the row calculation (e.g., block_row = 17 - ((block_idx - 1) %/% 16) + 1).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:49:44