普通网友 2017-11-29 01:38 采纳率: 0%
浏览 1365

二维数组数据ucValue[100][100],想存储为灰度图。(但是该图像存储后为彩色的)

二维数组数据ucValue[100][100],存储为灰度图。(但是用我代码存储后该图像为彩色的)
代码如下:
width_p = 100;
height_p = 100;
bitCount = 8;
//存储为bitmap
FILE* f = fopen(save_fileName, "wb");//create or open file to be written
if (f == NULL) return false;
memset(&fileHeader, 0, sizeof(BITMAPFILEHEADER));
memset(&infoHeader, 0, sizeof(BITMAPINFOHEADER));
int colorTableSize = 0;//if bitcount is 24, there is no color table.
if (bitCount == 8)//if bitcount is 8 ,the size of color table is 256*4,4B is the size of RGBQUAD.
colorTableSize = sizeof(RGBQUAD)* 256;

int headerSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTableSize;//the size of the header of bmp file.
int lineSize = (width_p*bitCount / 8 + 3) / 4 * 4;//the size of each line in bmp file.
int dataSize = lineSize*height_p;//the size of the image data of bmp file.

fileHeader.bfType = (WORD)0x4D42;//set the attribute of BITMAPFILEHEADER
fileHeader.bfSize = (DWORD)(headerSize + dataSize);
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = (DWORD)headerSize;

infoHeader.biSize = sizeof(BITMAPINFOHEADER);//set the attribute of BITMAPINFOHEADER
infoHeader.biClrUsed = 0;
infoHeader.biWidth = width_p;
infoHeader.biHeight = height_p;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = bitCount;
infoHeader.biCompression = BI_RGB;
infoHeader.biSizeImage = dataSize;
infoHeader.biClrImportant = 0;
infoHeader.biXPelsPerMeter = 0;
infoHeader.biYPelsPerMeter = 0;

fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f);//write the data of BITFILEHEADER to bmp file
fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f);//write the data of BITINFOHEADER to bmp file
colorTable = new RGBQUAD[256];
int i;
for (i = 0; i<256; i++)
{
colorTable[i].rgbBlue = (BYTE)i;
colorTable[i].rgbGreen = (BYTE)i;
colorTable[i].rgbRed = (BYTE)i;
colorTable[i].rgbReserved = 0;
}
fwrite(&colorTable, sizeof(RGBQUAD), 256, f);
fwrite(radarData.ucValue, 1, dataSize, f);//write the image data to bmp file

fclose(f);//data writting is finished,close the bmp file.

  • 写回答

1条回答 默认 最新

  • shifenglv 2017-11-29 02:43
    关注

    因为你的颜色数据就是彩色的。不是你想保存为灰度,他就是灰度图。你要把彩色图变为灰度图,要先对彩色数据进行计算转换才行。

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题