//2,阈值选取及二值化
void CDIBDoc::OnBinarization() 
{
    if     (!m_hDIB)
    {
        AfxMessageBox("请先打开一个位图文件!");
        return;
    }
    Binarization(DaJinProc(m_hDIB),m_hDIB);
}
//Helper function:大津判别法
int CDIBDoc::DaJinProc(HDIB hdib)
{
    if (!m_hDIB)
    {
        AfxMessageBox("请先打开一个位图文件!");
        return 0;
    }
LONG DataHistogram[256];
    LPBYTE    pData;                //the pointer to the bitmap data
    UINT    BytesPerRow;        //bytes of each bitmap line
    LONG    w,h,nHeight,nWidth;
    BITMAPINFO *bmi;            //bitmap information
    bmi=(BITMAPINFO *)GlobalLock(hdib);//get the point of the memory block
    if(!bmi)
        return 0;
    LPBITMAPINFOHEADER    lpbi=(LPBITMAPINFOHEADER)bmi;//get the bitmapinfoheader pointer
    pData=(LPBYTE)lpbi+lpbi->biSize;//blue sub-color
    BytesPerRow=WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth);//Macro
    nHeight=lpbi->biHeight;
    nWidth=lpbi->biWidth;
memset((void *)DataHistogram,0,256*sizeof(LONG));//allocate memory
    for (h=0;h<nHeight;h++)
    {
        for(w=0;w<nWidth;w++)
        {
            //分别计算0~255颜色值个数
            DataHistogram[*(pData+w*3+h*BytesPerRow)]++;
        }
    }
    //方法:大津判别法
    const    double ConstMin=1.0E-9;
    int        loThreshold=0;
    double    RM=0.0;
    double    VA=0.0;
    double    S,SMax=0.0;
    double    D;
    double  p=0.0;
    double  a=0.0;
    for (h=0;h<256;h++)
    {
        RM+=(double)h*DataHistogram[h]/nWidth/nHeight;    //计算整体均值
    }
    for (h=0;h<256-1;h++)
    {
        
        p+=(double)DataHistogram[h]/nWidth/nHeight;
        a+=(double)h*DataHistogram[h]/nWidth/nHeight;
        S=RM*p-a;
        D=p*(1-p);
        if (D<ConstMin)
            continue;
        S=S*S/D;
        if (S<SMax)
            continue;
        SMax=S;
        loThreshold=h;
    }
    return    loThreshold;
}
//Helper function:二值化
void CDIBDoc::Binarization(int threshold,HDIB hdib)
{
    CString str;
    str.Format("阈值为:%d",threshold);//显示阀值
    AfxMessageBox(str);
    
    BITMAPINFO        *bmi;
    bmi=(BITMAPINFO *)GlobalLock(hdib);
    if (!bmi)
        return;
    LPBITMAPINFOHEADER    lpbi;
    lpbi=(LPBITMAPINFOHEADER)bmi;
    int nWidth,nHeight,w,h;
    nWidth=lpbi->biWidth;
    nHeight=lpbi->biHeight;
    
    LPBYTE lpBits=(LPBYTE)lpbi+lpbi->biSize;//blue sub_color
    int BytesPerLine=WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth);
    for (h=0;h<nHeight;h++)
    {
        for (w=0;w<nWidth;w++)
        {
            
            if (*(lpBits+w*3+h*BytesPerLine)>threshold)
            {
                *(lpBits+w*3+h*BytesPerLine+2)=255;
                *(lpBits+w*3+h*BytesPerLine+1)=255;
                *(lpBits+w*3+h*BytesPerLine)=255;                
            }
            else 
            {
                *(lpBits+w*3+h*BytesPerLine+2)=0;
                *(lpBits+w*3+h*BytesPerLine+1)=0;
                *(lpBits+w*3+h*BytesPerLine)=0;                
            }
        }
    }
    UpdateAllViews(NULL);//更新视
}
麻烦大侠们帮我加一下注释啊!!谢谢啊!!!



 
											





 
	    

 
	
