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

📄 fuzzyregionlabel.cpp

📁 基于模糊理论的连通区域标记算法
💻 CPP
字号:
// FuzzyLabel.cpp: implementation of the CFuzzyLabel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "APCount.h"
#include "FuzzyLabel.h"

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

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

CFuzzyLabel::CFuzzyLabel()
{
	m_pSrcBits = NULL;

	m_pFrstLabel = m_pThisLabel = NULL;
	m_pFrstRect  = m_pThisRect  = NULL;

	m_snImageWidth = m_snRowBytes = m_snImageHeight = 0;
	m_bInitStatus  = FALSE;
}


CFuzzyLabel::~CFuzzyLabel()
{
	DeleteMem();
}


void CFuzzyLabel::DeleteMem( )
{
	m_pSrcBits = NULL;

	/*释放所有标记位置的链表所占用的内存*/
	m_pThisLabel = m_pFrstLabel;
	while ( m_pThisLabel != NULL )
	{
		m_pFrstLabel = m_pThisLabel;
	    m_pThisLabel = m_pThisLabel->pLabelNext;
		if ( m_pFrstLabel != NULL )
		{
			delete[] m_pFrstLabel;
		}
	}
	m_pFrstLabel = m_pThisLabel = NULL;

	m_pThisRect = m_pFrstRect;
	while ( m_pThisRect != NULL )
	{
		m_pFrstRect = m_pThisRect;
	    m_pThisRect = m_pThisRect->pRectNext;
		if ( m_pFrstRect != NULL )
		{
			delete[] m_pFrstRect;
		}
	}
	m_pFrstRect = m_pThisRect = NULL;
}


void CFuzzyLabel::ParamInit( short snImageWidth, short snImageHeight )
{
	if ( m_bInitStatus == FALSE )
	{
		DeleteMem();

		m_snImageWidth  = snImageWidth;
		m_snRowBytes    = snImageWidth * ONE_BYTES;
		m_snImageHeight = snImageHeight;

		m_pSrcBits = NULL;

		/*头节点不存储任何实际的连通区域信息*/
		m_pFrstLabel = m_pThisLabel = new LabelLocation;
		m_pThisLabel->snStartX   = -1;
		m_pThisLabel->snEndX     = -1;
		m_pThisLabel->snHeight   = -1;
		m_pThisLabel->pLabelNext = NULL;

		/*头节点不存储任何最小外接矩形信息*/
		m_pFrstRect = m_pThisRect = new LabelRect;
		m_pThisRect->snLeftX   = -1;
		m_pThisRect->snRightX  = -1;
		m_pThisRect->snBottomY = -1;
		m_pThisRect->snTopY    = -1;
		m_pThisRect->pRectNext = NULL;
		
		m_bInitStatus = TRUE;
	}
}


/*标记连通的边缘*/
void CFuzzyLabel::ConnxyLabel_Edge( LPBYTE lpSrcBits )
{
	m_pSrcBits = lpSrcBits;
	/*置连通区域的初始标记*/
	m_byLabelVal = FORE_GROUND_3;

	/*记录连通区域最小外接矩形的开始地址*/
	m_pThisRect = m_pFrstRect;
	
	LPBYTE pSrc = lpSrcBits;
	short  dwStartX  = 0,
		   dwWidth   = 0;
	BYTE   byThisVal = 0;
	
	for ( short h = 0; h < m_snImageHeight; ++ h )
	{
		for ( short w = 0; w < m_snRowBytes; NULL )
		{
			switch ( pSrc[ w ] )
			{
			case FORE_GROUND_1:
				for ( dwStartX = w, w += ONE_BYTES;
				      ( FORE_GROUND_1 == pSrc[ w ] ) && ( w < m_snRowBytes );
					  w += ONE_BYTES );
				EdgeProc( dwStartX, w, h, FORE_GROUND_1 );

				break;

			case FORE_GROUND_2:
				for ( dwStartX = w, w += ONE_BYTES;
				      ( FORE_GROUND_2 == pSrc[ w ] ) && ( w < m_snRowBytes );
					  w += ONE_BYTES );
				EdgeProc( dwStartX, w, h, FORE_GROUND_2 );

				break;

			case BACK_GROUND:
				for ( dwStartX = w, w += ONE_BYTES;
				      ( BACK_GROUND == pSrc[ w ] ) && ( w < m_snRowBytes );
					  w += ONE_BYTES );

				break;

			default:
				byThisVal = pSrc[ w ];
				for ( w += ONE_BYTES;
				      ( byThisVal == pSrc[ w ] ) && ( w < m_snRowBytes );
					  w += ONE_BYTES );

				break;
			}
		}

		pSrc += m_snRowBytes;
	}

	m_pSrcBits = NULL;
}


/*连通的边缘处理过程*/
void  CFuzzyLabel::EdgeProc( short dwStartX, short dwEndX,
						     short dwHeight, BYTE byForeType )
{
	if ( m_byLabelVal < 254 )
	{
		++m_byLabelVal;
	}
	else
	{
		m_byLabelVal = FORE_GROUND_3 + 1;
	}

	m_dwLabelStat   = 0;
	m_dwLabelStartX = dwStartX;
	m_dwLabelEndX   = dwEndX;
	m_dwLabelStartY = dwHeight;
	m_dwLabelEndY   = dwHeight + 1;
	//获得记录本连通区域位置的首指针
	m_pThisLabel = m_pFrstLabel;

	//开始进行递归标记
	switch ( byForeType )
	{
	case FORE_GROUND_1:
		RecurLabel_1( dwStartX, dwEndX, dwHeight );
		break;
	
	case FORE_GROUND_2:
		RecurLabel_2( dwStartX, dwEndX, dwHeight );
		break;
	
	default:
		break;
	}

	if ( m_dwLabelStat < EDGE_THRESHOLD )//噪音区域,消除之
	{
		//消除该标记
    	DeleteLabel( );
		if ( m_byLabelVal == FORE_GROUND_3 + 1 )
		{
			m_byLabelVal = 254;
		}
		else
		{
			--m_byLabelVal;
		}
	}
	else
	{
		//保留其最小外接矩形信息
		if ( m_pThisRect->pRectNext != NULL )
		{
			m_pThisRect = m_pThisRect->pRectNext;
		}
		else
		{
			m_pThisRect->pRectNext = new LabelRect;
			m_pThisRect->pRectNext->pRectNext = NULL;
			m_pThisRect = m_pThisRect->pRectNext;
		}
		m_pThisRect->snLeftX   = m_dwLabelStartX;
		m_pThisRect->snRightX  = m_dwLabelEndX;
		m_pThisRect->snBottomY = m_dwLabelStartY;
		m_pThisRect->snTopY    = m_dwLabelEndY;
	}
}

⌨️ 快捷键说明

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