// ======================================== Graph Extensions ======================================== void testGraphExtension(CGraphExt& graphExt, CGraph& graph) { const byte nStates = graph.getNumStates(); Size graphSize = Size(random::u<int>(10, 100), random::u<int>(10, 100)); graphExt.buildGraph(graphSize); ASSERT_EQ(graphSize, graphExt.getSize()); ASSERT_EQ(graphSize.width * graphSize.height, graph.getNumNodes()); graphSize = Size(random::u<int>(10, 100), random::u<int>(10, 100)); Mat pots = random::U(graphSize, CV_32FC(nStates)); graphExt.setGraph(pots); ASSERT_EQ(graphSize, graphExt.getSize()); Mat test_pots; graph.getNodes(0, 0, test_pots); test_pots = test_pots.clone().reshape(graph.getNumStates(), graphSize.height); ASSERT_EQ(pots.rows, test_pots.rows); for (int y = 0; y < test_pots.rows; y++) { float *pPots = pots.ptr<float>(y); float *pTestPots = test_pots.ptr<float>(y); for (int x = 0; x < test_pots.cols; x++) for (int c = 0; c < test_pots.channels(); c++) ASSERT_EQ(pPots[x * nStates + c], pTestPots[x * nStates + c]); } // void addDefaultEdgesModel(float val, float weight = 1.0f); // void addDefaultEdgesModel(const Mat &featureVectors, float val, float weight); // void addDefaultEdgesModel(const vec_mat_t &featureVectors, float val, float weight); }
// ======================================== CGraph Building ======================================== void testGraphBuilding(CGraph& graph, byte nStates) { ASSERT_EQ(nStates, graph.getNumStates()); int nNodes = random::u<int>(100, 100000); Mat pots1 = random::U(Size(nStates, nNodes), CV_32FC1, 0.0, 100.0); ASSERT_EQ(0, graph.addNode()); ASSERT_EQ(1, graph.addNode(pots1.row(1).t())); graph.addNodes(pots1); ASSERT_EQ(nNodes + 2, graph.getNumNodes()); graph.setNode(0, pots1.row(0).t()); Mat pot0, pot1, pot2; graph.getNode(0, pot0); graph.getNode(1, pot1); graph.getNodes(0, 10, pot2); float *pPot0 = pots1.ptr<float>(0); float *pPot1 = pots1.ptr<float>(1); for (byte s = 0; s < nStates; s++) { ASSERT_EQ(pot0.at<float>(s, 0), pPot0[s]); ASSERT_EQ(pot1.at<float>(s, 0), pPot1[s]); ASSERT_EQ(pot2.at<float>(0, s), pPot0[s]); ASSERT_EQ(pot2.at<float>(1, s), pPot1[s]); } Mat pots2 = random::U(Size(nStates, static_cast<int>(graph.getNumNodes()) - 10), CV_32FC1, 0.0, 100.0); graph.setNodes(2, pots2); Mat pot; for (size_t n = 2; n < graph.getNumNodes(); n++) { graph.getNode(n, pot); float *pPot = (static_cast<int>(n) - 2 < pots2.rows) ? pots2.ptr<float>(static_cast<int>(n) - 2) : pots1.ptr<float>(static_cast<int>(n) - 2); for (byte s = 0; s < nStates; s++) ASSERT_EQ(pot.at<float>(s, 0), pPot[s]); } graph.reset(); ASSERT_EQ(graph.getNumEdges(), 0); }