由于图像处理的范围非常广泛,以下只提供一个简单的示例代码,用于将一张 BMP 格式的图片转换成灰度图。

首先,需要引入相关头文件:

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

#pragma pack(1)

typedef struct {
    char header[2];
    unsigned int size;
    unsigned int reserved;
    unsigned int offset;
} BMP_HEADER;

typedef struct {
    unsigned int size;
    int width;
    int height;
    unsigned short planes;
    unsigned short bpp;
    unsigned int compression;
    unsigned int image_size;
    int x_ppm;
    int y_ppm;
    unsigned int clr_used;
    unsigned int clr_important;
} BMP_INFO_HEADER;

#pragma pack()

其中,BMP_HEADERBMP_INFO_HEADER 分别表示 BMP 文件头和图像信息头。

然后,定义一个函数用于将彩色图像转换成灰度图像:

void rgb2gray(unsigned char* rgb, unsigned char* gray, int width, int height) {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int idx = i * width + j;
            int r = rgb[3 * idx];
            int g = rgb[3 * idx + 1];
            int b = rgb[3 * idx + 2];
            gray[idx] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b);
        }
    }
}

最后,编写主函数读取图片文件并进行处理:

int main(int argc, char** argv) {
    if (argc < 3) {
        printf("Usage: %s input_file output_file\n", argv[0]);
        return 0;
    }

    char* input_file = argv[1];
    char* output_file = argv[2];

    FILE* fp = fopen(input_file, "rb");
    if (fp == NULL) {
        printf("Failed to open input file: %s\n", input_file);
        return -1;
    }

    BMP_HEADER bmp_header;
    BMP_INFO_HEADER bmp_info_header;

    fread(&bmp_header, sizeof(BMP_HEADER), 1, fp);
    fread(&bmp_info_header, sizeof(BMP_INFO_HEADER), 1, fp);

    if (bmp_header.header[0] != 'B' || bmp_header.header[1] != 'M') {
        printf("Invalid BMP file: %s\n", input_file);
        return -1;
    }

    if (bmp_info_header.bpp != 24) {
        printf("Unsupported color depth: %d\n", bmp_info_header.bpp);
        return -1;
    }

    int width = bmp_info_header.width;
    int height = bmp_info_header.height;

    unsigned char* rgb_data = malloc(width * height * 3);
    unsigned char* gray_data = malloc(width * height);

    fread(rgb_data, sizeof(unsigned char), width * height * 3, fp);
    fclose(fp);

    rgb2gray(rgb_data, gray_data, width, height);

    fp = fopen(output_file, "wb");
    if (fp == NULL) {
        printf("Failed to open output file: %s\n", output_file);
        return -1;
    }

    fwrite(&bmp_header, sizeof(BMP_HEADER), 1, fp);
    fwrite(&bmp_info_header, sizeof(BMP_INFO_HEADER), 1, fp);
    fwrite(gray_data, sizeof(unsigned char), width * height, fp);
    fclose(fp);

    free(rgb_data);
    free(gray_data);

    return 0;
}

最后,使用 CMakeLists.txt 文件来构建项目:

cmake_minimum_required(VERSION 3.10)

project(image-processing C)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_STANDARD 11)

add_executable(image-processing main.c)

target_link_libraries(image-processing m)

其中,add_executable 指定了编译的可执行文件名为 image-processingtarget_link_libraries 则指定了需要链接的库,这里用到了 m 库,用于调用数学函数。


原文地址: https://www.cveoy.top/t/topic/CUs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录