【 ORB_SLAM2源码解读】特征点均匀化之四叉树节点分裂(8)

图文讲解

    // --> 添加微信 slamshizhanjiaocheng 邀请加入微信交流群
    // 这部分代码主要是将节点划分为四部分(四叉树), 并且计算每部分的左上右下点的坐标, 然后根据坐标将特征点存储到对应的节点中
    void ExtractorNode::DivideNode(ExtractorNode &n1, ExtractorNode &n2,
                                   ExtractorNode &n3, ExtractorNode &n4) {

        const int halfX = ceil(static_cast<float>(UR.x - UL.x) / 2);// 180
        const int halfY = ceil(static_cast<float>(BR.y - UL.y) / 2);// 224

        // Define boundaries of childs
        n1.UL = UL;
        n1.UR = cv::Point2i(UL.x + halfX, UL.y);
        n1.BL = cv::Point2i(UL.x, UL.y + halfY);
        n1.BR = cv::Point2i(UL.x + halfX, UL.y + halfY);
        n1.vKeys.reserve(vKeys.size());
        // --> 添加微信 slamshizhanjiaocheng 邀请加入微信交流群
        // (UL.x, UL.y)= (0, 0)   . . . . .(UR.x, UR.y)= (360, 0) (UR.x, UR.y)= (360, 0)    . . . . .(UR.x, UR.y)= (720, 0)
        //                         . . . . .                                                    . . . . .
        //                         . . . . .                                                    . . . . .
        // (BL.x, BL.y)= (0, 448) . . . . .(BR.x, BR.y)= (360, 448)(BR.x, BR.y)= (360, 448) . . . . .(BR.x, BR.y)= (720, 448)

        // 每个节点的起始点坐标和终止点坐标  --> 添加微信 slamshizhanjiaocheng 邀请加入微信交流群
        // UL: [0, 0]   UR: [180, 0]   UL: [180, 0]   UR: [360, 0]     UL: [360, 0] UR: [540, 0]     UL: [540, 0] UR: [720, 0]
        // BL: [0, 224] BR: [180, 224] BL: [180, 224] BR: [360, 224] BL: [360, 224] BR: [540, 224] BL: [540, 224] BR: [720, 224]
        // UL: [0, 224] UR: [180, 224] UL: [180, 224] UR: [360, 224] UL: [360, 224] UR: [540, 224] UL: [540, 224] UR: [720, 224]
        // BL: [0, 448] BR: [180, 448] BL: [180, 448] BR: [360, 448] BL: [360, 448] BR: [540, 448] BL: [540, 448] BR: [720, 448]
        std::cout << "n1:\nUL: " << n1.UL << " UR: " << n1.UR << "\nBL: " << n1.BL << " BR: " << n1.BR  << std::endl;

        n2.UL = n1.UR;
        n2.UR = UR;
        n2.BL = n1.BR;
        n2.BR = cv::Point2i(UR.x, UL.y + halfY);
        n2.vKeys.reserve(vKeys.size());

        std::cout << "n2:\nUL: " << n2.UL << " UR: " << n2.UR << "\nBL: " << n2.BL << " BR: " << n2.BR  << std::endl;


        n3.UL = n1.BL;
        n3.UR = n1.BR;
        n3.BL = BL;
        n3.BR = cv::Point2i(n1.BR.x, BL.y);
        n3.vKeys.reserve(vKeys.size());
        std::cout << "n3:\nUL: " << n3.UL << " UR: " << n3.UR << "\nBL: " << n3.BL << " BR: " << n3.BR  << std::endl;

        n4.UL = n3.UR;
        n4.UR = n2.BR;
        n4.BL = n3.BR;
        n4.BR = BR;
        n4.vKeys.reserve(vKeys.size());
        std::cout << "n4:\nUL: " << n4.UL << " UR: " << n4.UR << "\nBL: " << n4.BL << " BR: " << n4.BR  << std::endl;

        // Associate points to childs --> 添加微信 slamshizhanjiaocheng 邀请加入微信交流群
        // 这部分是根据特征点的坐标, 将特征点存储到对应的子节点里面的vKeys中, 循环次数是父亲节点存储的特征点数目

        // (UL.x, UL.y)= (0, 0)   . . . . .(UR.x, UR.y)= (360, 0) (UR.x, UR.y)= (360, 0)    . . . . .(UR.x, UR.y)= (720, 0)
        //                         . . . . .                                                    . . . . .
        //                         . . . . .                                                    . . . . .
        // (BL.x, BL.y)= (0, 448) . . . . .(BR.x, BR.y)= (360, 448)(BR.x, BR.y)= (360, 448) . . . . .(BR.x, BR.y)= (720, 448)

        // 每个节点的起始点坐标和终止点坐标  --> 添加微信 slamshizhanjiaocheng 邀请加入微信交流群
        // UL: [0, 0]   UR: [180, 0]   UL: [180, 0]   UR: [360, 0]     UL: [360, 0] UR: [540, 0]     UL: [540, 0] UR: [720, 0]
        // BL: [0, 224] BR: [180, 224] BL: [180, 224] BR: [360, 224] BL: [360, 224] BR: [540, 224] BL: [540, 224] BR: [720, 224]
        // UL: [0, 224] UR: [180, 224] UL: [180, 224] UR: [360, 224] UL: [360, 224] UR: [540, 224] UL: [540, 224] UR: [720, 224]
        // BL: [0, 448] BR: [180, 448] BL: [180, 448] BR: [360, 448] BL: [360, 448] BR: [540, 448] BL: [540, 448] BR: [720, 448]

        for (size_t i = 0; i < vKeys.size(); i++) {
            const cv::KeyPoint &kp = vKeys[i];
            if (kp.pt.x < n1.UR.x) {
                if (kp.pt.y < n1.BR.y)
                    n1.vKeys.push_back(kp);// 左上
                else
                    n3.vKeys.push_back(kp);// 左下
            } else if (kp.pt.y < n1.BR.y)
                n2.vKeys.push_back(kp);// 右上
            else
                n4.vKeys.push_back(kp);// 右下
        }

        if (n1.vKeys.size() == 1) n1.bNoMore = true;
        if (n2.vKeys.size() == 1) n2.bNoMore = true;
        if (n3.vKeys.size() == 1) n3.bNoMore = true;
        if (n4.vKeys.size() == 1) n4.bNoMore = true;
    }

视频讲解

你可能感兴趣的:(从零开始学习SLAM,ORB_SLAM2,ORB_SLAM3)