读取bmp图像所有像素点的RGB值的函数实现.docx

上传人:b****2 文档编号:29708 上传时间:2022-10-01 格式:DOCX 页数:16 大小:13.39KB
下载 相关 举报
读取bmp图像所有像素点的RGB值的函数实现.docx_第1页
第1页 / 共16页
读取bmp图像所有像素点的RGB值的函数实现.docx_第2页
第2页 / 共16页
读取bmp图像所有像素点的RGB值的函数实现.docx_第3页
第3页 / 共16页
读取bmp图像所有像素点的RGB值的函数实现.docx_第4页
第4页 / 共16页
读取bmp图像所有像素点的RGB值的函数实现.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

读取bmp图像所有像素点的RGB值的函数实现.docx

《读取bmp图像所有像素点的RGB值的函数实现.docx》由会员分享,可在线阅读,更多相关《读取bmp图像所有像素点的RGB值的函数实现.docx(16页珍藏版)》请在冰豆网上搜索。

读取bmp图像所有像素点的RGB值的函数实现.docx

读取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;j

cout<

cout<

}

ofstreamout("high.txt",ios:

:

out);

for(inti=0;i

{

for(intj=0;j

out<

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.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1