不适用opencv用C语言读取100张bmp图片的代码
时间: 2023-05-23 14:02:00 浏览: 109
以下是使用 C 语言读取 100 张 bmp 图片的基本代码:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
for (int i = 1; i <= 100; i++) {
char filename[20];
sprintf(filename, "image_%d.bmp", i);
FILE* file = fopen(filename, "rb");
if (file == NULL) {
printf("Error: Cannot open file %s\n", filename);
continue;
}
// Read header (14 bytes)
unsigned char header[14];
fread(header, sizeof(unsigned char), 14, file);
// Read DIB header (40 bytes)
unsigned char dib_header[40];
fread(dib_header, sizeof(unsigned char), 40, file);
// Read pixel data
int width = *(int*)&dib_header[4];
int height = *(int*)&dib_header[8];
int bit_count = *(short*)&dib_header[14];
int row_size = ((width * bit_count + 31) / 32) * 4;
int data_size = row_size * height;
unsigned char* data = (unsigned char*)malloc(data_size);
fread(data, sizeof(unsigned char), data_size, file);
// Processing image data ...
free(data);
fclose(file);
}
return 0;
}
```
这段代码可以循环读取名为 `image_1.bmp` 到 `image_100.bmp` 的图片文件。每次读取过程需要读取 BMP 文件的头信息和像素数据,后续的处理部分可以根据需要进行修改。如果文件不存在或者读取错误,会跳过当前文件继续循环。
阅读全文