⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autothreshhold.cpp

📁 Otsu算法(灰度图像的域值分割)的C++类实现.
💻 CPP
字号:
// AutoThreshhold.cpp: implementation of the CAutoThreshhold class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FaceRecognition.h"
#include "AutoThreshhold.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAutoThreshhold::CAutoThreshhold()
{
}

CAutoThreshhold::~CAutoThreshhold()
{

}

BOOL CAutoThreshhold::LoadImage(char *filename)
{
	return TRUE;
}

BOOL CAutoThreshhold::LoadImage(BYTE *Array, long width, long height)
{
	if (!Array)
		return FALSE;
	m_Array=(BYTE*)new BYTE[width*height];
	for (long i=0;i<width*height;i++)
		m_Array[i]=Array[i];
	m_Height=height;
	m_Width=width;
	return TRUE;
}

BYTE CAutoThreshhold::CalcBestThreshold()
{
	if (!m_Array)
		return 0;
	GetGrayTable();
	GetPiTable();
	int i,Maxi;
	double max=0;
	for (i=0;i<256;i++)
	{
		GetUk(i);
		deltak[i]=(u*wk0-uk0)*(u*wk0-uk0)/(wk0*wk1);
		if (deltak[i]>=max)
		{
			max=deltak[i];
			Maxi=i;
		}
	}
	return Maxi;
}

void CAutoThreshhold::GetGrayTable()
{
	for (long i=0;i<256;i++)
		Gray[i]=0;
	for (i=0;i<m_Height*m_Width;i++)
		Gray[m_Array[i]]++;
}

void CAutoThreshhold::GetPiTable()
{
	u=0;
	for (long i=0;i<256;i++)
	{
		Pi[i]=(double)Gray[i]/(double)(m_Height*m_Width);
		u+=i*Pi[i];
	}
}

void CAutoThreshhold::GetUk(int k)
{
	uk0=0;
	wk0=0;
	for (long i=0;i<=k;i++)
	{
		uk0+=i*Pi[i];
		wk0+=Pi[i];
	}
	uk1=u-uk0;
	wk1=1-wk0;
}

void CAutoThreshhold::Destroy()
{
	if (m_Array)
	{
		delete [] m_Array;
		m_Array=0;
	}
	m_Height=0;
	m_Width=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -