用VC++读取BMP图像像素矩阵的问题

jtacm 2006-02-08 05:51:32
请问下各位高手们,我现在想读取BMP格式的图片的像素矩阵值,就是每个像素的颜色值,构成的矩阵,各位高手能介绍下自己的方法啊,小弟万分感谢啊!
...全文
564 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jtacm 2006-02-10
  • 打赏
  • 举报
回复
???
jtacm 2006-02-10
  • 打赏
  • 举报
回复
??
jtacm 2006-02-08
  • 打赏
  • 举报
回复
我想知道最后得到的矩阵的指针是哪个呢?
CmUpDate 2006-02-08
  • 打赏
  • 举报
回复
阿哈,

代码没看,支持楼上
du51 2006-02-08
  • 打赏
  • 举报
回复
Emboss_f(FILE *fpi, FILE *fpo) /*浮雕效果处理*/
{
int nCounti,nCountj,nCountk,nCountl;
int nRed,nBlue,nGreen;
int nDx=1,nDy=1;
unsigned int nPixel1,nPixel2,nPixel;
fread((char *)&strHead,1,sizeof(strHead),fpi);
fread((char *)&strInfo,1,sizeof(strInfo),fpi);
for(nCounti=0;nCounti<256;nCounti++)
fread((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpi);
fwrite((char *)&strHead,1,sizeof(strHead),fpo);
fwrite((char*)&strInfo,1,sizeof(strInfo),fpo);
for(nCounti=0;nCounti<256;nCounti++)
fwrite((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpo);
for(nCounti=0;nCounti<8;nCounti++)
{
for(nCountj=0;nCountj<64;nCountj++)
fread(&naImage[nCountj][0],1,nWidth,fpi);
for(nCountk=0;nCountk<64-1;nCountk++)
{
for(nCountl=0;nCountl<nWidth-1;nCountl++)
{
nPixel1=(unsigned char)(naImage[nCountk][nCountl]);
nPixel2=(unsigned char)(naImage[nCountk+nDx][nCountl+nDy]);
nRed=fabs(straPla[nPixel1].rgbRed-straPla[nPixel2].rgbRed+128);
nGreen=fabs(straPla[nPixel1].rgbGreen-straPla[nPixel2].rgbGreen+128);
nBlue=fabs(straPla[nPixel1].rgbBlue-straPla[nPixel2].rgbBlue+128);
nPixel=Match_f(nRed,nGreen,nBlue);
naImage[nCountk][nCountl]=nPixel;
}
}
for(nCountl=0;nCountl<nWidth-1;nCountl++)
{
nPixel1=(unsigned char)(naImage[63][nCountl]);
nPixel2=(unsigned char)(naImage[63][nCountl+nDy]);
nRed=fabs(straPla[nPixel1].rgbRed-straPla[nPixel2].rgbRed+128);
nGreen=fabs(straPla[nPixel1].rgbGreen-straPla[nPixel2].rgbGreen+128);
nBlue=fabs(straPla[nPixel1].rgbBlue-straPla[nPixel2].rgbBlue+128);
nPixel=Match_f(nRed,nGreen,nBlue);
naImage[63][nCountl]=nPixel;
}
for(nCountj=0;nCountj<64;nCountj++)
fwrite(&naImage[nCountj][0],1,nWidth,fpo);
}
}

Smooth_f(FILE *fpi,FILE *fpo) /*柔化效果处理*/
{
int nCounti,nCountj,nCountk,nCountl,nCountm,nCountn,nRed,nGreen,nBlue;
unsigned nPixel;
fread((char *)&strHead,1,sizeof(strHead),fpi);
fread((char *)&strInfo,1,sizeof(strInfo),fpi);
for(nCounti=0;nCounti<256;nCounti++)
fread((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpi);
fwrite((char *)&strHead,1,sizeof(strHead),fpo);
fwrite((char*)&strInfo,1,sizeof(strInfo),fpo);
for(nCounti=0;nCounti<256;nCounti++)
fwrite((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpo);
for(nCounti=0;nCounti<8;nCounti++)
{
for(nCountj=0;nCountj<64;nCountj++)
fread(&naImage[nCountj][0],1,nWidth,fpi);
for(nCountk=1;nCountk<64-2;nCountk++)
{
for(nCountl=1;nCountl<nWidth-2;nCountl++)
{
nRed=0,nGreen=0,nBlue=0;
for(nCountm=0;nCountm<3;nCountm++) /*用9个点的均值柔化*/
for(nCountn=0;nCountn<3;nCountn++)
{
nPixel=(unsigned char)(naImage[nCountk-1+nCountm][nCountl-1+nCountn]);
nRed+=straPla[nPixel].rgbRed;
nGreen+=straPla[nPixel].rgbGreen;
nBlue+=straPla[nPixel].rgbBlue;
}
nRed=nRed/9;
nGreen=nGreen/9;
nBlue=nBlue/9;
nPixel=Match_f(nRed,nGreen,nBlue);
naImage[nCountk][nCountl]=nPixel;
}
}
for(nCountj=0;nCountj<64;nCountj++)
fwrite(&naImage[nCountj][0],1,nWidth,fpo);
}
}
Initial_f(int nChoice)
{
char szFilena1[30],szFilena2[30];
FILE *fpi, *fpo;
printf("Input the bmp source file: ");
scanf("%s",szFilena1);
printf("Input the bmp termini file: ");
scanf("%s", szFilena2);
if((fpi=fopen(szFilena1,"rb"))==NULL)
{
printf("OPEN SOURCE FILE ERROR\n");
exit(0);
}
if((fpo=fopen(szFilena2,"wb"))==NULL)
{
printf("OPEN TERMINI FILE ERROR\n");
exit(0);
}
if(nChoice==3)
Emboss_f(fpi,fpo);
else if(nChoice==2)
Sharp_f(fpi,fpo);
else if(nChoice==1)
Smooth_f(fpi,fpo);
fclose(fpi);
fclose(fpo);
}

void main()
{
int nChoice;
do
{
printf("\n\t1. Smooth\n");
printf("\n\t2. Sharp\n");
printf("\n\t3. Emboss\n");
printf("\n\t4. Exit\n");
scanf("%d",&nChoice);
switch(nChoice)
{
case 1:
case 2:
case 3:
Initial_f(nChoice);
break;
case 4:
break;
default:
printf("Input again!\n");
break;
}
}while(nChoice!=4);
}
du51 2006-02-08
  • 打赏
  • 举报
回复
#include"stdio.h"
#include"alloc.h"
#include"dos.h"
#include"graphics.h"
#include"math.h"

typedef struct
{
int bfType; /* 类型标志,总是BM */
long bfSize; /* 文件大小 */
int bfReserved1;
int bfReserved2;
long bfOffBits; /* 位图点阵偏移量*/
}HEAD;
typedef struct
{
long biSize; /* 结构体字节总数 */
long biWidth; /* 图像宽度 */
long biHeight; /*图像高度 */
int biPlanes; /* 必须为1 */
int biBitCount; /* 每个像素所占二进制位数,可能是1,4,8或 24 */
long biCompress; /*压缩方式*/
long biSizeImage; /*像素点阵大小 */
long biXPelsPerMeter; /* 水平像素数*/
long biYPelsPerMeter; /* 垂直像素数 */
long biClrUsed; /*使用的颜色数 */
long biClrImportant; /*重要颜色数 */
}INFO;
typedef struct
{
unsigned char rgbBlue; /*蓝色所占比重*/
unsigned char rgbGreen; /*绿色所占比重*/
unsigned char rgbRed; /*红色所占比重*/
unsigned char rgbReserved; /*保留字节 */
}RGBQUAD;

int Match_f(int nRed,int nGreen,int nBlue); /*寻找与像素匹配的调色板的序号*/
Sharp_f(FILE *fpi, FILE *fpo); /* 锐化处理*/
Emboss_f(FILE *fpi, FILE *fpo); /*浮雕效果处理*/
Smooth_f(FILE *fpi,FILE *fpo); /*柔化效果处理*/
Initial_f(int nChoice); /*初始化图像文件*/


RGBQUAD straPla[256]; /*256色调色板*/
HEAD strHead;
INFO strInfo;
unsigned int nWidth=512,nDepth=512; /*处理512×512位图*/
unsigned char naImage[64][512];

int Match_f(int nRed,int nGreen,int nBlue) /*寻找与像素匹配的调色板的序号*/
{
long int nMax,nLength;
int nNum=0,nCounti;
unsigned int nImageRed,nImageGreen, nImageBlue;
nImageRed=(unsigned int)(straPla[0].rgbRed);
nImageGreen=(unsigned int)(straPla[0].rgbGreen);
nImageBlue=(unsigned int)(straPla[0].rgbBlue);
nMax=(nRed-nImageRed)*(nRed-nImageRed)+(nGreen-nImageGreen)*(nGreen-nImageGreen)+(nBlue-nImageBlue)*(nBlue-nImageBlue);
for(nCounti=1;nCounti<256;nCounti++)
{
nImageRed=(unsigned int)(straPla[nCounti].rgbRed);
nImageGreen=(unsigned int)(straPla[nCounti].rgbGreen);
nImageBlue=(unsigned int)(straPla[nCounti].rgbBlue);
nLength=(nRed-nImageRed)*(nRed-nImageRed)+(nGreen-nImageGreen)*(nGreen-nImageGreen)+(nBlue-nImageBlue)*(nBlue-nImageBlue);
if(nMax>nLength)
{
nMax=nLength;
nNum=nCounti;
if(nMax==0)
break;
}
}
return nNum;
}

Sharp_f(FILE *fpi, FILE *fpo) /* 锐化处理*/
{
int nCounti,nCountj,nCountk,nCountl;
int nRed,nBlue,nGreen;
int nDx=1,nDy=1;
unsigned int nPixel1,nPixel2,nPixel;
fread((char *)&strHead,1,sizeof(strHead),fpi);
fread((char *)&strInfo,1,sizeof(strInfo),fpi);
for(nCounti=0;nCounti<256;nCounti++)
fread((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpi);
fwrite((char *)&strHead,1,sizeof(strHead),fpo);
fwrite((char*)&strInfo,1,sizeof(strInfo),fpo);
for(nCounti=0;nCounti<256;nCounti++)
fwrite((char *)&straPla[nCounti],1,sizeof(RGBQUAD),fpo);
for(nCounti=0;nCounti<8;nCounti++)
{
for(nCountj=0;nCountj<64;nCountj++)
fread(&naImage[nCountj][0],1,nWidth,fpi);
for(nCountk=1;nCountk<64-2;nCountk++)
for(nCountl=1;nCountl<nWidth-2;nCountl++)
{
nPixel1=(unsigned char)(naImage[nCountk][nCountl]);
nPixel2=(unsigned char)(naImage[nCountk-nDx][nCountl-nDy]);
nRed=straPla[nPixel1].rgbRed+0.5*(straPla[nPixel1].rgbRed-straPla[nPixel2].rgbRed);
/*0.5为锐化系数*/
nGreen=straPla[nPixel1].rgbGreen+0.5*(straPla[nPixel1].rgbGreen-straPla[nPixel2].rgbGreen);
nBlue=straPla[nPixel1].rgbBlue+0.5*(straPla[nPixel1].rgbBlue-straPla[nPixel2].rgbBlue);
if(nRed>255)
nRed=255;
if(nRed<0)
nRed=0;
if(nGreen>255)
nGreen=255;
if(nGreen<0)
nGreen=0;
if(nBlue>255)
nBlue=255;
if(nBlue<0)
nBlue=0;
nPixel=Match_f(nRed,nGreen,nBlue);
naImage[nCountk][nCountl]=nPixel;
}
for(nCountj=0;nCountj<64;nCountj++)
fwrite(&naImage[nCountj][0],1,nWidth,fpo);
}

}
lukeguo 2006-02-08
  • 打赏
  • 举报
回复
定义一个结构,和BMP的头部说明一致,然后就简单了。

读取头部结构,再根据结构读取数据。

具体的结构忘了,在网上找一下。如果找不到就用图画板画图,存成不同颜色位宽的文件分析。

现在只记得:
图像点的存储是倒的,即最后一行在前面,第一行在最后;
每一行的数据是按普通顺序的,第一点在开始;占用字节好像是图像宽度点按8对齐(即1~8点都是一样的存储尺寸)。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧