#include "stdafx.h"
|
#include "BLOB_Tool.h"
|
|
CBlobTool::CBlobTool()
|
{
|
|
}
|
|
|
CBlobTool::~CBlobTool()
|
{
|
|
}
|
|
bool CBlobTool::BlobAnalysis(BYTE *pImageData, int nWidth, int nHeight, int nStep, ListEdgeBlobData &blobList, int nMergeRange)
|
{
|
if (pImageData==NULL) return false;
|
|
int nIndex;
|
int i, j, y, x;
|
ListEdgeBlobData pixelList;
|
|
for (i=0; i<nHeight; i++)
|
{
|
for (j=0; j<nWidth; j++)
|
{
|
nIndex = (i*nStep)+j;
|
if (pImageData[nIndex] == 255)
|
{
|
// push pixel
|
CEdgeBlobData *pPos = new CEdgeBlobData(j, i);
|
pixelList.push_back(pPos);
|
pImageData[nIndex] = 0;
|
|
int nPixelCount = 0;
|
int nTotalX = 0;
|
int nTotalY = 0;
|
int nMaxX, nMaxY;
|
int nMinX, nMinY;
|
|
nMinX = nMinY = INT_MAX;
|
nMaxX = nMaxY = INT_MIN;
|
|
ListEdgeBlobDataIt it;
|
while (pixelList.size()!=0)
|
{
|
// pop pixel
|
it = pixelList.begin();
|
CEdgeBlobData *pCurPos = *it;
|
|
x = (int)pCurPos->fCenterX;
|
y = (int)pCurPos->fCenterY;
|
|
int nTmpIndex;
|
for (int sy=(-nMergeRange); sy<(nMergeRange+1); sy++)
|
{
|
for (int sx=(-nMergeRange); sx<(nMergeRange+1); sx++)
|
{
|
if (((sx+x) > nWidth-1) || ((sx+x) < 0)) continue;
|
if (((sy+y) > nHeight-1) || ((sy+y) < 0)) continue;
|
if ((sx==0 && sy==0) || sx==sy || sx==(-sy)) continue;
|
|
nTmpIndex = ((y+sy)*nStep)+(x+sx);
|
|
if (pImageData[nTmpIndex] == 255)
|
{
|
// push pixel
|
CEdgeBlobData *pNode1 = new CEdgeBlobData(x+sx, y+sy);
|
pixelList.push_back(pNode1);
|
pImageData[nTmpIndex] = 0;
|
} // end if
|
} // end for sx
|
} // end for sy
|
|
// blob info
|
nMinX = (nMinX > x) ? x : nMinX;
|
nMinY = (nMinY > y) ? y : nMinY;
|
nMaxX = (nMaxX < x) ? x : nMaxX;
|
nMaxY = (nMaxY < y) ? y : nMaxY;
|
nTotalX += x;
|
nTotalY += y;
|
nPixelCount++;
|
|
// delete pixel
|
delete pCurPos;
|
pixelList.erase(it);
|
|
} // end while
|
pixelList.clear();
|
|
// push blob
|
CEdgeBlobData *pBlob = new CEdgeBlobData(nMinX, nMinY, nMaxX, nMaxY);
|
pBlob->fMassCenterX = float(nTotalX) / float(nPixelCount);
|
pBlob->fMassCenterY = float(nTotalY) / float(nPixelCount);
|
pBlob->nPixelCount = nPixelCount;
|
blobList.push_back(pBlob);
|
|
}
|
}
|
}
|
|
return true;
|
}
|