这是八位灰度图像的直方图统计:
DWORD * m_pGrayBuf =new DWORD[256];
memset((void*)m_pGrayBuf,0,256*sizeof(DWORD));
for(y=0;y<Height;y++)
{
for(x=0;x<Width;x++)
{
m_pGrayBuf[m_dib.m_Buffer [y*Width+x]]++;
}
}
下面是32位图像转灰度后的直方图:
DWORD dwGray;
for(y=0;y<Height;y++)
{
for(x=0;x<Width;x++)
{
dwGray=(DWORD)(m_dib.m_Buffer[y*Width+x*4+0]*30+
m_dib.m_Buffer[y*Width+x*4+1]*59+
m_dib.m_Buffer[y*Width+x*4+2]*11)/100;
dwGray&=0x00ff;
m_pGrayBuf[dwGray]++;
}
}
其次,在界面显示结果:
void CHistDlg::OnPaint()
{
CPaintDC dc(this);
CString str;
CRect rc,rcbk;
GetClientRect(&rc);
dc.MoveTo (rc.Width ()/4,0);
dc.LineTo (rc.Width ()/4,rc.Height ()*2/3+50);
dc.MoveTo (rc.Width ()/4-50,rc.Height ()*2/3+50);
dc.LineTo (rc.Width ()/4+550,rc.Height ()*2/3+50);
for(int i=0;i<=510;i++)
{
if((i%20)==0)
{
dc.SetPixel (rc.Width ()/4+i,rc.Height ()*2/3+50-2,RGB(255,0,0));
dc.SetTextColor (RGB(128,128,128));
str.Format (L"%d",i/2);
dc.TextOut (rc.Width ()/4+i,rc.Height ()*2/3+50+1,str);
}
if(i==510)
{
dc.SetPixel (rc.Width ()/4+i,rc.Height ()*2/3+50-2,RGB(255,0,0));
dc.SetTextColor (RGB(128,128,128));
str.Format (L"%d",i/2);
dc.TextOut(rc.Width ()/4+i,rc.Height ()*2/3+50+1,str);
}
}
for(int i=1;i<=500;i++)
{
if((i%100)==0)
{
dc.SetPixel (rc.Width ()/4+2,rc.Height ()*2/3+50-i,RGB(255,0,0));
dc.SetTextColor (RGB(128,128,128));
str.Format (L"%d",i);
dc.TextOut(rc.Width ()/4-24,rc.Height ()*2/3+50-i,str);
}
}
if(m_dib .m_BmpLoaded)
{
if(this->m_bYou )
{
for(int i=0;i<=255;i++)
{
dc.MoveTo (rc.Width ()/4+2*i,rc.Height ()*2/3+50);
dc.LineTo (rc.Width ()/4+2*i,rc.Height ()*2/3+50-this->m_pGrayBuf[i]);
}
}
}
}
直方图的统计特性,也是用来匹配的一个方法。
直方图均衡化,我们博客中已经有了,不再累述。