/* VQ.cpp Written by: Michael Götting (2007) University of Bielefeld AG Neuroinformatik mgoettin@techfak.uni-bielefeld.de */ #include "iclVQ.h" #include "iclStackTimer.h" #include "iclMathematics.h" #include using namespace std; using namespace icl; namespace icl { // {{{ Constructor/ Destructor template class U> VQ::VQ(const ImgBase *poSrc, float fLearnRate) : m_fLearnRate(fLearnRate) { FUNCTION_LOG(""); m_poData = new U(poSrc); m_vecRefDataPtr = m_poData->getDataPtr(); m_uiVQDim = m_poData->getDim(); m_uiSrcDim = poSrc->getDim(); m_bClusterIsInitialized = false; m_uiMaxTrainSteps = 3000; } template class U> VQ::VQ(unsigned int uiVQDim, float fLearnRate) : m_fLearnRate(fLearnRate), m_uiVQDim(uiVQDim) { FUNCTION_LOG(""); m_poData = new U(); m_bClusterIsInitialized = false; m_uiMaxTrainSteps=3000; } template class U> VQ::~VQ() { FUNCTION_LOG(""); } // }}} // {{{ cluster functions template class U> void VQ::createCluster(unsigned int uiCenter) { FUNCTION_LOG(""); // store variables m_uiCenter = uiCenter; // allocate memory for cluster m_vecCluster.resize(m_uiCenter); for (unsigned int i=0;i class U> void VQ::resizeCluster(unsigned int uiCenter) { FUNCTION_LOG(""); m_vecCluster.clear(); m_vecClusterInfo.clear(); createCluster(uiCenter); } template class U> void VQ::clearCluster() { FUNCTION_LOG(""); for (unsigned int i=0;i class U> void VQ::initClusterFromSrc(const ImgBase *poSrc, vqinitmode eMode, unsigned int uiStart) { FUNCTION_LOG(""); // Variable initialisation static MathematicsRandomSeedInitializer initSeed; unsigned int uiRndPos; m_poData->setData(poSrc); m_vecRefDataPtr = m_poData->getDataPtr(); m_uiSrcDim = poSrc->getDim(); // Select initialisation mode switch(eMode) { case initRnd: for (unsigned int i=0;i class U> void VQ::vq(const ImgBase *poSrc, unsigned int uiDataIdx) { // {{{ open FUNCTION_LOG(""); // check pre start options ICLASSERT(m_bClusterIsInitialized); // Initialise data object m_poData->setData(poSrc); m_vecRefDataPtr = m_poData->getDataPtr(); // variable initilization unsigned int uiMinDistIdx; // The min distance codeword to the source data float fErrBuf=0; // The minimal distance // calc the euklidian distance to each codeword uiMinDistIdx = nn(uiDataIdx, fErrBuf); // update the codebook for (unsigned int i=0;i class U> void VQ::lbg() { // {{{ open FUNCTION_LOG(""); // check pre start options ICLASSERT(m_bClusterIsInitialized); // variable initilization unsigned int uiMinDistIdx; // The min distance codeword float fErrBuf=0; vector vecClustCnt(m_uiCenter); //cluster size counters vector > vecAccu(m_uiCenter); // data accumulators for (unsigned int i=0;i::max(); // calc the euklidian distance to each codeword LOOP_LOG("Center -> Distance"); for (unsigned int i=0;i "< class U> ImgBase* VQ::wta(bool bInking, ImgBase **dstImg) { // {{{ open FUNCTION_LOG(""); // variable deklaration ImgBase *oTmpImg; icl8u *dataPtr=0; float fMinDist = 0; int iPos, iWinnerCV = 0, iColorIdx=1; unsigned int uiSrcWidth = m_poData->m_poData->getWidth(); unsigned int uiSrcHeight = m_poData->m_poData->getHeight(); clearClusterInfo(); // Ink WTA map if (bInking) { iColorIdx = 255 / (m_uiCenter-1); } // deklare dstImg if (!dstImg) { oTmpImg = imgNew(depth8u, m_poData->m_poData->getSize(), formatGray); } else { ensureCompatible(dstImg, depth8u, m_poData->m_poData->getSize(), formatGray); oTmpImg = *dstImg; } dataPtr = (icl8u*) oTmpImg->getDataPtr(0); // Compute WTA for(unsigned int y=0;y class U> void VQ::printCluster() { // {{{ open FUNCTION_LOG(""); // calc center with minimal distance printf("Cluster data\n"); printf("------------\n"); for (unsigned int i=0;i class U> float VQ::discriminantAnalysis(const ImgBase *clusterImg) { // {{{ open FUNCTION_LOG(""); // variable deklaration ImgBase *grayImg = imgNew(depth8u, m_poData->m_poData->getSize(), formatGray); vector<_ClProp> vecClProp(m_uiVQDim); vector fMeanImg; vector vecSelChannel; float fIntraClassVar=0, fInterClassVar=0; float fTmp; icl8u *clusterPtr = (icl8u*) clusterImg->getDataPtr(0); ImgBase *rgbImg2=0; // Compute gray src image vecSelChannel.push_back(0); vecSelChannel.push_back(1); vecSelChannel.push_back(2); const ImgBase *rgbImg = m_poData->getSrcImg()->selectChannels(vecSelChannel); rgbImg2 = const_cast(rgbImg); rgbImg2->setFormat(formatRGB); rgbImg2->normalizeImg(Range(0,255)); cc(rgbImg2, grayImg); icl8u *grayPtr = (icl8u*) grayImg->getDataPtr(0); // Compute cluster properties for (int i=0;igetDim();i++) { vecClProp[clusterPtr[i]].pixSum += grayPtr[i]; vecClProp[clusterPtr[i]].uiClSize++; } for (unsigned int i=0;igetDim();i++) { fTmp = grayPtr[i] - vecClProp[clusterPtr[i]].fMean; vecClProp[clusterPtr[i]].fIntraVar += fTmp * fTmp; } for (unsigned int i=0;i class U> void VQ::clearClusterInfo() { // {{{ open for (unsigned int i=0;i; template class VQ; template class VQ; }