如何获取24位图的灰度值?高手请进啊!
有源代码最好,并且要有注释啊。解决后,立即加分啊,我急!
谢谢!

解决方案 »

  1.   

    灰度的概念是RGB中三个分量都是一样的,所以24位图就是三个分量可以不一样了,所以你可以这样想就知道了
      

  2.   

    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);
    }
    }
      

  3.   

    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
      

  4.   

    DIB处理原理(当时是为了VB写的):
    http://www.csdn.net/develop/read_article.asp?id=20442
      

  5.   

    真正意义上的灰度应该是彩色图像上各颜色值的功率之和,所以不难得到其转换公式
    v=sqrt(R*R+G*G+B*B),需要源码就给分,留下Email 我给你做个程序