void AdjacencyAnnotator::propagateLeafAdjacencies(Crag& crag) { _numAdded = 0; for (Crag::CragNode n : crag.nodes()) if (crag.isRootNode(n)) recurseAdjacencies(crag, n); if (optionPruneChildEdges) pruneChildEdges(crag); LOG_USER(adjacencyannotatorlog) << "added " << _numAdded << " super node adjacency edges" << std::endl; }
HammingLoss::HammingLoss( const Crag& crag, const BestEffort& bestEffort, int balance) : Loss(crag), _balance((balance == 2 && optionBalanceHammingLoss) || (balance == 1)) { constant = 0; for (Crag::CragNode n : crag.nodes()) if (isBestEffort(n, crag, bestEffort)) { if (_balance) UTIL_ASSERT(crag.isLeafNode(n));; node[n] = -1; constant++; } else { if (!_balance || crag.isLeafNode(n)) node[n] = 1; } for (Crag::CragEdge e : crag.edges()) if (isBestEffort(e, crag, bestEffort)) { if (_balance) UTIL_ASSERT(crag.isLeafEdge(e));; edge[e] = -1; constant++; } else { if (!_balance || crag.isLeafEdge(e)) edge[e] = 1; } if (_balance) propagateLeafLoss(crag); }
void SolutionImageWriter::write( const Crag& crag, const CragVolumes& volumes, const CragSolution& solution, const std::string& basename, bool boundary) { LOG_DEBUG(solutionimagewriterlog) << "storing solution in " << basename << std::endl; if (_volumesBB.isZero()) _volumesBB = volumes.getBoundingBox(); LOG_DEBUG(solutionimagewriterlog) << "using bounding box of " << _volumesBB << std::endl; util::point<float, 3> resolution; for (Crag::CragNode n : crag.nodes()) { if (!crag.isLeafNode(n)) continue; resolution = volumes[n]->getResolution(); break; } LOG_DEBUG(solutionimagewriterlog) << "using resolution of " << resolution << std::endl; // create a vigra multi-array large enough to hold all volumes vigra::MultiArray<3, float> components( vigra::Shape3( _volumesBB.width() /resolution.x(), _volumesBB.height()/resolution.y(), _volumesBB.depth() /resolution.z()), std::numeric_limits<int>::max()); // background for areas without candidates if (boundary) components = 0.25; else components = 0; for (Crag::CragNode n : crag.nodes()) { LOG_ALL(solutionimagewriterlog) << "drawing node " << crag.id(n) << std::endl; // draw only selected nodes if (!solution.selected(n)) continue; const util::point<float, 3>& volumeOffset = volumes[n]->getOffset(); const util::box<unsigned int, 3>& volumeDiscreteBB = volumes[n]->getDiscreteBoundingBox(); util::point<unsigned int, 3> begin = (volumeOffset - _volumesBB.min())/resolution; util::point<unsigned int, 3> end = begin + util::point<unsigned int, 3>( volumeDiscreteBB.width(), volumeDiscreteBB.height(), volumeDiscreteBB.depth()); LOG_ALL(solutionimagewriterlog) << "\toffset : " << volumeOffset << std::endl; LOG_ALL(solutionimagewriterlog) << "\tdiscrete bb : " << volumeDiscreteBB << std::endl; LOG_ALL(solutionimagewriterlog) << "\ttarget area : " << begin << " -- " << end << std::endl; // fill id of connected component vigra::combineTwoMultiArrays( volumes[n]->data(), components.subarray(TinyVector3UInt(&begin[0]),TinyVector3UInt(&end[0])), components.subarray(TinyVector3UInt(&begin[0]),TinyVector3UInt(&end[0])), vigra::functor::ifThenElse( vigra::functor::Arg1() == vigra::functor::Param(1), vigra::functor::Param(solution.label(n)), vigra::functor::Arg2() )); } if (boundary) { // gray boundary for all leaf nodes for (Crag::CragNode n : crag.nodes()) if (crag.isLeafNode(n)) drawBoundary(volumes, n, components, 0.5); // black boundary for all selected nodes for (Crag::CragNode n : crag.nodes()) if (solution.selected(n)) drawBoundary(volumes, n, components, 0); } if (components.shape(2) > 1) { boost::filesystem::create_directory(basename); for (unsigned int z = 0; z < components.shape(2); z++) { std::stringstream ss; ss << std::setw(4) << std::setfill('0') << z; vigra::exportImage( components.bind<2>(z), vigra::ImageExportInfo((basename + "/" + ss.str() + ".tif").c_str())); } } else { vigra::exportImage( components.bind<2>(0), vigra::ImageExportInfo((basename + ".tif").c_str())); } }
void closed_set_solver() { /** * Subsets: * n7 * / \ * / \ * / \ * n5 n6 * / \ / \ * n1 n2 n3 n4 * * Adjacencies: * n7 * * * d * n5--------n6 * \ / * * e \ / f * * / \ * n1---n2----n3---n4 * a b c */ Crag crag; Crag::CragNode n1 = crag.addNode(); Crag::CragNode n2 = crag.addNode(); Crag::CragNode n3 = crag.addNode(); Crag::CragNode n4 = crag.addNode(); Crag::CragNode n5 = crag.addNode(); Crag::CragNode n6 = crag.addNode(); Crag::CragNode n7 = crag.addNode(); crag.addSubsetArc(n1, n5); crag.addSubsetArc(n2, n5); crag.addSubsetArc(n3, n6); crag.addSubsetArc(n4, n6); crag.addSubsetArc(n5, n7); crag.addSubsetArc(n6, n7); Crag::CragEdge a = crag.addAdjacencyEdge(n1, n2); Crag::CragEdge b = crag.addAdjacencyEdge(n2, n3); Crag::CragEdge c = crag.addAdjacencyEdge(n3, n4); Crag::CragEdge d = crag.addAdjacencyEdge(n5, n6); Crag::CragEdge e = crag.addAdjacencyEdge(n5, n3); Crag::CragEdge f = crag.addAdjacencyEdge(n2, n6); ClosedSetSolver solver(crag); CragSolution x(crag); ClosedSetSolver::Status status; { Costs costs(crag); costs.node[n7] = -1; solver.setCosts(costs); status = solver.solve(x); BOOST_CHECK_EQUAL(status, ClosedSetSolver::SolutionFound); // everything should be turned on for (Crag::CragNode n : crag.nodes()) BOOST_CHECK(x.selected(n)); for (Crag::CragEdge e : crag.edges()) BOOST_CHECK(x.selected(e)); } { Costs costs(crag); costs.node[n7] = 1; costs.edge[d] = -1; solver.setCosts(costs); status = solver.solve(x); BOOST_CHECK_EQUAL(status, ClosedSetSolver::SolutionFound); // everything except n7 should be turned on for (Crag::CragNode n : crag.nodes()) BOOST_CHECK(n != n7 ? x.selected(n) : !x.selected(n)); for (Crag::CragEdge e : crag.edges()) BOOST_CHECK(x.selected(e)); } }
void crag_iterators() { Crag crag; int numNodes = 10; for (int i = 0; i < numNodes; i++) crag.addNode(); int numEdges = 0; for (int i = 0; i < numNodes; i++) for (int j = 0; j < numNodes; j++) if (rand() > RAND_MAX/2) { crag.addAdjacencyEdge( crag.nodeFromId(i), crag.nodeFromId(j)); numEdges++; } int numArcs = 0; for (int i = 0; i < numNodes; i += 5) { for (int j = i; j < i + 4; j++) { crag.addSubsetArc( crag.nodeFromId(j), crag.nodeFromId(j+1)); numArcs++; } } BOOST_CHECK_EQUAL(crag.nodes().size(), numNodes); BOOST_CHECK_EQUAL(crag.edges().size(), numEdges); BOOST_CHECK_EQUAL(crag.arcs().size(), numArcs); numNodes = 0; { //UTIL_TIME_SCOPE("crag node old-school iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::CragNodeIterator i = crag.nodes().begin(); i != crag.nodes().end(); i++) numNodes++; } { //UTIL_TIME_SCOPE("crag node iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::CragNode n : crag.nodes()) { dontWarnMeAboutUnusedVar(n); numNodes++; } } { //UTIL_TIME_SCOPE("lemon node iterator"); //for (int j = 0; j < 1e8; j++) for (Crag::NodeIt n(crag); n != lemon::INVALID; ++n) numNodes -= 2; } BOOST_CHECK_EQUAL(numNodes, 0); numEdges = 0; for (Crag::CragEdge e : crag.edges()) { dontWarnMeAboutUnusedVar(e); numEdges++; } for (Crag::EdgeIt e(crag); e != lemon::INVALID; ++e) numEdges--; BOOST_CHECK_EQUAL(numEdges, 0); numArcs = 0; for (Crag::CragArc a : crag.arcs()) { dontWarnMeAboutUnusedVar(a); numArcs++; } for (Crag::SubsetArcIt a(crag); a != lemon::INVALID; ++a) numArcs--; BOOST_CHECK_EQUAL(numArcs, 0); for (Crag::CragNode n : crag.nodes()) { int numAdjEdges = 0; for (Crag::IncEdgeIt e(crag, n); e != lemon::INVALID; ++e) numAdjEdges++; BOOST_CHECK_EQUAL(crag.adjEdges(n).size(), numAdjEdges); for (Crag::CragEdge e : crag.adjEdges(n)) { dontWarnMeAboutUnusedVar(e); numAdjEdges--; } BOOST_CHECK_EQUAL(numAdjEdges, 0); int numInArcs = 0; for (Crag::SubsetInArcIt a(crag, crag.toSubset(n)); a != lemon::INVALID; ++a) numInArcs++; BOOST_CHECK_EQUAL(numInArcs, crag.inArcs(n).size()); for (Crag::CragArc a : crag.inArcs(n)) { dontWarnMeAboutUnusedVar(a); numInArcs--; } BOOST_CHECK_EQUAL(numInArcs, 0); int numOutArcs = 0; for (Crag::SubsetOutArcIt a(crag, crag.toSubset(n)); a != lemon::INVALID; ++a) numOutArcs++; BOOST_CHECK_EQUAL(numOutArcs, crag.outArcs(n).size()); for (Crag::CragArc a : crag.outArcs(n)) { dontWarnMeAboutUnusedVar(a); numOutArcs--; } BOOST_CHECK_EQUAL(numOutArcs, 0); Crag::CragEdgeIterator cei = crag.edges().begin(); Crag::EdgeIt ei(crag); while (ei != lemon::INVALID) { BOOST_CHECK((Crag::RagType::Node)((*cei).u()) == crag.getAdjacencyGraph().u(ei)); BOOST_CHECK((Crag::RagType::Node)((*cei).v()) == crag.getAdjacencyGraph().v(ei)); ++cei; ++ei; } Crag::CragArcIterator cai = crag.arcs().begin(); Crag::SubsetArcIt ai(crag); while (ai != lemon::INVALID) { BOOST_CHECK(crag.toSubset((*cai).source()) == crag.getSubsetGraph().source(ai)); BOOST_CHECK(crag.toSubset((*cai).target()) == crag.getSubsetGraph().target(ai)); ++cai; ++ai; } } }