double compareSpatiograms(const spatiogram &p, const spatiogram &q ){ CV_Assert(p.bins==q.bins); cv::Mat temp = cv::Mat::zeros(1, p.bins, CV_64FC1); cv::Mat w = cv::Mat::zeros(1, p.bins, CV_64FC1); for (int i=0; i<p.bins; i++){ // Means cv::Mat ub1(2,1,CV_64FC1); ub1.at<double>(0,0)=p.mu.at<double>(0,i); ub1.at<double>(1,0)=p.mu.at<double>(1,i); cv::Mat ub2(2,1,CV_64FC1); ub2.at<double>(0,0)=q.mu.at<double>(0,i); ub2.at<double>(1,0)=q.mu.at<double>(1,i); // covariances in matrix format and clipped to 1 cv::Mat cm1=cv::Mat::ones(2,2,CV_64FC1); cm1.at<double>(0,0) += p.cm.at<double>(0,i); cm1.at<double>(1,1) += p.cm.at<double>(1,i); cv::Mat cm2=cv::Mat::ones(2,2,CV_64FC1); cm2.at<double>(0,0) += q.cm.at<double>(0,i); cm2.at<double>(1,1) += q.cm.at<double>(1,i); // weightening factor cv::Mat invS = cm1.inv(cv::DECOMP_LU) + cm2.inv(cv::DECOMP_LU); cv::Mat diff = ub2-ub1; cv::Mat expo = (-0.5)*diff.t()*invS*diff; w.at<double>(0,i) = std::exp( expo.at<double>(0,0) ); // Bhattacharyya coefficient double rho = sqrt( p.cd.at<double>(0,i)*q.cd.at<double>(0,i) ); temp.at<double>(0,i) = (w.at<double>(0,i)) * rho; } return cv::sum(temp)[0]; }
TEST(BoundingBox, CoordinateSize){ Point lb(1,2,0); Point ub(2,4,0); BoundingBox bb1(lb, ub); EXPECT_EQ(2, bb1.sizeCoordX()); EXPECT_EQ(3, bb1.sizeCoordY()); EXPECT_EQ(1, bb1.sizeCoordZ()); Point lb2(1,2,0); Point ub2(2,4,0); BoundingBox bb2(lb, ub); // Intersection should return an bounding box equal to bb2 and bb1, // they are the same bb BoundingBox bbIntersection = bb2.intersection(bb1); EXPECT_TRUE( bbIntersection.getLb().getX() == bb2.getLb().getX() && bbIntersection.getLb().getY() == bb2.getLb().getY() && bbIntersection.getLb().getZ() == bb2.getLb().getZ()); EXPECT_TRUE( bbIntersection.getUb().getX() == bb2.getUb().getX() && bbIntersection.getUb().getY() == bb2.getUb().getY() && bbIntersection.getUb().getZ() == bb2.getUb().getZ()); Point lb3(2,3,0); Point ub3(2,4,0); BoundingBox bb3(lb3, ub3); bbIntersection = bb2.intersection(bb3); // lower bound is the value of of bb3 EXPECT_TRUE( bbIntersection.getLb().getX() == lb3.getX() && bbIntersection.getLb().getY() == lb3.getY() && bbIntersection.getLb().getZ() == lb3.getZ()); // upper bound should not change EXPECT_TRUE( bbIntersection.getUb().getX() == bb2.getUb().getX() && bbIntersection.getUb().getY() == bb2.getUb().getY() && bbIntersection.getUb().getZ() == bb2.getUb().getZ()); }
TEST(BoundingBox, NoIntersection){ Point lb(1,2,0), ub(2,4,0), lb2(3,5,0), ub2(4,6,0); BoundingBox bb1(lb, ub); BoundingBox bb2(lb2, ub2); // Intersection should return an bounding box equal to bb2 and bb1, // they are the same bb BoundingBox bbInter = bb2.intersection(bb1); EXPECT_FALSE( bb1.doesIntersect(bb2)); // Lower bound of intersection should contain greater values // among lower bound of both bounding boxes (lb2) EXPECT_TRUE( bbInter.getLb().getX() == lb2.getX() && bbInter.getLb().getY() == lb2.getY() && bbInter.getLb().getZ() == lb2.getZ()); // Upper bound of intersection should contain smaller values // among upper bound of both bounding boxes (ub) EXPECT_TRUE( bbInter.getUb().getX() == ub.getX() && bbInter.getUb().getY() == ub.getY() && bbInter.getUb().getZ() == ub.getZ()); }
//Construct the kdTree kdTree::kdTree(vector<vector<double>> points, const string& method, int cutdimension): splitMethod(method){ if (points.empty()) { kdTreeRoot = nullptr; return; } // Select intial splitting axis int dimension = (int)(points[0].size()); int axis = cutdimension % dimension; size_t lb1(0),lb2(0),ub1(0),ub2(0); size_t pivotLocation; size_t numpoints = points.size(); if(numpoints == 1) { kdTreeRoot = shared_ptr<kdTreeNode>(new kdTreeNode(points[0])); } else { sort(points.begin(), points.end(), sorter(axis)); if (method == "median") { pivotLocation = (size_t)(numpoints/2); } else if (method == "mean") { double mean(0.0); for (size_t i=0;i<numpoints;i++) { mean = mean + points[i][axis]; } mean =mean/(double)numpoints; for (size_t i=0;i<numpoints;i++) { if (points[i][axis]>= mean) { pivotLocation = i; break; } } } /* lb1 = 0; */ ub1 = pivotLocation; lb2 = pivotLocation +1; ub2 = (size_t)points.size(); // Recursively create Left SubTree unique_ptr<kdTree> left_child = move(unique_ptr<kdTree> (new kdTree( slice<vector<double>>(points,lb1,ub1), method,axis+1))); // Recursively create Right SubTree unique_ptr<kdTree> right_child = move(unique_ptr<kdTree> (new kdTree( slice<vector<double>>(points,lb2,ub2), method,axis+1))); kdTreeRoot = shared_ptr<kdTreeNode>( new kdTreeNode( points[pivotLocation], axis, right_child->kdTreeRoot, left_child->kdTreeRoot)); } }