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

使用R语言将32*32图像按8*8块转换为CSV的技术咨询

Solution to Split 32x32 Image into 8x8 Blocks for CSV Output

Got it, let's break this down into straightforward steps to get exactly what you need—16 rows of 64 features each, where each row represents an 8x8 pixel block from your original 32x32 image.

Step 1: Reshape Your Flat CSV Data Back into a 32x32 Matrix

First, we need to convert that single row of 1024 pixels back into the original 32x32 grid. Let's assume your existing CSV is loaded as a data frame named img_df (where the first row holds all the pixel values):

# Load your original CSV (if not already loaded)
img_df <- read.csv("your_original_image.csv", header = FALSE)

# Convert the flat row into a 32x32 matrix
# Use `byrow = TRUE` if your CSV pixels are ordered row-wise (top-left to bottom-right)
# Switch to `byrow = FALSE` if they're ordered column-wise
img_matrix <- matrix(unlist(img_df), nrow = 32, ncol = 32, byrow = TRUE)

Step 2: Split the Matrix into 8x8 Blocks

Next, we'll split the 32x32 matrix into 4x4 groups of 8x8 blocks (since 32/8 = 4, that gives us 16 total blocks). We'll use index splits to grab each block systematically:

# Create row and column indices split into chunks of 8
row_chunks <- split(1:32, ceiling(1:32 / 8))
col_chunks <- split(1:32, ceiling(1:32 / 8))

# Initialize a list to store each flattened block
block_list <- list()
counter <- 1

# Loop through each row chunk and column chunk to extract 8x8 blocks
for (rows in row_chunks) {
  for (cols in col_chunks) {
    # Extract the 8x8 block from the matrix
    current_block <- img_matrix[rows, cols]
    # Flatten the block into a 1x64 vector
    flattened_block <- as.vector(current_block)
    # Add to our list
    block_list[[counter]] <- flattened_block
    counter <- counter + 1
  }
}

Step 3: Convert Blocks to Data Frame and Save as CSV

Finally, we'll turn our list of flattened blocks into a data frame (16 rows, 64 columns) and write it to a CSV:

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

# Write to CSV (no row names to keep it clean)
write.csv(result_df, "8x8_image_blocks.csv", row.names = FALSE)

Bonus: If You're Working Directly with Images (Not Just CSV)

If you haven't already converted the image to CSV and want to do this directly from the image file, use the EBImage package (great for image processing in R):

# Install and load EBImage if you haven't already
# install.packages("EBImage")
library(EBImage)

# Read your image
img <- readImage("your_image.png")

# Convert to grayscale if it's a color image
if (colorMode(img) == Color) {
  img <- channel(img, "gray")
}

# Convert image to a 32x32 matrix (skip if your image is already 32x32)
img_matrix <- as.matrix(img)

# Follow Steps 2 and 3 above to split into blocks and save to CSV

A quick check: you should end up with a CSV where each row is one 8x8 block (64 pixels) and there are 16 rows total (4 blocks across, 4 blocks down in the original image).

Let me know if you hit any snags with the code!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:26:11