最大方差阈值分割(vc实现)
2012-08-14 10:09
134 查看
void CISLSView::OnThresholdOtsu() { //程序编制:李立宗 lilizong@gmail.com //2012-8-14 if(myImage1.IsNull()) OnOpenResourceFile(); if(!myImage2.IsNull()) myImage2.Destroy(); if(myImage2.IsNull()){ myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0); } //COLORREF pixel; int maxY = myImage1.GetHeight(); int maxX=myImage1.GetWidth(); byte* pRealData; byte* pRealData2; pRealData=(byte*)myImage1.GetBits(); pRealData2=(byte*)myImage2.GetBits(); int pit=myImage1.GetPitch(); int pit2=myImage2.GetPitch(); //需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现 //CString str; //str.Format(TEXT("%d"),pit); //MessageBox(str); //str.Format(TEXT("%d"),pit2); //MessageBox(str); int bitCount=myImage1.GetBPP()/8; int bitCount2=myImage2.GetBPP()/8; int tempR,tempG,tempB; //float temp,tempX,tempY; int temp; int T=128; //int pixel[4]; float u0,u1; //均值 float w0,w1; //概率 float sum0,sum1; //像素和 int optIndex,optT; //最优阈值,及其所在像素的值 float fVaria,fMaxVaria=0; //临时方差,最大方差 int i; //循环变量 //int pixelR[256],pixelG[256],pixelB[256]; int pixel[256]={0}; //不要忘记初始化 for (int y=0; y<maxY-1; y++) { for (int x=0; x<maxX-1; x++) { temp=*(pRealData+pit*(y)+(x)*bitCount); if(bitCount==3) { tempR=*(pRealData+pit*(y)+(x)*bitCount); tempG=*(pRealData+pit*(y)+(x)*bitCount+1); tempB=*(pRealData+pit*(y)+(x)*bitCount+2); temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2); //temp=(int)((tempR+tempG+tempB)/3); } *(pRealData2+pit2*(y)+(x)*bitCount2)=temp; *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp; *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp; } } for (int y=0; y<maxY; y++) { for (int x=0; x<maxX; x++) { temp=*(pRealData2+pit2*(y)+(x)*bitCount2); pixel[temp]++; } } //CString str; //str.Format(TEXT("%d"),pixel[3]); //MessageBox(str); for(optIndex=0;optIndex<256;optIndex++) { u0=0; sum0=0; for(i=0;i<=optIndex;i++) { u0+=i*pixel[i]; sum0+=pixel[i]; } u0/=sum0; w0=(float)(sum0)/(maxX*maxY); u1=0; sum1=0; for(i=optIndex;i<=255;i++) { u1+=i*pixel[i]; sum1+=pixel[i]; } u1/=sum1; w1=1-w0; fVaria=w0*w1*(u0-u1)*(u0-u1); if(fVaria>fMaxVaria) { fMaxVaria=fVaria; optT=optIndex; } } for (int y=0; y<maxY; y++) { for (int x=0; x<maxX; x++) { temp=*(pRealData2+pit2*(y)+(x)*bitCount2); if(temp>optT) tempR=255; else tempR=0; tempG=tempR; tempB=tempR; *(pRealData2+pit2*y+x*bitCount2)=tempR; *(pRealData2+pit2*y+x*bitCount2+1)=tempG; *(pRealData2+pit2*y+x*bitCount2+2)=tempB; } } Invalidate(); }
相关文章推荐
- 最大方差阈值分割(vc实现)
- OpenCV-最大类间方差(大津法OTSU)实现
- 二维最大类间方差(otsu)matlab实现源码
- 自适应阈值分割(vc实现)
- MATLAB实现最大类间方差算法
- 利用最大熵进行阈值分割从而实现灰度图像的二值化的原理概要及OpenCV代码
- 自适应阈值分割(vc实现)
- [开发日记]图:图片抽奖软件的原型设想及界面设计-PowerPoint与Vc++完美集成实现 (进展三)-2011年1月3日修订
- 利用VC++实现WIN95/NT下位图淡入淡出的二种技巧
- 在VC++中实现让程序只运行一个实例的方法
- [精通WindowsSocket网络开发-基于VC++实现]第三章——WindowsSockets基础—概念
- VC实现对话框客户区透明!
- VC++ 实现CEdit编辑框输入小数!
- 实用算法实现-第 25 篇 最大公约数
- vc++如何实现远程调试
- VC++ WIN32 sdk实现按钮自绘详解 之二.
- 利用《双缓冲技术及其在VC的GDI环境下的实现》理解armv7计数器中的双缓冲技术
- RSA在Android、vc、java下加解密实现互通。
- [VC/MFC]使用CTabCtrl控件实现属性页功能
- 应用层vc实现三种文件监视方法