C 语言实现 NV12 图片拼接(水平/垂直)
C 语言实现 NV12 图片拼接(水平/垂直)
本示例展示如何使用 C 语言将多个 NV12 格式的图片进行水平或垂直拼接。
代码示例
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 640
#define HEIGHT 480
void main(int argc, char *argv[]) {
if (argc < 4) {
printf("Usage: %s <mode> <output> <input1> <input2> ...\n", argv[0]);
printf("mode: 0 for horizontal, 1 for vertical\n");
return;
}
int mode = atoi(argv[1]);
char *output = argv[2];
int num_inputs = argc - 3;
FILE *fout = fopen(output, "wb");
if (!fout) {
printf("Failed to open output file %s\n", output);
return;
}
int output_width, output_height;
if (mode == 0) {
output_width = WIDTH * num_inputs;
output_height = HEIGHT;
} else {
output_width = WIDTH;
output_height = HEIGHT * num_inputs;
}
// Write header
fprintf(fout, "NV12\n");
fprintf(fout, "%d %d\n", output_width, output_height);
// Allocate memory for output buffer
int output_size = output_width * output_height * 3 / 2;
unsigned char *output_buffer = (unsigned char *) malloc(output_size);
// Loop through input files
for (int i = 0; i < num_inputs; i++) {
char *input = argv[i+3];
FILE *fin = fopen(input, "rb");
if (!fin) {
printf("Failed to open input file %s\n", input);
return;
}
// Read header
char header[5];
int width, height;
fscanf(fin, "%s\n%d %d\n", header, &width, &height);
if (width != WIDTH || height != HEIGHT) {
printf("Input file dimensions do not match (%s)\n", input);
return;
}
// Allocate memory for input buffer
int input_size = width * height * 3 / 2;
unsigned char *input_buffer = (unsigned char *) malloc(input_size);
// Read input data
fread(input_buffer, sizeof(unsigned char), input_size, fin);
// Close input file
fclose(fin);
// Copy input data to output buffer
if (mode == 0) {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int input_offset = y * WIDTH + x;
int output_offset = y * output_width + i * WIDTH + x;
output_buffer[output_offset] = input_buffer[input_offset];
}
}
for (int y = 0; y < HEIGHT / 2; y++) {
for (int x = 0; x < WIDTH; x += 2) {
int input_offset = HEIGHT * WIDTH + y * WIDTH + x;
int output_offset = HEIGHT * output_width / 2 + i * WIDTH / 2 + y * output_width / 2 + x;
output_buffer[output_offset] = input_buffer[input_offset];
output_buffer[output_offset + 1] = input_buffer[input_offset + 1];
}
}
} else {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int input_offset = y * WIDTH + x;
int output_offset = i * HEIGHT * output_width + y * output_width + x;
output_buffer[output_offset] = input_buffer[input_offset];
}
}
for (int y = 0; y < HEIGHT / 2; y++) {
for (int x = 0; x < WIDTH; x += 2) {
int input_offset = HEIGHT * WIDTH + y * WIDTH + x;
int output_offset = i * HEIGHT * output_width / 2 + HEIGHT * output_width + y * output_width / 2 + x;
output_buffer[output_offset] = input_buffer[input_offset];
output_buffer[output_offset + 1] = input_buffer[input_offset + 1];
}
}
}
// Free input buffer
free(input_buffer);
}
// Write output data
fwrite(output_buffer, sizeof(unsigned char), output_size, fout);
// Free output buffer
free(output_buffer);
// Close output file
fclose(fout);
}
使用方法
该程序的输入格式为:<mode> <output> <input1> <input2> ...,其中:
mode表示拼接模式,0 表示水平方向拼接,1 表示垂直方向拼接。output表示输出文件的路径。input1、input2等表示要拼接的输入文件的路径。
说明
- 程序首先读取所有的输入文件,检查它们的尺寸是否都一致,并根据拼接模式计算输出图像的尺寸。
- 然后,程序分别从每个输入文件中读取数据,并将它们拷贝到输出缓冲区中。
- 最后,程序将输出缓冲区中的数据写入输出文件中,并清理所有的内存和文件句柄。
注意
- 该程序中假设输入图像的尺寸固定为 640x480,输出图像的格式为 NV12。如果需要处理其他格式或尺寸的图像,需要修改程序中的相应部分。
- 为了方便理解,代码中使用了
WIDTH和HEIGHT宏定义,实际使用中可以根据需要进行修改。 - 为了提高代码可读性,代码中添加了注释,并使用了缩进格式。
- 程序中使用
malloc()和free()函数来动态分配和释放内存。 - 程序中使用
fopen()和fclose()函数来打开和关闭文件。 - 程序中使用
fwrite()函数来写入数据到文件。 - 程序中使用
fscanf()函数来读取数据从文件。 - 程序中使用
atoi()函数来将字符串转换为整数。
总结
本示例展示了如何使用 C 语言将多个 NV12 格式的图片进行水平或垂直拼接,并提供了详细的代码示例和说明。希望本示例能帮助你更好地理解 C 语言图像处理的基本原理。
原文地址: https://www.cveoy.top/t/topic/lGOh 著作权归作者所有。请勿转载和采集!