使用OpenCV Cuda ORB特征检测器

3
我有一个应用程序,我接收到一系列图像流,其中我想要监视一组感兴趣区域内检测到的特征。这是使用ORB检测器完成的。在第一张图片中,我使用检测器找到给定ROI的“参考”关键点和描述符。对于后续的图像,我为同一ROI找到“测试”关键点和描述符。然后,我使用knn匹配器在参考和测试描述符之间找到匹配项。最后,我尝试找到“最佳”匹配项,将相关的关键点添加到“匹配关键点”集合中,然后计算“匹配强度”。这个匹配强度旨在指示参考图像中找到的关键点与测试图像中的关键点匹配程度如何。
我有几个问题:
1-这是特征检测器的有效用法吗?我知道简单的模板匹配可能会给我类似的结果,但我希望避免光线微小变化的问题。
2-我是否正确筛选“好”的匹配项,然后是否正确关联了该匹配项的关键点?
3-我的代码似乎可以正常工作,但是,如果我尝试使用流的异步版本的OpenCV调用,我会得到一个异常:“函数cv :: cuda :: GpuMat :: setTo中的无效资源句柄” ,这发生在对ORB_Impl :: buildScalePyramids的调用中(从ORB_Impl :: detectAndComputeAsync调用)。请参见下面我的“NewFrame”函数的异步版本。这让我认为我没有正确设置所有内容。
这是我的代码:
void Matcher::Matcher()
{
    // create ORB detector and descriptor matcher
    m_b = cuda::ORB::create(500, 1.2f, 8, 31, 0, 2, 0, 31, 20, true);   
    m_descriptorMatcher =       cv::cuda::DescriptorMatcher::createBFMatcher(cv::NORM_HAMMING); 
}

void Matcher::Configure(int imageWidth, int imageHeight, int roiX, int roiY, int roiW, int roiH)
{
    // set member variables
    m_imageWidth = imageWidth;
    m_imageHeight = imageHeight;
    m_roiX = roiX;
    m_roiY = roiY;
    m_roiW = roiW;
    m_roiH = roiH;

    m_GpuRefSet = false; // set flag indicating reference not yet set

    // create mask for specified ROI
    m_mask = GpuMat(imageHeight,imageWidth, CV_8UC1, Scalar::all(0));
    cv::Rect rect = cv::Rect(m_roiX, m_roiY, m_roiW, m_roiH);
    m_mask(rect).setTo(Scalar::all(255));       
}


double Matcher::NewFrame(void *pImagedata)
{
    // pImagedata = pointer to BGRA byte array
    // m_imageHeight and m_imageWidth have already been set
    // m_b is a pointer to the ORB detector

    if (!m_GpuRefSet)
    { // 1st time through (after call to Matcher::Configure), set reference keypoints and descriptors

        cv::cuda::GpuMat mat1(m_imageHeight, m_imageWidth, CV_8UC4, pImagedata);  // put image data into GpuMat

        cv::cuda::cvtColor(mat1, m_refImage, CV_BGRA2GRAY); // convert to grayscale as required by ORB

        m_keyRef.clear(); // clear the vector<KeyPoint>, keypoint vector for reference image

        m_b->detectAndCompute(m_refImage, m_mask, m_keyRef, m_descRef, false); // detect keypoints and compute descriptors

        m_GpuRefSet = true;     
    }

    cv::cuda::GpuMat mat2(m_imageHeight, m_imageWidth, CV_8UC4, pImagedata);  // put image data into GpuMat

    cv::cuda::cvtColor(mat2, m_testImage, CV_BGRA2GRAY, 0);  // convert to grayscale as required by ORB

    m_keyTest.clear(); // clear vector<KeyPoint>, keypoint vector for test image

    m_b->detectAndCompute(m_testImage, m_mask, m_keyTest, m_descTest, false);  // detect keypoints and compute descriptors


    double value = 0.0f;  // used to store return value ("match intensity")

        // calculate best match for each descriptor
        if (m_descTest.rows > 0)
        {   
            m_goodKeypoints.clear(); // clear vector of "good" KeyPoints, vector<KeyPoint> 

            m_descriptorMatcher->knnMatch(m_descTest, m_descRef, m_matches, 2, noArray());  // find matches

            // examine all matches, and collect the KeyPoints whose match distance mets given criteria
            for (int i = 0; i<m_matches.size(); i++){
                if (m_matches[i][0].distance < m_matches[i][1].distance * m_nnr){ // m_nnr = nearest neighbor ratio (typically 0.6 - 0.8)
                    m_goodKeypoints.push_back(m_keyRef.at(m_matches[i][0].trainIdx));  // not sure if getting the correct keypoint here
                }
            }

            // calculate "match intensity", i.e. percent of the keypoints found in the reference image that are also in the test image
            value = ((double)m_goodKeypoints.size()) / ((double)m_keyRef.size());
        }
        else
        {
            value = 0.0f;
        }

    return value;
}

这是一个与 IT 技术相关的流/异步 NewFrame 函数,它会失败:

double Matcher::NewFrame(void *pImagedata)
{
    if (m_b.empty()) return 0.0f;

    if (!m_GpuRefSet)
    {
        try
        {
            cv::cuda::GpuMat mat1(m_imageHeight, m_imageWidth, CV_8UC4, pImagedata);

            cv::cuda::cvtColor(mat1, m_refImage, CV_BGRA2GRAY);

            m_keyRef.clear();

            m_b->detectAndComputeAsync(m_refImage, m_mask, m_keyRef, m_descRef, false,m_stream);  // FAILS HERE

            m_stream.waitForCompletion();

            m_GpuRefSet = true;
        }
        catch (Exception e)
        {
            string msg = e.msg;
        }

    }

    cv::cuda::GpuMat mat2(m_imageHeight, m_imageWidth, CV_8UC4, pImagedata);

    cv::cuda::cvtColor(mat2, m_testImage, CV_BGRA2GRAY, 0, m_stream);

    m_keyTest.clear();

    m_b->detectAndComputeAsync(m_testImage, m_mask, m_keyTest, m_descTest, false, m_stream);

    m_stream.waitForCompletion();

    double value = 0.0f;

    // calculate best match for each descriptor

    if (m_descTest.rows > 0)
    {
        m_goodKeypoints.clear();
        m_descriptorMatcher->knnMatchAsync(m_descTest, m_descRef, m_matches, 2, noArray(), m_stream);

        m_stream.waitForCompletion(); 

        for (int i = 0; i<m_matches.size(); i++){
            if (m_matches[i][0].distance < m_matches[i][1].distance * m_nnr) // m_nnr = nearest neighbor ratio
            {
                m_goodKeypoints.push_back(m_keyRef.at(m_matches[i][0].trainIdx));
            }
        }

        value = ((double)m_goodKeypoints.size()) / ((double)m_keyRef.size());
    }
    else
    {
        value = 0.0f;
    }


    if (value > 1.0f) value = 1.0f;

    return value;
}

任何建议/意见都将不胜感激。
谢谢!
1个回答

5
经过一些尝试,我确信这确实是ORB检测器的合理使用方式,并且我使用最近邻比率方法进行“好坏”测试似乎也行得通。这回答了上述问题1和2。
关于问题3,我发现了一些可以大大改善情况的发现。
首先,事实证明,我没有足够小心地处理cv::cuda::Stream和cpu线程。虽然我相信对许多人来说这是显而易见的,并且在OpenCV文档中提到,但任何放置在特定cv::cuda::Stream上的内容都应该从同一个cpu线程中完成。不这样做不一定会创建异常,但会创建包括异常在内的未确定行为。
其次,对我来说,使用detectAndCompute和knnMatch的Async版本在多线程下更可靠。这似乎与Async版本使用基于GPU的所有参数有关,而非Async版本具有基于CPU的向量参数。当我编写简单的单线程测试应用程序时,两个Async和non-Async版本似乎都能正常工作。但是,我的真正应用程序在其他线程上运行CUDA内核和CUDA视频解码器,因此GPU上的东西很拥挤。
无论如何,这是我清理所有内容的异步函数调用版本。它演示了ORB检测器和描述符匹配器的异步/Stream版本的使用。传递给它的cv::cuda::Stream可以是cv::cuda::Stream::NullStream(),也可以是您创建的cv::cuda::Stream。只需记住在使用它的同一个cpu线程上创建流。
我仍然对改进有兴趣,但以下内容似乎有效。
orb = cuda::ORB::create(500, 1.2f, 8, 31, 0, 2, 0, 31, 20, true);   
matcher = cv::cuda::DescriptorMatcher::createBFMatcher(cv::NORM_HAMMING);  

// process 1st image
GpuMat imgGray1;  // load this with your grayscale image
GpuMat keys1; // this holds the keys detected
GpuMat desc1; // this holds the descriptors for the detected keypoints
GpuMat mask1; // this holds any mask you may want to use, or can be replace by noArray() in the call below if no mask is needed
vector<KeyPoint> cpuKeys1;  // holds keypoints downloaded from gpu

//ADD CODE TO LOAD imgGray1

orb->detectAndComputeAsync(imgGray1, mask1, keys1, desc1, false, m_stream);
stream.waitForCompletion();
orb->convert(keys1, cpuKeys1); // download keys to cpu if needed for anything...like displaying or whatever

// process 2nd image
GpuMat imgGray2;  // load this with your grayscale image
GpuMat keys2; // this holds the keys detected
GpuMat desc2; // this holds the descriptors for the detected keypoints
GpuMat mask2; // this holds any mask you may want to use, or can be replace by noArray() in the call below if no mask is needed
vector<KeyPoint> cpuKeys2;  // holds keypoints downloaded from gpu

//ADD CODE TO LOAD imgGray2

orb->detectAndComputeAsync(imgGray2, mask2, keys2, desc2, false, m_stream);
stream.waitForCompletion();
orb->convert(keys2, cpuKeys2); // download keys to cpu if needed for anything...like displaying or whatever

if (desc2.rows > 0)
{
    vector<vector<DMatch>> cpuKnnMatches;
    GpuMat gpuKnnMatches;  // holds matches on gpu
    matcher->knnMatchAsync(desc2, desc1, gpuKnnMatches, 2, noArray(), *stream);  // find matches
    stream.waitForCompletion();

    matcher->knnMatchConvert(gpuKnnMatches, cpuKnnMatches); // download matches from gpu and put into vector<vector<DMatch>> form on cpu

    vector<DMatch> matches;         // vector of good matches between tested images

    for (std::vector<std::vector<cv::DMatch> >::const_iterator it = cpuKnnMatches.begin(); it != cpuKnnMatches.end(); ++it) {
                if (it->size() > 1 && (*it)[0].distance / (*it)[1].distance < m_nnr) {  // use Nearest-Neighbor Ratio to determine "good" matches
            DMatch m = (*it)[0];
            matches.push_back(m);       // save good matches here                           

                }
            }

        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接