如何获取24位图的灰度值?高手请进啊! 如何获取24位图的灰度值?高手请进啊!有源代码最好,并且要有注释啊。解决后,立即加分啊,我急!谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 灰度的概念是RGB中三个分量都是一样的,所以24位图就是三个分量可以不一样了,所以你可以这样想就知道了 void Onfile() { CString FileName; BYTE check[10]; CFile bmp; CFileDialog bmpfile(TRUE,_T("*.*"),NULL,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,_T("BMP 文件|*.bmp|All Files (*.*)|*.*||"),NULL); //获取文件路径 if(bmpfile.DoModal()==IDCANCEL) return; FileName=bmpfile.GetPathName(); //打开文件 OK=bmp.Open(FileName,CFile::typeBinary|CFile::shareExclusive,NULL); if(OK==FALSE) { MessageBox("该文件打开出错","warnning",MB_OK); Clear(); return; } //判断文件格式 bmp.Read(check,2); //BMP文件第一二个字母为BM if(check[0]!='B' || check[1]!='M'){ MessageBox("文件格式不正确","warnning",MB_OK); return; } SetWindowText(FileName); bmp.Seek(0,0); bmp.Read(&FileHead,sizeof(FileHead)); bmp.Read(&BmpInfo,sizeof(BmpInfo)); if(buffer!=NULL) free(buffer); if(ash!=NULL) free(ash); buffer=(unsigned char *) malloc(BmpInfo.biHeight*BmpInfo.biWidth*3); ash=(unsigned char *) malloc(BmpInfo.biHeight*BmpInfo.biWidth); bmp.Seek(FileHead.bfOffBits,0); for(int y=BmpInfo.biHeight-1;y>=0;y--) for(int x=0;x<BmpInfo.biWidth;x++){ bmp.Read(&buffer[y*BmpInfo.biWidth*3+3*x],3); } bmp.Close();}void Drawashp() //绘制灰度图{ CClientDC dc(this); for(int x=0;x<BmpInfo.biHeight;x++) for(int y=0;y<BmpInfo.biWidth;y++){ ash[x*BmpInfo.biWidth+y]=(150*buffer[x*BmpInfo.biWidth*3+3*y+1]+29*buffer[x*BmpInfo.biWidth*3+3*y]+76*buffer[x*BmpInfo.biWidth*3+3*y+2])/256; int t=RGB(ash[x*BmpInfo.biWidth+y],ash[x*BmpInfo.biWidth+y],ash[x*BmpInfo.biWidth+y]); dc.SetPixel(y,x,t); }} RGB转灰度公式:Gray=R*0.299 + G*0.587 + B*0.114整数运算版:Gray=(R*299 + G* 587 + B*114 +500)/1000 //+500是为了四舍五入楼上的“Gray=(R*76 + G*150 + B*29)/256”是一种简化算法其实这样效率更高:ash[x*BmpInfo.biWidth+y]=(150*buffer[x*BmpInfo.biWidth*3+3*y+1]+29*buffer[x*BmpInfo.biWidth*3+3*y]+76*buffer[x*BmpInfo.biWidth*3+3*y+2])>>8 DIB处理原理(当时是为了VB写的):http://www.csdn.net/develop/read_article.asp?id=20442 真正意义上的灰度应该是彩色图像上各颜色值的功率之和,所以不难得到其转换公式v=sqrt(R*R+G*G+B*B),需要源码就给分,留下Email 我给你做个程序 vc截取固定像素宽度的字符串方法 线程下创建socket出错 请教vfw视频切换问题 我是如何进入IBM的!!!!!!! 哪有Dsoframer详细解说文档 各位VC经验丰富的前辈,帮一下忙... 看我小样 子父类对话框的问题,求大神帮助啊!!! VC ODBC 编程问题——recordset只读 急!急需rational rose 98i的license key!! 很简单的问题,希望大家帮忙解决一下 一个关于对话框的问题:有一个基于对话框的程序,点一个button,创建一个modeless对话框
{
CString FileName;
BYTE check[10];
CFile bmp;
CFileDialog bmpfile(TRUE,_T("*.*"),NULL,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,_T("BMP 文件|*.bmp|All Files (*.*)|*.*||"),NULL);
//获取文件路径
if(bmpfile.DoModal()==IDCANCEL) return;
FileName=bmpfile.GetPathName();
//打开文件
OK=bmp.Open(FileName,CFile::typeBinary|CFile::shareExclusive,NULL);
if(OK==FALSE) {
MessageBox("该文件打开出错","warnning",MB_OK);
Clear();
return;
} //判断文件格式
bmp.Read(check,2); //BMP文件第一二个字母为BM
if(check[0]!='B' || check[1]!='M'){
MessageBox("文件格式不正确","warnning",MB_OK);
return;
}
SetWindowText(FileName);
bmp.Seek(0,0);
bmp.Read(&FileHead,sizeof(FileHead));
bmp.Read(&BmpInfo,sizeof(BmpInfo));
if(buffer!=NULL) free(buffer);
if(ash!=NULL) free(ash);
buffer=(unsigned char *) malloc(BmpInfo.biHeight*BmpInfo.biWidth*3);
ash=(unsigned char *) malloc(BmpInfo.biHeight*BmpInfo.biWidth);
bmp.Seek(FileHead.bfOffBits,0); for(int y=BmpInfo.biHeight-1;y>=0;y--)
for(int x=0;x<BmpInfo.biWidth;x++){
bmp.Read(&buffer[y*BmpInfo.biWidth*3+3*x],3);
}
bmp.Close();
}void Drawashp() //绘制灰度图
{
CClientDC dc(this);
for(int x=0;x<BmpInfo.biHeight;x++)
for(int y=0;y<BmpInfo.biWidth;y++){
ash[x*BmpInfo.biWidth+y]=(150*buffer[x*BmpInfo.biWidth*3+3*y+1]+29*buffer[x*BmpInfo.biWidth*3+3*y]+76*buffer[x*BmpInfo.biWidth*3+3*y+2])/256;
int t=RGB(ash[x*BmpInfo.biWidth+y],ash[x*BmpInfo.biWidth+y],ash[x*BmpInfo.biWidth+y]);
dc.SetPixel(y,x,t);
}
}
楼上的“Gray=(R*76 + G*150 + B*29)/256”是一种简化算法
其实这样效率更高:ash[x*BmpInfo.biWidth+y]=(150*buffer[x*BmpInfo.biWidth*3+3*y+1]+29*buffer[x*BmpInfo.biWidth*3+3*y]+76*buffer[x*BmpInfo.biWidth*3+3*y+2])>>8
http://www.csdn.net/develop/read_article.asp?id=20442
v=sqrt(R*R+G*G+B*B),需要源码就给分,留下Email 我给你做个程序