我想知道,从这里我将如何将像素值存储到矩阵中?我想打印出强度(灰度)值。
示例 Array[5] = 4 或 Array[3][1] = 17
我真的不知道该怎么做。我看到的所有示例似乎都很复杂,有没有简单的方法可以做到这一点?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BitMap
{
short Type;
long Size;
short Reserve1;
short Reserve2;
long OffBits;
long biSize;
long biWidth;
long biHeight;
short biPlanes;
short biBitCount;
long biCompression;
long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
long biClrUsed;
long biClrImportant;
}Header;
int main( void ){
FILE *BMPFile = fopen ("MriHotrod.bmp", "rb");
if(BMPFile == NULL){
return;
}
unsigned char DataBuff[128*128];
int i;
int j;
memset(&Header, 0, sizeof(Header));
fread(&Header.Type, 2, 1, BMPFile);
fread(&Header.Size, 4, 1, BMPFile);
fread(&Header.Reserve1, 2, 1, BMPFile);
fread(&Header.Reserve2, 2, 1, BMPFile);
fread(&Header.OffBits, 4, 1, BMPFile);
fread(&Header.biSize, 4, 1, BMPFile);
fread(&Header.biWidth, 4, 1, BMPFile);
fread(&Header.biHeight, 4, 1, BMPFile);
fread(&Header.biPlanes, 2, 1, BMPFile);
fread(&Header.biBitCount, 2, 1, BMPFile);
fread(&Header.biCompression, 4, 1, BMPFile);
fread(&Header.biSizeImage, 4, 1, BMPFile);
fread(&Header.biXPelsPerMeter, 4, 1, BMPFile);
fread(&Header.biYPelsPerMeter, 4, 1, BMPFile);
fread(&Header.biClrUsed, 4, 1, BMPFile);
fread(&Header.biClrImportant, 4, 1, BMPFile);
printf("\nType:%hd\n", Header.Type);
printf("Size:%ld\n", Header.Size);
printf("Reserve1:%hd\n", Header.Reserve1);
printf("Reserve2:%hd\n", Header.Reserve2);
printf("OffBits:%ld\n", Header.OffBits);
printf("biSize:%ld\n", Header.biSize);
printf("Width:%ld\n", Header.biWidth);
printf("Height:%ld\n", Header.biHeight);
printf("biPlanes:%hd\n", Header.biPlanes);
printf("biBitCount:%hd\n", Header.biBitCount);
printf("biCompression:%ld\n", Header.biCompression);
printf("biSizeImage:%ld\n", Header.biSizeImage);
printf("biXPelsPerMeter:%ld\n", Header.biXPelsPerMeter);
printf("biYPelsPerMeter:%ld\n", Header.biYPelsPerMeter);
printf("biClrUsed:%ld\n", Header.biClrUsed);
printf("biClrImportant:%ld\n\n", Header.biClrImportant);
fread(DataBuff, sizeof(DataBuff), 128*128, BMPFile);
for(i=0; i<20; i++){
printf("%d\n", DataBuff[i]);
}
fclose(BMPFile);
return 0;
}
Output
Type:19778
Size:17462
Reserve1:0
Reserve2:0
OffBits:1078
biSize:40
Width:128
Height:128
biPlanes:1
biBitCount:8
biCompression:0
biSizeImage:0
biXPelsPerMeter:0
biYPelsPerMeter:0
biClrUsed:256
biClrImportant:0
0
0
0
0
1
1
1
0
2
2
2
0
3
3
3
0
4
4
4
0
我认为标题信息是正确的,并且元素的数量是正确的(16384)。我只是不确定如何打印出像素值。以上显然是不对的...
这与填充有关吗?
谢谢!