0

我正在使用带有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);  
    }

}   
4

1 回答 1

0

对不起,很长的帖子,它运行它给了我很好的匹配,但只适用于第一个图像(对象),其他人得到与第一个相同的结果,在这段代码的末尾我想要匹配,matches1,2,3,4 有不同每个图像(对象)的结果都知道凸轮看到什么对象,在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();
于 2013-04-04T04:35:44.820 回答