📄 rxyconnectedcomponent2d.cpp
字号:
// RxyConnectedComponent2D.cpp: implementation of the RxyConnectedComponent2D class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "RxyConnectedComponent2D.h"#include "ProgressWnd.h"#include "fusionglobal.h" // for g_outFile#define BKValue 0 // background value//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////RxyConnectedComponent2D::RxyConnectedComponent2D(){}RxyConnectedComponent2D::~RxyConnectedComponent2D(){}void RxyConnectedComponent2D::ControlProgress(RxProgressWnd *pProWnd){ if(pProWnd && pProWnd->Cancelled()){ delete pProWnd; pProWnd = NULL; return; } else if(pProWnd){ pProWnd->StepIt(); pProWnd->PeekAndPump(); }}// for 2D Image "pMap"int RxyConnectedComponent2D::GetConnectedComponent(unsigned char *pMap, int nWidth, int nHeight, int nLargeThreshold){ unsigned int iImageSize = nWidth*nHeight; unsigned short *pTempMap = (unsigned short *)GlobalAlloc(GMEM_FIXED, iImageSize*sizeof(unsigned short)); unsigned short *pTable = (unsigned short *)GlobalAlloc(GMEM_FIXED, iImageSize*sizeof(unsigned short)); memset(pTempMap, 0, iImageSize*sizeof(unsigned short)); memset(pTable, 0, iImageSize*sizeof(unsigned short)); for(int i=0;i<iImageSize;i++) { pTempMap[i] = pMap[i]; pTable[i] = i; } int nLabelCnt=1; for(int y=0; y<nHeight; y++) { int nRowOffset = y*nWidth; for(int x=0; x<nWidth; x++) { int nIdx = x + nRowOffset; if(pMap[nIdx]==BKValue ) continue; int nState = GetState(pTempMap, nWidth, nHeight, x, y); SetLabelByState(pTempMap, nWidth, nHeight, x, y, &nLabelCnt, nState, pTable); } } for(i=0;i<iImageSize;i++) pTable[i] = FindFinalLabel(pTable, i); int nMaxLabel = MapFillingWithSortedLabel(pTable, pTempMap, iImageSize); for(i=0;i<iImageSize;i++) { pMap[i] = pTempMap[i]; } // find the largest component /////////////////////////////////////////////////////////////////////////////////////////////// memset(pTable, 0, iImageSize*sizeof(unsigned short)); for( i=0; i < iImageSize; i++){ if(pTempMap[i]!=0 ) pTable[pTempMap[i]]++; } unsigned int maxValue1 = 0, maxIndex1 = 0; int nLargeComp = 0; for(i=0; i<nMaxLabel; i++) { if(pTable[i] > nLargeThreshold ) { nLargeComp++; TRACE(_T("pTable[i] is larger than threshold: %d, index: %d\n"), pTable[i], i); if(pTable[i] > maxValue1) { maxValue1 = pTable[i]; maxIndex1 = i; } } } TRACE(_T("maxIndex1 = %d, maxValue1 = %d\n"), maxIndex1, maxValue1 ); if(nLargeComp!=0) { for( i=0;i<iImageSize;i++) { if(pMap[i]==0x00) continue; if(pTempMap[i] == maxIndex1 ) { // 力老 奴 component pMap[i] = 0xff; } else { pMap[i] = 0; } } } GlobalFree((unsigned short *)pTempMap); GlobalFree((unsigned short *)pTable); return(nMaxLabel-1);}// for 2D Image "pMap"int RxyConnectedComponent2D::RemoveDiaphragm(unsigned char *pMap, int nWidth, int nHeight, int nLargeThreshold, RxProgressWnd *pWndProgress){ unsigned int iImageSize = nWidth*nHeight; unsigned short *pTempMap = (unsigned short *)GlobalAlloc(GMEM_FIXED, iImageSize*sizeof(unsigned short)); unsigned short *pTable = (unsigned short *)GlobalAlloc(GMEM_FIXED, iImageSize*sizeof(unsigned short)); memset(pTempMap, 0, iImageSize*sizeof(unsigned short)); memset(pTable, 0, iImageSize*sizeof(unsigned short)); ControlProgress(pWndProgress); for(int i=0;i<iImageSize;i++) { pTempMap[i] = pMap[i]; pTable[i] = i; } ControlProgress(pWndProgress); int nLabelCnt=1; for(int y=0; y<nHeight; y++) { int nRowOffset = y*nWidth; for(int x=0; x<nWidth; x++) { int nIdx = x + nRowOffset; if(pMap[nIdx]==BKValue ) continue; int nState = GetState(pTempMap, nWidth, nHeight, x, y); SetLabelByState(pTempMap, nWidth, nHeight, x, y, &nLabelCnt, nState, pTable); } } for(i=0;i<iImageSize;i++) pTable[i] = FindFinalLabel(pTable, i); ControlProgress(pWndProgress); int nMaxLabel = MapFillingWithSortedLabel(pTable, pTempMap, iImageSize); ControlProgress(pWndProgress); for(i=0;i<iImageSize;i++) { pMap[i] = pTempMap[i]; } // find the largest component -> diapharam(染拜阜) removal /////////////////////////////////////////////////////////////////////////////////////////////// memset(pTable, 0, iImageSize*sizeof(unsigned short)); for( i=0; i < iImageSize; i++){ if(pTempMap[i]!=0 ) pTable[pTempMap[i]]++; } unsigned int maxValue1 = 0, maxIndex1 = 0; int nLargeComp = 0; for(i=0; i<nMaxLabel; i++) { if(pTable[i] > nLargeThreshold ) { nLargeComp++; TRACE(_T("pTable[i] is larger than threshold: %d, index: %d\n"), pTable[i], i); if(pTable[i] > maxValue1) { maxValue1 = pTable[i]; maxIndex1 = i; } } } TRACE(_T("maxIndex1 = %d, maxValue1 = %d\n"), maxIndex1, maxValue1 ); ControlProgress(pWndProgress); int iIndexX, iIndexY; int iMaxX = 0, iMaxY = 0; int iMinX = 1000, iMinY = 1000; if(nLargeComp!=0) { for( i=0;i<iImageSize;i++) { if(pMap[i]==0x00) continue; if(pTempMap[i] == maxIndex1 ) { // 力老 奴 component iIndexX = i%nWidth; iIndexY = i/nWidth; if(iIndexX < iMinX) iMinX = iIndexX; if(iIndexY < iMinY) iMinY = iIndexY; if(iIndexX > iMaxX) iMaxX = iIndexX; if(iIndexY > iMaxY) iMaxY = iIndexY; } } float fCircularity = 0; int nLenX = iMaxX-iMinX, nLenY = iMaxY-iMinY; if( nLenX > nLenY) fCircularity = nLenY/(float)nLenX; else fCircularity = nLenX/(float)nLenY; TRACE(_T("circularity: %f\n"), fCircularity); int nDiameter = 5; if(fCircularity>0.6 && (nLenX>nDiameter && nLenY>nDiameter)) { for( i=0;i<iImageSize;i++) { if(pMap[i]==0x00) continue; if(pTempMap[i] == maxIndex1 ) // the largest circular component pMap[i] = 0x00; else pMap[i] = 0xff; } } } GlobalFree((unsigned short *)pTempMap); GlobalFree((unsigned short *)pTable); ControlProgress(pWndProgress); return(nMaxLabel-1);}int RxyConnectedComponent2D::FindFinalLabel(unsigned short *pTable, unsigned short nNowLabel){ if(pTable[nNowLabel]==nNowLabel) return(nNowLabel); else { return(FindFinalLabel(pTable, pTable[nNowLabel])); }}int RxyConnectedComponent2D::GetState(unsigned short *pMap, int nWidth, int nHeight, int x, int y){ int nIdx = x + y*nWidth; int nIdxXbefore = (x-1) + y*nWidth; int nIdxYbefore = x + (y-1)*nWidth; if(x==0 && y==0) return(COMP_NEW); else if(y==0 && pMap[nIdxXbefore]!=BKValue) return(COMP_X); else if(y==0 && pMap[nIdxXbefore]==BKValue) return(COMP_NEW); else if(x==0 && pMap[nIdxYbefore]!=BKValue) return(COMP_Y); else if(x==0 && pMap[nIdxYbefore]==BKValue) return(COMP_NEW); /* else if(pMap[nIdxXbefore]!=BKValue) return(COMP_X); else if(pMap[nIdxXbefore]==BKValue) return(COMP_NEW); else if(pMap[nIdxYbefore]!=BKValue) return(COMP_Y); else if(pMap[nIdxYbefore]==BKValue) return(COMP_NEW);*/ else if(pMap[nIdxXbefore]!=BKValue && pMap[nIdxYbefore]!=BKValue) { if(pMap[nIdxXbefore]==pMap[nIdxYbefore]) return(COMP_SAME); else if(pMap[nIdxXbefore]!=pMap[nIdxYbefore]) return(COMP_DIFF_XY); } else if(pMap[nIdxXbefore]!=BKValue ) return(COMP_X); else if(pMap[nIdxYbefore]!=BKValue ) return(COMP_Y); else return(COMP_NEW); return 0;}void RxyConnectedComponent2D::SetLabelByState(unsigned short *pMap, int nWidth, int nHeight, int x, int y, int *nLabel, int nState,unsigned short *pTable){ int nIdx = x+y*nWidth; int nMinTemp, nMaxTemp; int Min_TempFinalLabel, Max_TempFinalLabel; switch(nState){ case COMP_NONE : case COMP_NEW : pMap[nIdx] = (*nLabel)++; break; case COMP_X : pMap[nIdx] = pMap[nIdx-1]; break; case COMP_SAME : case COMP_Y : pMap[nIdx] = pMap[nIdx-nWidth]; break; case COMP_DIFF_XY : nMinTemp = min(pMap[nIdx-1], pMap[nIdx-nWidth]); nMaxTemp = max(pMap[nIdx-1], pMap[nIdx-nWidth]); Max_TempFinalLabel = FindFinalLabel(pTable, nMaxTemp); Min_TempFinalLabel = FindFinalLabel(pTable, nMinTemp); pTable[Max_TempFinalLabel] = pMap[nIdx] = Min_TempFinalLabel; break; }}int RxyConnectedComponent2D::MapFillingWithSortedLabel(unsigned short *pTable, unsigned short *pMap, int iImageSize){ unsigned char *pSortTable = (unsigned char *)GlobalAlloc(GMEM_FIXED, iImageSize*sizeof(unsigned char)); ZeroMemory(pSortTable , iImageSize*sizeof(unsigned char)); int nSortedCnt = 1, old = 0; for(int i=0; i<iImageSize; i++) { int nUnsortedLabel = pTable[pMap[i]]; pMap[i] = nUnsortedLabel; if( nUnsortedLabel!=0 && pSortTable[nUnsortedLabel] == 0) { //int diff = abs(old-nUnsortedLabel); //if(diff > 100) pSortTable[nUnsortedLabel] = nSortedCnt++; old = nUnsortedLabel; } } for(i=0; i<iImageSize; i++) { pMap[i] = pSortTable[pMap[i]]; } TRACE(_T("nSortedCnt=%d\n"), nSortedCnt); GlobalFree((unsigned char *)pSortTable); return(nSortedCnt);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -