| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 713 人关注过本帖
标题:[求助]基于VC的数字图像的阈值选取和二值化
只看楼主 加入收藏
shdenny
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-4-26
收藏
 问题点数:0 回复次数:1 
[求助]基于VC的数字图像的阈值选取和二值化

//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);//更新视
}


麻烦大侠们帮我加一下注释啊!!谢谢啊!!!

搜索更多相关主题的帖子: 阈值 图像 数字 hDIB 
2007-05-10 23:17
shdenny
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-4-26
收藏
得分:0 
麻烦啊!!!
2007-05-10 23:17
快速回复:[求助]基于VC的数字图像的阈值选取和二值化
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018798 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved