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

如何从文件读取3D整数数组并转存为1D数组实现图像重映射

问题背景

需要处理一个720×1280规格的3D像素坐标数组(存储在.txt文件中,每组坐标首数为x、次为y),目标是:

  • 读取该文本文件,将坐标整数转存为1D数组
  • 用这个1D坐标数组对同为1D数组格式的720×1280输入图像执行重映射操作,同时提供图像旋转的示例代码
已完成的解决步骤及代码实现

预处理操作

先手动或通过脚本移除输入.txt文件中的所有括号,让所有坐标数据以空格作为分隔符,确保后续C代码能正常读取。

C语言核心实现代码

1. 读取坐标文件并转存为1D数组、计算重映射位置并保存

#include <stdio.h>
#include <stdlib.h>

#define WIDTH 720
#define HEIGHT 1280
#define TOTAL_PIXELS (WIDTH * HEIGHT)

int main() {
    // 读取处理后的坐标文件
    FILE *coord_file = fopen("coordinates_processed.txt", "r");
    if (!coord_file) {
        perror("Failed to open coordinate file");
        return 1;
    }

    // 分配内存存储x、y坐标(每个像素占2个int)
    int *remap_coords = (int *)malloc(2 * TOTAL_PIXELS * sizeof(int));
    if (!remap_coords) {
        perror("Memory allocation failed");
        fclose(coord_file);
        return 1;
    }

    // 读取坐标数据
    for (int i = 0; i < 2 * TOTAL_PIXELS; i++) {
        if (fscanf(coord_file, "%d", &remap_coords[i]) != 1) {
            perror("Failed to read coordinate data");
            free(remap_coords);
            fclose(coord_file);
            return 1;
        }
    }
    fclose(coord_file);

    // 读取输入1D图像(假设为灰度图,单字节像素)
    FILE *input_img = fopen("input_image_1d.bin", "rb");
    if (!input_img) {
        perror("Failed to open input image");
        free(remap_coords);
        return 1;
    }

    unsigned char *input_data = (unsigned char *)malloc(TOTAL_PIXELS * sizeof(unsigned char));
    unsigned char *output_data = (unsigned char *)malloc(TOTAL_PIXELS * sizeof(unsigned char));
    if (!input_data || !output_data) {
        perror("Image memory allocation failed");
        free(remap_coords);
        fclose(input_img);
        return 1;
    }

    fread(input_data, sizeof(unsigned char), TOTAL_PIXELS, input_img);
    fclose(input_img);

    // 执行重映射操作
    for (int i = 0; i < TOTAL_PIXELS; i++) {
        int x = remap_coords[2*i];
        int y = remap_coords[2*i + 1];
        // 坐标有效性校验
        if (x >= 0 && x < WIDTH && y >=0 && y < HEIGHT) {
            int src_idx = y * WIDTH + x;
            output_data[i] = input_data[src_idx];
        } else {
            // 超出范围的像素设为黑色
            output_data[i] = 0;
        }
    }

    // 保存重映射后的图像
    FILE *output_img = fopen("remapped_image_1d.bin", "wb");
    if (!output_img) {
        perror("Failed to open output image file");
        free(remap_coords);
        free(input_data);
        free(output_data);
        return 1;
    }
    fwrite(output_data, sizeof(unsigned char), TOTAL_PIXELS, output_img);
    fclose(output_img);

    // 保存1D坐标数组到文件
    FILE *saved_coords = fopen("remap_coords_1d.txt", "w");
    if (!saved_coords) {
        perror("Failed to save coordinate array");
        free(remap_coords);
        free(input_data);
        free(output_data);
        return 1;
    }
    for (int i = 0; i < 2 * TOTAL_PIXELS; i++) {
        fprintf(saved_coords, "%d ", remap_coords[i]);
    }
    fclose(saved_coords);

    // 释放内存
    free(remap_coords);
    free(input_data);
    free(output_data);

    printf("Remapping completed successfully.\n");
    return 0;
}

2. 图像旋转重映射示例代码

如果需要实现图像顺时针旋转90度的重映射逻辑,可以用以下代码替换上述读取坐标文件的部分,直接生成旋转坐标数组:

// 生成顺时针旋转90度的重映射坐标
for (int y = 0; y < HEIGHT; y++) {
    for (int x = 0; x < WIDTH; x++) {
        int idx = y * WIDTH + x;
        // 顺时针旋转90度的坐标转换公式
        int new_x = HEIGHT - 1 - y;
        int new_y = x;
        remap_coords[2*idx] = new_x;
        remap_coords[2*idx + 1] = new_y;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 13:10:26