?? fuzzyregionlabel.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;
/*釋放所有標記位置的鏈表所占用的內(nèi)存*/
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;
/*頭節(jié)點不存儲任何實際的連通區(qū)域信息*/
m_pFrstLabel = m_pThisLabel = new LabelLocation;
m_pThisLabel->snStartX = -1;
m_pThisLabel->snEndX = -1;
m_pThisLabel->snHeight = -1;
m_pThisLabel->pLabelNext = NULL;
/*頭節(jié)點不存儲任何最小外接矩形信息*/
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;
/*置連通區(qū)域的初始標記*/
m_byLabelVal = FORE_GROUND_3;
/*記錄連通區(qū)域最小外接矩形的開始地址*/
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;
//獲得記錄本連通區(qū)域位置的首指針
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 )//噪音區(qū)域,消除之
{
//消除該標記
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -