高手来看看,这个二值化的处理问题处在哪里
不论设置阈值为多少,处理结果是图像变成纯白(就是图像没有了)
void CXXXView::OnBinary()
{
CBinaryDlg binaryDlg;
if(binaryDlg.DoModal() == IDOK)
{
CShuFaDoc* pDoc = GetDocument();
pDoc->m_pDib->Binary(binaryDlg.m_nThre);
pDoc->SetModifiedFlag(TRUE);
OnRealizePal((WPARAM)m_hWnd,0); // realize the new palette
pDoc->UpdateAllViews(NULL);
}
}
BOOL CDib::Binary(BYTE bThre)
{
HDIB hNewDib = ThresholdDIB(m_hDib, bThre);
if(! hNewDib)
return FALSE;
// set to m_hDib
Destroy();
m_hDib = hNewDib;
// return
return UpdateInternal();
}
HDIB ThresholdDIB(HDIB hDIB, BYTE bThre)
{
WaitCursorBegin();
// Get source bitmap info
LPBITMAPINFO lpBmInfo = (LPBITMAPINFO)GlobalLock(hDIB);
int bpp = lpBmInfo->bmiHeader.biBitCount; // Bits per pixel
int nWidth = lpBmInfo->bmiHeader.biWidth;
int nHeight = lpBmInfo->bmiHeader.biHeight;
int nRowBytes = ((((nWidth * bpp) + 31) & ~31) / 8);
// Make sure height is positive and biCompression is BI_RGB or BI_BITFIELDS
DWORD compression = lpBmInfo->bmiHeader.biCompression;
if(nHeight < 0 || (compression != BI_RGB))
{
GlobalUnlock(hDIB);
WaitCursorEnd();
return NULL;
}
LPBYTE lpDIBBits = FindDIBBits((LPBYTE)lpBmInfo);
unsigned char *lpSrc;
for(int i = 0; i < nHeight; i++)
{
for(int j = 0; j < nWidth; j++)
lpSrc = lpDIBBits + nRowBytes * (nHeight - 1 - i) + j;
if((*lpSrc) < bThre)
{
*lpSrc = 0;
}
else
{
*lpSrc = 255;
}
}
GlobalUnlock(hDIB);
WaitCursorEnd();
return hDIB;
}