删除OpenCV中的匹配项(关键点和描述符)

时间:2015-03-21 08:42:25

标签: c++ opencv matching

我想针对两张火车图像检查一个场景图像。 为此,我检测了两个训练图像的特征和计算描述符。 在检测,计算和匹配场景图像之前,我将删除train1和train2的所有匹配项。因为这些匹配不利于场景图像与train1和train2的匹配。

因此,我将train1与train2匹配,并获得与trainIdx和queryIdx匹配的向量。但是如何在关键点矢量和train1和train2的描述符矩阵中删除这些匹配?

祝你好运, DWI

2 个答案:

答案 0 :(得分:2)

我会像下面这样做:

std::vector<cv::KeyPoint> keypoints[2];
cv::Mat descriptor[2];
std::vector< cv::DMatch > matches;

/* 
      Write code to generate the keypoints, descriptors and matches here...
      keypoint[0] -> Train Image 1 keypoints
      keypoint[1] -> Train Image 2 keypoints
      descriptor[0] -> Train Image 1 descriptors
      descriptor[1] -> Train Image 2 descriptors
      matches -> matched between train image 1 and 2

*/

// Logic to keep unmatched keypoints and corresponding descriptors
for (int idx = 0; idx < 2; idx++) {
    std::vector<bool> isMatched(keypoints[idx].size(), false);

    // Mark all matched keypoint as true
    for (int i = 0; i < matches.size(); i++) {
        if (idx == 0) {
            isMatched[matches[i].queryIdx] = true;
        }
        else {
            isMatched[matches[i].trainIdx] = true;
        }
    }

    std::vector<cv::KeyPoint>::const_iterator itr = keypoints[idx].begin();

    // New descriptor length will be old descriptor length minus matched keypoints size
    int descriptor_length = keypoints[idx].size() - matches.size();

    // Create temporary descriptor of new descriptor length
    cv::Mat tempDescriptor(descriptor_length, descriptor[idx].cols, descriptor[idx].depth());
    int count = 0;
    for (int i = 0; i < isMatched.size(); i++) {
        // Remove matched keypoints
        if (isMatched[i] == true) {
            itr = keypoints[idx].erase(itr);
        }
        else {
            descriptor[idx].row(i).copyTo(tempDescriptor.row(count));
            itr++;
            count++;
        }
    }

    descriptor[idx].release();
    descriptor[idx] = tempDescriptor.clone();
}

我希望这会有所帮助。

答案 1 :(得分:0)

好的,就像Micka建议的那样,我迭代所有的关键点和描述符,并在新的向量/矩阵中添加除了匹配之外的所有关键点和描述符。 没有必要将它们标记为不必要。