bmp图像降灰度值的问题~

VAN_C VAN_C 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
需要把一张灰度图降低灰度值后输出,降到2灰度级是正常的。
但是降到16灰度级和4灰度级图像不知为什么有部分变成蓝色,循环中除了判断条件外其他都是跟降到2灰度级完全一样的,效果如下:
(4灰度级的)


(16灰度级的)




代码:
void GrayLevelResolution(BITMAPFILEHEADER head, BITMAPINFOHEADER info, RGBQUAD pla[256], IMAGEDATA* image)
{
IMAGEDATA *new_image;
new_image = (IMAGEDATA*)malloc(info.biWidth *info.biHeight * sizeof(IMAGEDATA));

cout<<"k=8: GrayLevelResolution_8.bmp"<<endl;
cout<<"k=4: GrayLevelResolution_4.bmp"<<endl;
cout<<"k=2: GrayLevelResolution_2.bmp"<<endl;
cout<<"k=1: GrayLevelResolution_1.bmp"<<endl;

FILE *fw;//k = 8,256灰度级
if((fw=fopen("GrayLevelResolution_8.bmp","wb"))==NULL)
{
cout<<"create the bmp file error!"<<endl;
return;
}
Write(fw,info.biWidth, info.biWidth,head,info,pla,image);


for(int i = 0;i < info.biHeight;i++) //k = 4, 16灰度级
for(int j = 0;j < info.biWidth;j++)
{
if((*(image + i * info.biWidth + j)).blue <= 15)
{
(*(new_image + i * info.biWidth + j)).blue = 0;
(*(new_image + i * info.biWidth + j)).green = 0;
(*(new_image + i * info.biWidth + j)).red = 0;
}
else if((*(image + i * info.biWidth + j)).blue < 31)
{
(*(new_image + i * info.biWidth + j)).blue = 15;
(*(new_image + i * info.biWidth + j)).green = 15;
(*(new_image + i * info.biWidth + j)).red = 15;
}
else if((*(image + i * info.biWidth + j)).blue < 47)
{
(*(new_image + i * info.biWidth + j)).blue = 31;
(*(new_image + i * info.biWidth + j)).green = 31;
(*(new_image + i * info.biWidth + j)).red = 31;
}
else if((*(image + i * info.biWidth + j)).blue < 63)
{
(*(new_image + i * info.biWidth + j)).blue = 47;
(*(new_image + i * info.biWidth + j)).green = 47;
(*(new_image + i * info.biWidth + j)).red = 47;
}
else if((*(image + i * info.biWidth + j)).blue < 79)
{
(*(new_image + i * info.biWidth + j)).blue = 63;
(*(new_image + i * info.biWidth + j)).green = 63;
(*(new_image + i * info.biWidth + j)).red = 63;
}
else if((*(image + i * info.biWidth + j)).blue < 95)
{
(*(new_image + i * info.biWidth + j)).blue = 79;
(*(new_image + i * info.biWidth + j)).green = 79;
(*(new_image + i * info.biWidth + j)).red = 79;
}
else if((*(image + i * info.biWidth + j)).blue < 111)
{
(*(new_image + i * info.biWidth + j)).blue = 95;
(*(new_image + i * info.biWidth + j)).green = 95;
(*(new_image + i * info.biWidth + j)).red = 95;
}
else if((*(image + i * info.biWidth + j)).blue < 127)
{
(*(new_image + i * info.biWidth + j)).blue = 111;
(*(new_image + i * info.biWidth + j)).green = 111;
(*(new_image + i * info.biWidth + j)).red = 111;
}
else if((*(image + i * info.biWidth + j)).blue < 143)
{
(*(new_image + i * info.biWidth + j)).blue = 127;
(*(new_image + i * info.biWidth + j)).green = 127;
(*(new_image + i * info.biWidth + j)).red = 127;
}
else if((*(image + i * info.biWidth + j)).blue < 159)
{
(*(new_image + i * info.biWidth + j)).blue = 143;
(*(new_image + i * info.biWidth + j)).green = 143;
(*(new_image + i * info.biWidth + j)).red = 143;
}
else if((*(image + i * info.biWidth + j)).blue < 175)
{
(*(new_image + i * info.biWidth + j)).blue = 159;
(*(new_image + i * info.biWidth + j)).green = 159;
(*(new_image + i * info.biWidth + j)).red = 159;
}
else if((*(image + i * info.biWidth + j)).blue < 191)
{
(*(new_image + i * info.biWidth + j)).blue = 175;
(*(new_image + i * info.biWidth + j)).green = 175;
(*(new_image + i * info.biWidth + j)).red = 175;
}
else if((*(image + i * info.biWidth + j)).blue < 207)
{
(*(new_image + i * info.biWidth + j)).blue = 191;
(*(new_image + i * info.biWidth + j)).green = 191;
(*(new_image + i * info.biWidth + j)).red = 191;
}
else if((*(image + i * info.biWidth + j)).blue < 223)
{
(*(new_image + i * info.biWidth + j)).blue = 207;
(*(new_image + i * info.biWidth + j)).green = 207;
(*(new_image + i * info.biWidth + j)).red = 207;
}
else if((*(image + i * info.biWidth + j)).blue < 239)
{
(*(new_image + i * info.biWidth + j)).blue = 223;
(*(new_image + i * info.biWidth + j)).green = 223;
(*(new_image + i * info.biWidth + j)).red = 223;
}
else if((*(image + i * info.biWidth + j)).blue < 255)
{
(*(new_image + i * info.biWidth + j)).blue = 239;
(*(new_image + i * info.biWidth + j)).green = 239;
(*(new_image + i * info.biWidth + j)).red = 239;
}
}
// FILE *fw;//写回文件
if((fw=fopen("GrayLevelResolution_4.bmp","wb"))==NULL)
{
cout<<"create the bmp file error!"<<endl;
return;
}
Write(fw,info.biWidth, info.biWidth,head,info,pla,new_image);


for(int i = 0;i < info.biHeight;i++) //k = 2, 4灰度级
for(int j = 0;j < info.biWidth;j++)
{
if((*(image + i * info.biWidth + j)).blue <= 15)
{
(*(new_image + i * info.biWidth + j)).blue = 0;
(*(new_image + i * info.biWidth + j)).green = 0;
(*(new_image + i * info.biWidth + j)).red = 0;
}
else if((*(image + i * info.biWidth + j)).blue < 63)
{
(*(new_image + i * info.biWidth + j)).blue = 15;
(*(new_image + i * info.biWidth + j)).green = 15;
(*(new_image + i * info.biWidth + j)).red = 15;
}
else if((*(image + i * info.biWidth + j)).blue < 127)
{
(*(new_image + i * info.biWidth + j)).blue = 63;
(*(new_image + i * info.biWidth + j)).green = 63;
(*(new_image + i * info.biWidth + j)).red = 63;
}
else if((*(image + i * info.biWidth + j)).blue < 191)
{
(*(new_image + i * info.biWidth + j)).blue = 127;
(*(new_image + i * info.biWidth + j)).green = 127;
(*(new_image + i * info.biWidth + j)).red = 127;
}
else if((*(image + i * info.biWidth + j)).blue < 255)
{
(*(new_image + i * info.biWidth + j)).blue = 191;
(*(new_image + i * info.biWidth + j)).green = 191;
(*(new_image + i * info.biWidth + j)).red = 191;
}
//cout<<(*(new_image + i * info.biWidth + j)).blue<<" "<<(*(new_image + i * info.biWidth + j)).green<<" "<<(*(new_image + i * info.biWidth + j)).red<<endl;
}
if((fw=fopen("GrayLevelResolution_2.bmp","wb"))==NULL)
{
cout<<"create the bmp file error!"<<endl;
return;
}

Write(fw,info.biWidth, info.biWidth,head,info,pla,new_image);


for(int i = 0;i < info.biHeight;i++) //k = 1,2灰度级
for(int j = 0;j < info.biWidth;j++)
{
if((*(image + i * info.biWidth + j)).blue <= 127)
{
(*(new_image + i * info.biWidth + j)).blue = 0;
(*(new_image + i * info.biWidth + j)).green = 0;
(*(new_image + i * info.biWidth + j)).red = 0;
}
else
{
(*(new_image + i * info.biWidth + j)).blue = 255;
(*(new_image + i * info.biWidth + j)).green = 255;
(*(new_image + i * info.biWidth + j)).red = 255;
}
}
// FILE *fw;//写回文件
if((fw=fopen("GrayLevelResolution_1.bmp","wb"))==NULL)
{
cout<<"create the bmp file error!"<<endl;
return;
}

Write(fw,info.biWidth, info.biWidth,head,info,pla,new_image);
}



第一次作业就遇上无从入手的问题= = 求救求救
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
VAN_C
其它技术问题 3,881 成员 9,044 内容