【发布时间】:2014-12-28 21:02:27
【问题描述】:
我正在创建一个程序,它可以读取 bmp 图像并计算其上红色、蓝色和绿色的百分比。查了很多,但不明白图像数据究竟是从哪个字节开始的,如何获取像素的RGB值?
#include<stdio.h>
typedef struct {
unsigned int fileSize;
unsigned int offset;
unsigned int reserved;
char signature[2];
} BmpHeader;
typedef struct {
unsigned short bitDepth;
unsigned int compressedImageSize;
unsigned int compression;
unsigned int headerSize;
unsigned int height;
unsigned int horizontalResolution;
unsigned int importantColors;
unsigned int numColors;
unsigned short planeCount;
unsigned int verticalResolution;
unsigned int width;
} BmpImageInfo;
typedef struct {
unsigned char blue;
unsigned char green;
unsigned char red;
} Rgb;
int main(void) {
BmpHeader header;
BmpImageInfo info;
char filename[40];
printf("Enter file name : ");scanf("%s", filename);
FILE *fp;
fp = fopen(filename, "rb");
fread(&header, 1, sizeof(BmpHeader), fp);
fread(&info, 1, sizeof(BmpImageInfo), fp);
printf("%u", info.height);
getchar();
return 0;
}
为什么我的高度不对???
【问题讨论】:
-
考虑使用 OpenCV
-
能否提供其C接口的链接
-
它的 C 接口被弃用,取而代之的是它的 C++ 接口。它的 C++ 接口需要对 C++ 的最少了解(我今年秋天通过 OpenCV 学习了 C++)。基本上,您只需使用
imread读取图像以从文件创建 Mat 对象,然后使用at方法访问 rgb 值。 StackOverflow 上有多个关于此的帖子。 -
有些人会认为这是这样做的一个重大缺点。说“我可以让它工作,但不明白它是如何做到的......”并不是一种荣誉徽章。
-
查看 paulbourke.net/dataformats/bmp/BITMAP.C 和 paulbourke.net/dataformats/bmp/BITMAP.H — 特别是
RGBQUAD结构。
标签: c