【问题标题】:using SURF to compare frame with multiple images gives me same result使用 SURF 将帧与多个图像进行比较给了我相同的结果
【发布时间】:2013-04-03 20:01:11
【问题描述】:

我正在使用带有 Visual c++ 2010 的 openCV 2.44 我想使用网络摄像头识别几个对象并在找到对象时打印对象的名称,但是当我与框架进行比较时,我得到相同的值,每次比较的框架和图像与 obj1,2,3,4,5 (good_matches.size (),good_matches1.size().. 具有相同的值) 你能告诉我哪里出了问题以及我如何加快算法速度

#include <opencv2\imgproc\imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
using namespace cv;


#define nimg 5


int main()
{
    Mat object =  imread( "aa.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object1 = imread( "bb.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object2 = imread( "bb.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object3 = imread( "bb.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object4 = imread( "bb.jpg", CV_LOAD_IMAGE_GRAYSCALE );

 /*   if( !object.data )
    {
        std::cout<< "Error reading object " << std::endl;
        return -1;
    }
*/

    //Detect the keypoints using SURF Detector
    int minHessian = 500;

    SurfFeatureDetector detector( minHessian );

    std::vector<KeyPoint> kp_object;
    std::vector<KeyPoint> kp_object1;
    std::vector<KeyPoint> kp_object2;
    std::vector<KeyPoint> kp_object3;
    std::vector<KeyPoint> kp_object4;

    detector.detect( object,  kp_object );
    detector.detect( object1, kp_object1 );
    detector.detect( object2, kp_object2 );
    detector.detect( object3, kp_object3 );
    detector.detect( object4, kp_object4 );


    //Calculate descriptors (feature vectors)
    SurfDescriptorExtractor extractor;
    Mat des_object;
    Mat des_object1;
    Mat des_object2;
    Mat des_object3;
    Mat des_object4;

    extractor.compute( object, kp_object, des_object );
    extractor.compute( object1, kp_object1, des_object1 );
    extractor.compute( object2, kp_object2, des_object2 );
    extractor.compute( object3, kp_object3, des_object3 );
    extractor.compute( object4, kp_object4, des_object4 );

    FlannBasedMatcher matcher;

    VideoCapture cap(1);

    namedWindow("Good Matches");

    std::vector<Point2f> obj_corners(4);

    //Get the corners from the object
    obj_corners[0] = cvPoint(0,0);
    obj_corners[1] = cvPoint( object.cols, 0 );
    obj_corners[2] = cvPoint( object.cols, object.rows );
    obj_corners[3] = cvPoint( 0, object.rows );

    char key = 'a';
    int framecount = 0;
    Mat frame;



 Mat des_image, img_matches;

 char vect[nimg];
 char contor;
 char ok, ko;



    while (1)
    {

        cap >> frame;

        if (framecount < 5)
        {
            framecount++;
            continue;
        }

        std::vector<KeyPoint> kp_image;

        std::vector<vector<DMatch > > matches;
        std::vector<vector<DMatch > > matches1;
        std::vector<vector<DMatch > > matches2;
        std::vector<vector<DMatch > > matches3;
        std::vector<vector<DMatch > > matches4;

        std::vector<DMatch > good_matches;
        std::vector<DMatch > good_matches1;
        std::vector<DMatch > good_matches2;
        std::vector<DMatch > good_matches3;
        std::vector<DMatch > good_matches4;

        std::vector<Point2f> obj;
        std::vector<Point2f> scene;
        std::vector<Point2f> scene_corners(4);
        Mat H;
        Mat image;




        cvtColor(frame, image, CV_RGB2GRAY);

        detector.detect( image, kp_image );
        extractor.compute( image, kp_image, des_image );



        matcher.knnMatch(des_object, des_image, matches, 2);








    //  printf("d \n");
//////////////////////////////////////////////////////

        contor=0;

        for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
            {
                good_matches.push_back(matches[i][0]);
            }
        }

        vect[contor]=good_matches.size();
/////////////////////////////////////////////////////   

        contor=1;
        matcher.knnMatch(des_object, des_image, matches1, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches1.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches1[i][0].distance < 0.6*(matches1[i][1].distance)) && ((int) matches1[i].size()<=2 && (int) matches1[i].size()>0))
            {
                good_matches1.push_back(matches1[i][0]);
            }
        }

        vect[contor]=good_matches1.size();

/////////////////////////////////////////////////////   

        contor=2;
        matcher.knnMatch(des_object, des_image, matches2, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches2.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches2[i][0].distance < 0.6*(matches2[i][1].distance)) && ((int) matches2[i].size()<=2 && (int) matches2[i].size()>0))
            {
                good_matches2.push_back(matches2[i][0]);
            }
        }
        vect[contor]=good_matches2.size();
////////////////////////////////////////////////////        

        contor =3; 
        matcher.knnMatch(des_object, des_image, matches3, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches3.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches3[i][0].distance < 0.6*(matches3[i][1].distance)) && ((int) matches3[i].size()<=2 && (int) matches3[i].size()>0))
            {
                good_matches3.push_back(matches[i][0]);
            }
        }
        vect[contor]=good_matches3.size();

//////////////////////////////////////////////////

        contor=4;
        matcher.knnMatch(des_object, des_image, matches4, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches4.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches4[i][0].distance < 0.6*(matches4[i][1].distance)) && ((int) matches4[i].size()<=2 && (int) matches4[i].size()>0))
            {
                good_matches4.push_back(matches[i][0]);
            }
        }
      vect[contor]=good_matches4.size();



printf("%d %d %d %d %d \n ",vect[0],vect[1],vect[2],vect[3],vect[4]);

ok=0;
    for (contor=1;contor<nimg;contor++)
        if (vect[contor]>vect[contor-1])
            ok=contor;
for (ko=10;ko>3;ko++)
{
if (ok==0 && vect[ok]>ko)
    {printf("obj1 \n");

ko=2;}
else if (ok==1 && vect[ok]>ko)
    {printf("obj2 \n");
ko=2;}
else if (ok==2 && vect[ok]>ko)
    {printf("obj3 \n");
    ko=2;}
else if (ok==3 && vect[ok]>ko)
    {printf("obj4 \n");
    ko=2;}
else if (ok==4 && vect[ok]>ko)
    {printf("obj5 \n");
    ko=2;}


}        



        //Show detected matches
        imshow( "Good Matches",frame /*img_matches*/ );

        key = waitKey(1);  
    }

}   

【问题讨论】:

  • 来吧,您希望这里有人深入您的代码并修复它吗?你需要更精确:到底什么不起作用?它建立吗?它运行吗?发生什么了 ?代码的哪一部分与您的问题有关?尝试减少到​​ MWE。

标签: c++ opencv surf


【解决方案1】:

sry for long post,它运行它给了我很好的匹配,但只适用于第一个图像(对象),其他得到与第一个相同的结果,在这段代码的末尾我想要匹配,matches1,2,3,4为每个图像(对象)获得不同的结果以了解 cam 看到的对象,在 vect 中为每个图像保留 good_matches.size()(比较)

//////////////////////////////////////////////////////
matcher.knnMatch(des_object, des_image, matches, 2);
        contor=0;

        for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0))
            {
                good_matches.push_back(matches[i][0]);
            }
        }

        vect[contor]=good_matches.size();
/////////////////////////////////////////////////////   

        contor=1;
        matcher.knnMatch(des_object, des_image, matches1, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches1.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches1[i][0].distance < 0.6*(matches1[i][1].distance)) && ((int) matches1[i].size()<=2 && (int) matches1[i].size()>0))
            {
                good_matches1.push_back(matches1[i][0]);
            }
        }

        vect[contor]=good_matches1.size();

/////////////////////////////////////////////////////   

        contor=2;
        matcher.knnMatch(des_object, des_image, matches2, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches2.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches2[i][0].distance < 0.6*(matches2[i][1].distance)) && ((int) matches2[i].size()<=2 && (int) matches2[i].size()>0))
            {
                good_matches2.push_back(matches2[i][0]);
            }
        }
        vect[contor]=good_matches2.size();
////////////////////////////////////////////////////        

        contor =3; 
        matcher.knnMatch(des_object, des_image, matches3, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches3.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches3[i][0].distance < 0.6*(matches3[i][1].distance)) && ((int) matches3[i].size()<=2 && (int) matches3[i].size()>0))
            {
                good_matches3.push_back(matches[i][0]);
            }
        }
        vect[contor]=good_matches3.size();

//////////////////////////////////////////////////

        contor=4;
        matcher.knnMatch(des_object, des_image, matches4, 2);


        for(int i = 0; i < min(des_image.rows-1,(int) matches4.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
        {
            if((matches4[i][0].distance < 0.6*(matches4[i][1].distance)) && ((int) matches4[i].size()<=2 && (int) matches4[i].size()>0))
            {
                good_matches4.push_back(matches[i][0]);
            }
        }
      vect[contor]=good_matches4.size();

【讨论】:

  • 您是否对所有 knnMatch() 调用使用相同的 des_objectdes_image?那么,matchesX 总是包含相同的值是有道理的。
猜你喜欢
  • 1970-01-01
  • 2016-03-21
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多