读取bmp图像所有像素点的RGB值的函数实现.docx
《读取bmp图像所有像素点的RGB值的函数实现.docx》由会员分享,可在线阅读,更多相关《读取bmp图像所有像素点的RGB值的函数实现.docx(16页珍藏版)》请在冰豆网上搜索。
![读取bmp图像所有像素点的RGB值的函数实现.docx](https://file1.bdocx.com/fileroot1/2022-10/1/927ff42c-6998-4992-be5c-69ba5511bfca/927ff42c-6998-4992-be5c-69ba5511bfca1.gif)
读取bmp图像所有像素点的RGB值的函数实现
读取bmp图像所有像素点的RGB值的函数实现:
voidpixHandle(char*bmpName)
{
intr[bmpHeight][bmpWidth];
intg[bmpHeight][bmpWidth];
intb[bmpHeight][bmpWidth];
readBmp(bmpName);
cout<<"width="<<<"height="<<<"BitCount="<intlineByte=(bmpWidth*biBitCount/8+3)/4*4;
intn=0;
if(biBitCount==8)//灰度图像
{
for(inti=0;i{
for(intj=0;j{
r[i][j]=*(pBmpBuf+i*lineByte+j);
g[i][j]=*(pBmpBuf+i*lineByte+j);
b[i][j]=*(pBmpBuf+i*lineByte+j);
n++;
}
}
}
elseif(biBitCount==24)//彩色图像
{
for(inti=0;i{
for(intj=0;j{
for(intk=0;k<3;k++)
{
if(k==0)
{
b[i][j]=*(pBmpBuf+i*lineByte+j*3+k);
}
if(k==1)
{
g[i][j]=*(pBmpBuf+i*lineByte+j*3+k);
}
if(k==2)
{
r[i][j]=*(pBmpBuf+i*lineByte+j*3+k);
}
}
n++;
}
}
}
cout<<"总的像素个数为:
"<ofstreamoutfile("图像像素的rgb分量.txt",ios:
:
out|ios:
:
trunc);
if(!
outfile)
{
cout<<"Openerror!
"<}
for(inti=0;i{
for(intj=0;j{
if(j%bmpWidth==0)
{
outfile<}
outfile<}
}
outfile<for(inti=0;i{
for(intj=0;j{
if(j%bmpWidth==0)
{
outfile<}
outfile<}
}
outfile<for(inti=0;i{
for(intj=0;j{
if(j%bmpWidth==0)
{
outfile<}
outfile<
}
}
outfile.close();
}
读取卫星小图片(针对3.1.3节输出的RGB三个结果矩阵)高度坐标的完整程序如下:
#include
#include
#include
#include
#include
#include
usingnamespacestd;
intbmpHeight=17;
intbmpWidth=16;
doubleheight[17][16];
typedefunsignedcharBYTE;
typedefvector>Mat;
structcolorTable
{
BYTEred;
BYTEgreen;
BYTEblue;
doubleheight;
};
voidgetHeight(Mat&r,Mat&g,Mat&b,colorTable*color);
Matinput(constchar*nameFile);
intgetMin(float*a);
intmain()
{
Matr=input("red.txt");
Matg=input("green.txt");
Matb=input("blue.txt");
colorTablecolor[31];
for(inti=0;i<31;++i)//color[31]的初始化
{
if(i<=15)
{
color[i].red=0;
color[i].green=0+17*i;
color[i].blue=255-17*i;
}
else
{
color[i].red=0+(i-15)*17;
color[i].green=255-(i-15)*17;
color[i].blue=0;
}
color[i].height=i*21.7;
}
getHeight(r,g,b,color);
for(inti=0;i{
for(intj=0;jcout<cout<}
ofstreamout("high.txt",ios:
:
out);
for(inti=0;i{
for(intj=0;jout<out<}
return0;
}
voidgetHeight(Mat&r,Mat&g,Mat&b,colorTable*color)
{
floata[31];
//获取每像素点的高度坐标
for(inti=0;i{
for(intj=0;j{
for(intk=0;k<31;++k)
{
a[k]=sqrt((r[i][j]-color[k].red)*(r[i][j]-color[k].red)+(g[i][j]-color[k].green)*(g[i][j]-color[k].green)+(b[i][j]-color[k].blue)*(b[i][j]-color[k].blue));
}
intindex=getMin(a);
height[i][j]=color[index].height;
}
}
}
Matinput(constchar*nameFile)
{
ifstreamin(nameFile,ios:
:
in);
Mata;
istringstreamiss;
stringstr;
vectortmpvec;
while(getline(in,str))
{
iss.str(str);
BYTEtmp;
while(iss>>tmp)
{
tmpvec.push_back(tmp);
}
a.push_back(tmpvec);
tmpvec.clear();
iss.clear();
}
returna;
}
intgetMin(float*a)
{
intindex=0;
floatmin=a[0];
for(inti=1;i<31;++i)
{
if(a[i]{
min=a[i];
index=i;
}
}
returnindex;
}
上述程序运行后输出高度矩阵的结果如下:
151.9477.4151.9151.9477.4151.9151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9
151.9151.9477.4151.9477.4151.9151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9
151.9151.9477.4151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9477.4151.9
151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9477.4151.9477.4151.9151.9
151.9151.9151.9151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9477.4151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9151.9
151.9151.9477.4151.9151.9151.9151.9151.9477.4151.9151.9151.9151.9151.9477.4151.9
151.9477.4477.4151.9477.4151.9151.9477.4477.4151.9477.4477.4151.9151.9477.4151.9
151.9151.9151.9151.9477.