SEXP addNeighborhoodToImageHelper( SEXP r_antsimage, SEXP r_center, SEXP r_rad, SEXP r_vec) { typedef typename ImageType::Pointer ImagePointerType; const unsigned int ImageDimension = ImageType::ImageDimension; typedef float PixelType; typename ImageType::Pointer image = Rcpp::as< ImagePointerType >( r_antsimage ); Rcpp::NumericVector center( r_center ); Rcpp::NumericVector rad( r_rad ); Rcpp::NumericVector intvec( r_vec ); if ( center.size() != ImageDimension ) Rcpp::stop("addNeighborhoodToImageHelper dim error."); typename itk::NeighborhoodIterator<ImageType>::SizeType nSize; typename ImageType::IndexType ind; ind.Fill( 0 ); for ( unsigned int i=0; i<ImageDimension; i++ ) { nSize[i] = rad[i]; ind[i] = center[i]; // R coords to ITK } itk::NeighborhoodIterator<ImageType> nit( nSize, image, image->GetLargestPossibleRegion() ) ; // for each location in nitSearch, compute the correlation // of the intvec with the nit neighborhood nit.SetLocation( ind ); for( unsigned int i = 0; i < nit.Size(); i++ ) { typename ImageType::IndexType ind2 = nit.GetIndex(i); PixelType lval = image->GetPixel( ind2 ); image->SetPixel( ind2, lval + intvec[i] ); } return 0; }
bool zzhang::CBPDualSolver::Init() { srand(456); clock_t init_start = clock(); std::vector<std::vector<int> > TNodeToEclusterIdx(m_NodeSize); std::vector<int> HasSuperSet(m_EClusterVec.size()); std::map<std::pair<int, int>, bool> IsVisited; for (int i = 0; i < m_EClusterVec.size(); i++) { HasSuperSet[i] = 0; for (int j = 0; j < m_EClusterVec[i].size(); j++) { TNodeToEclusterIdx[m_EClusterVec[i][j]].push_back(i); } } for (int i = 0; i < m_NodeSize; i++) { for (int j = 0; j < TNodeToEclusterIdx[i].size(); j++) { int cj = TNodeToEclusterIdx[i][j]; for (int k = j + 1; k < TNodeToEclusterIdx[i].size(); k++) { int ck = TNodeToEclusterIdx[i][k]; if (cj == ck) continue; std::pair<int, int> currentjk(cj, ck); if (IsVisited.find(currentjk) != IsVisited.end()) continue; IsVisited[currentjk] = true; IsVisited[std::pair<int, int>(ck, cj)] = true; if (m_EClusterSet[cj].size() == m_EClusterSet[ck].size()) continue; if (m_EClusterSet[cj].size() < m_EClusterSet[ck].size()) { int temp = cj; cj = ck; ck = temp; } if (std::includes(m_EClusterSet[cj].begin(), m_EClusterSet[cj].end(), m_EClusterSet[ck].begin(), m_EClusterSet[ck].end())) { HasSuperSet[ck] = 1; } } } } IsVisited.clear(); int maxsize = HasSuperSet.size(); for (int i = 0; i < maxsize; i++) { if (HasSuperSet[i]) { m_SubEClusters[i].clear(); continue; } for (int ni = 0; ni < m_EClusterVec[i].size(); ni++) { int nidx = m_EClusterVec[i][ni]; for (int k = 0; k < TNodeToEclusterIdx[nidx].size(); k++) { int cj = i, ck = TNodeToEclusterIdx[nidx][k]; if (cj == ck) continue; if (HasSuperSet[ck]) continue; std::pair<int, int> currentjk(cj, ck); if (IsVisited.find(currentjk) != IsVisited.end()) continue; IsVisited[currentjk] = true; IsVisited[std::pair<int, int>(ck, cj)] = true; std::set<int> intersection; std::set_intersection(m_EClusterSet[cj].begin(), m_EClusterSet[cj].end(), m_EClusterSet[ck].begin(), m_EClusterSet[ck].end(), std::inserter(intersection, intersection.begin())); if (m_EClusterIdx.find(intersection) != m_EClusterIdx.end()) continue; std::vector<int> intvec(intersection.begin(), intersection.end()); AddECluster(intvec, -1); //std::cout << "Added New intersection\n"; for (int j = 0; j < intvec.size(); j++) { TNodeToEclusterIdx[intvec[j]].push_back( m_EClusterVec.size() - 1); } HasSuperSet.push_back(1); } } } IsVisited.clear(); for (int i = 0; i < m_EClusterSet.size(); i++) { if (HasSuperSet[i]) continue; std::map<int, bool> isvisited; for (int ni = 0; ni < m_EClusterSet[i].size(); ni++) { int nidx = m_EClusterVec[i][ni]; for (int k = 0; k < TNodeToEclusterIdx[nidx].size(); k++) { int cj = i, ck = TNodeToEclusterIdx[nidx][k]; if (cj == ck) continue; if (isvisited.find(ck) != isvisited.end()) continue; isvisited[ck] = true; if (m_EClusterSet[cj].size() == m_EClusterSet[ck].size()) continue; if (std::includes(m_EClusterSet[cj].begin(), m_EClusterSet[cj].end(), m_EClusterSet[ck].begin(), m_EClusterSet[ck].end())) { m_SubEClusters[cj].push_back(ck); } } } } clock_t time_explore_graph_end = clock(); // printf("Explore Graph Structure Spends: %f seconds\n", // (time_explore_graph_end - init_start) * 1.0 / CLOCKS_PER_SEC); CacheIndex(); m_Potentials.clear(); m_LocalMaximum = std::vector<int>(m_EClusterVec.size()); m_DecodedSolution = boost::shared_array<int>(new int[m_NodeSize]); m_NodeToECluIdx = TNodeToEclusterIdx; clock_t cache_idx_cvt = clock(); printf("Cache index converting table spends: %f seconds\n", (cache_idx_cvt - time_explore_graph_end) * 1.0 / CLOCKS_PER_SEC); return true; }
void SceneModeler::computeIndexSequence(){ setProgressText("compute index sequence"); rvector rawData(m_ptrData->getTrace()); int menge = 0; for(unsigned int i = 0;i < this->classes->size(); i++){ if((*this->classes)[i].size() != 0){ menge += 1; } } this->classTraces = new vector< Trace* >; this->classData = new vector< vector <double>* >(menge); this->transitionDataClasses = new vector< int >(Ub(this->m_ptrData->getTrace())+1); this->numStates = new int[this->numClasses]; this->classMap = new int[this->numClasses]; unsigned int count = 0; /*creating vectors containing: -a map that compensates the empty classes(this->classMap) -the specific class for each datapoint(this->transitionDataClasses)*/ for(unsigned int i = 0;i < this->classes->size(); i++){ if((*this->classes)[i].size() != 0){ this->classMap[i] = i-count; (*this->classData)[this->classMap[i]] = new vector<double>; for(unsigned int n = 0; n < (*this->classes)[i].size(); n++){ int ende = ((unsigned int) (*this->classes)[i][n] != this->sceneLimits->size()-1) ? ((int) (*this->sceneLimits)[((int) (*this->classes)[i][n])+1]) -1 : Ub(this->m_ptrData->getTrace());; for(int x = (*this->sceneLimits)[(*this->classes)[i][n]]; x <= ende; x++){ (*this->classData)[this->classMap[i]]->push_back(_double(rawData[x])); (*this->transitionDataClasses)[x] = i; } } double max = 0; double min = 0; for(unsigned int n = 0; n < (*this->classData)[this->classMap[i]]->size();n++){ min = (n == 0 || min > (*(*this->classData)[this->classMap[i]])[n]) ? (*(*this->classData)[this->classMap[i]])[n] : min; max = (max < (*(*this->classData)[this->classMap[i]])[n]) ? (*(*this->classData)[this->classMap[i]])[n] : max; } rvector dump(0,(*this->classData)[this->classMap[i]]->size()-1); for(unsigned int n=0;n< (*this->classData)[this->classMap[i]]->size();n++){ dump[n] = (*(*this->classData)[this->classMap[i]])[n]; } this->classTraces->push_back(new Trace(dump)); if( _double(this->classTraces->back()->getStandardDeviation()) == 0 ){ this->numStates[i] = 1; } else{ this->numStates[i] = (int) floor((max - min) / _double(this->classTraces->back()->getStandardDeviation())); } } else{ this->numStates[i] = 0; this->classMap[i] = -1; count++; } setProgressValue(41+ (int)( (double) (20.0 / this->classes->size()) * i)); } /*creating vector containing empty states*/ this->strippedStates = new vector< vector <int>* >(this->classes->size()); for(unsigned int i=0; i < this->classes->size();i++){ (*this->strippedStates)[i] = new vector<int>; if(this->classMap[i] != -1){ intvector intvec(0,Ub((*this->classTraces)[this->classMap[i]]->getTrace())); for(int c=0;c <= Ub(intvec);c++){ intvec[c] = this->getStateFromTrace((*(*this->classTraces)[this->classMap[i]]),(*this->classTraces)[this->classMap[i]]->getTrace()[c]); } rmatrix* tmp = new rmatrix(AbstractDiscreteSSMPModeler::computeTransition(intvec)); for(int n=0;n < this->numStates[i];n++){ real sum=0; for(int x=0;x < this->numStates[i];x++){ sum += (*tmp)[cxsc::Row(n)][x]; } if(((int) cxsc::_double(sum)) != 1){ (*this->strippedStates)[this->classMap[i]]->push_back(n); } } delete tmp; } setProgressValue(61 + (int)( (double) (20.0 / this->classes->size()) * i)); } /*creating intvector containing states for each datapoint*/ this->transitionDataStates = new intvector(0,Ub(m_ptrData->getTrace())); for(int i=0; i <= Ub(m_ptrData->getTrace());i++){ int stateBuff=0; for(int n=0;n < (*this->transitionDataClasses)[i];n++){ stateBuff += this->numStates[n]-(*this->strippedStates)[n]->size(); } (*this->transitionDataStates)[i] = (stateBuff+ this->getStateFromTrace( ((*(*this->classTraces)[this->classMap[(*this->transitionDataClasses)[i]]])) , m_ptrData->getTrace()[i] ) ); setProgressValue(81 + (int)( (double) ( 15.0 / Ub( m_ptrData->getTrace() )) * i)); } }
SEXP jointLabelFusionNeighborhoodSearchHelper( SEXP r_intvec, SEXP r_center, unsigned int rad, unsigned int radSearch, SEXP r_antsimage, SEXP r_antsimageseg) { unsigned int segval = 0; typedef typename ImageType::Pointer ImagePointerType; const unsigned int ImageDimension = ImageType::ImageDimension; typedef float PixelType; typename ImageType::Pointer image = Rcpp::as< ImagePointerType >( r_antsimage ); typename ImageType::Pointer imageseg = Rcpp::as< ImagePointerType >( r_antsimageseg ); Rcpp::NumericVector intvec( r_intvec ); Rcpp::NumericVector outvec = Rcpp::NumericVector( intvec.size(), Rcpp::NumericVector::get_na() ); Rcpp::NumericVector bestvec = Rcpp::NumericVector( intvec.size(), Rcpp::NumericVector::get_na() ); Rcpp::NumericVector outsegvec = Rcpp::NumericVector( intvec.size(), Rcpp::NumericVector::get_na() ); Rcpp::NumericVector bestsegvec = Rcpp::NumericVector( intvec.size(), Rcpp::NumericVector::get_na() ); Rcpp::NumericVector center( r_center ); if ( center.size() != ImageDimension ) Rcpp::stop("jointLabelFusionNeighborhoodSearchHelper dim error."); typename itk::NeighborhoodIterator<ImageType>::SizeType nSize; typename itk::NeighborhoodIterator<ImageType>::SizeType nSizeSearch; typename ImageType::IndexType ind; ind.Fill( 0 ); for ( unsigned int i=0; i<ImageDimension; i++ ) { nSize[i] = rad; nSizeSearch[i] = radSearch; ind[i] = center[i]; // R coords to ITK } itk::NeighborhoodIterator<ImageType> nit( nSize, image, image->GetLargestPossibleRegion() ) ; itk::NeighborhoodIterator<ImageType> nitSearch( nSizeSearch, image, image->GetLargestPossibleRegion() ) ; // for each location in nitSearch, compute the correlation // of the intvec with the nit neighborhood nitSearch.SetLocation( ind ); PixelType bestcor = 1.e11; PixelType bestsd = 0; PixelType bestmean = 0; for( unsigned int i = 0; i < nitSearch.Size(); i++ ) { typename ImageType::IndexType ind2 = nitSearch.GetIndex(i); nit.SetLocation( ind2 ); PixelType outmean = 0; PixelType outsd = 0; PixelType inmean = 0; PixelType insd = 0; for ( unsigned int i=0; i < intvec.size(); i++ ) { PixelType pix = image->GetPixel( nit.GetIndex(i) ); outvec[i] = pix; outsegvec[i] = imageseg->GetPixel( nit.GetIndex(i) ); outmean += pix; inmean += intvec[i]; } outmean /= ( static_cast<PixelType>(intvec.size()) ); inmean /= ( static_cast<PixelType>(intvec.size()) ); for ( unsigned int i=0; i < intvec.size(); i++ ) { // should use recursive formula in above loop outsd += ( outvec[i] - outmean ) * ( outvec[i] - outmean ); insd += ( intvec[i] - inmean ) * ( intvec[i] - inmean ); } outsd = sqrt( outsd ); insd = sqrt( insd ); PixelType sum_uv = 0; PixelType sum_psearch = 0; PixelType ssq_psearch = 0; unsigned int n = intvec.size(); for(unsigned int i = 0; i < n; i++) { PixelType v = intvec[i]; PixelType u = outvec[i]; sum_psearch += u; ssq_psearch += u * u; sum_uv += u * v; } PixelType var_u_unnorm = ssq_psearch - sum_psearch * sum_psearch / n; if(var_u_unnorm < 1.0e-6) var_u_unnorm = 1.0e-6; PixelType locor = 0; if ( sum_uv > 0 ) locor = ( -1.0 * (sum_uv * sum_uv) / var_u_unnorm ); else locor = ( sum_uv * sum_uv ) / var_u_unnorm; // - (\Sum u_i v_i)^2 / z, where z = sigma_v^2 * (n-1) // locor = locor / ( insd * outsd ); if ( locor < bestcor ) { segval = imageseg->GetPixel( ind2 ); for ( unsigned int i=0; i < intvec.size(); i++ ) { bestvec[i] = outvec[i]; bestsegvec[i] = outsegvec[i]; } bestcor = locor; bestsd = outsd; bestmean = outmean; } } return Rcpp::List::create( Rcpp::Named("segval") = segval, Rcpp::Named("values") = bestvec, Rcpp::Named("bestmean") = bestmean, Rcpp::Named("bestsd") = bestsd, Rcpp::Named("bestcor") = bestcor, Rcpp::Named("bestsegvec") = bestsegvec ); }