我有一个阅读bmp图像的问题。如何获取bmp图像中的像素值(R、G、B值)?有人能帮助我使用C编程语言吗?
发布于 2009-12-28 16:52:57
最简单的方法是为你选择的平台找到一个好的图像操作库并使用它。
最困难的方法是打开文件并实际解释其中的二进制数据。要做到这一点,您需要BMP File Specification。我建议先试一试简单的方法。
发布于 2011-08-30 12:42:58
注意:如果您的BMP具有alpha通道,则可能需要为alpha值抓取一个额外的字节。在这种情况下,图像将是image[pixelcount][4],您将添加另一个getc(streamIn)行来保存第四个索引。我的BMP证明不需要这个。
// super-simplified BMP read algorithm to pull out RGB data
// read image for coloring scheme
int image[1024][3]; // first number here is 1024 pixels in my image, 3 is for RGB values
FILE *streamIn;
streamIn = fopen("./mybitmap.bmp", "r");
if (streamIn == (FILE *)0){
printf("File opening error ocurred. Exiting program.\n");
exit(0);
}
int byte;
int count = 0;
for(i=0;i<54;i++) byte = getc(streamIn); // strip out BMP header
for(i=0;i<1024;i++){ // foreach pixel
image[i][2] = getc(streamIn); // use BMP 24bit with no alpha channel
image[i][1] = getc(streamIn); // BMP uses BGR but we want RGB, grab byte-by-byte
image[i][0] = getc(streamIn); // reverse-order array indexing fixes RGB issue...
printf("pixel %d : [%d,%d,%d]\n",i+1,image[i][0],image[i][1],image[i][2]);
}
fclose(streamIn);~蝗虫
发布于 2009-12-28 16:53:50
您需要研究BMP文件格式。读取未压缩的24位BMP文件更容易。它们只包含开头的标题和每个像素的RGB值。
首先,请查看http://en.wikipedia.org/wiki/BMP_file_format上的2x2位图图像示例。请按照以下步骤操作。
使用C程序以二进制模式创建在Wikipedia.
字节将分别为0、0和255。(不确定订单是否为RGB。我很久以前就这样做了,我想顺序不是RGB。只需验证这一点。)
就这么简单!研究BMP的报头以了解有关格式的更多信息。
https://stackoverflow.com/questions/1968561
复制相似问题