简体   繁体   English

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

[英]Delete matches in OpenCV (Keypoints and descriptors)

I want to check a scene image against two train images. 我想对照两个火车图像检查场景图像。 For that, I detect features and compute descriptors of both training images. 为此,我检测了特征并计算了两个训练图像的描述符。 Before detecting, computing and matching the scene image, I will delete all matches of train1 and train2. 在检测,计算和匹配场景图像之前,我将删除train1和train2的所有匹配项。 Because these matches won't facilitate the matching of the scene image with train1 and train2. 因为这些匹配将不便于将场景图像与train1和train2进行匹配。

So, I match train1 with train2 and get a vector of the matches with the trainIdx and queryIdx. 因此,我将train1与train2匹配,并使用trainIdx和queryIdx获得匹配的向量。 But how can I delete these matches in the keypoints-vector and the descriptor matrix of train1 and train2? 但是,如何删除关键点向量以及train1和train2的描述符矩阵中的这些匹配项?

Best regards, dwi 最好的问候,DWI

I would have done like below: 我会做如下:

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();
}

I hope this will help. 我希望这将有所帮助。

OK, like Micka suggested, I iterate over all keypoints and descriptors and add all of them, except the matches, in a new vector/matrix. 好的,就像Micka建议的那样,我遍历所有关键点和描述符,并将除匹配项外的所有关键点和描述符添加到新的向量/矩阵中。 There is no possibility to mark them unnecessary. 无法将它们标记为不必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM