0

有谁熟悉这个错误?我实时测试了一个冲浪描述符。我想用它来识别使用此功能的不同种类的鱼。有时程序没问题,但有时会出错。编译成功。编译后显示此错误。

在此处输入图像描述

#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 3


int main()
{
    Mat object =  imread( "D:/galunggong.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object1 = imread( "D:/sapsap.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    Mat object2 = imread( "D:/bisugo.jpg",CV_LOAD_IMAGE_GRAYSCALE );
    // Mat object3 = imread( "4.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    //Mat object4 = imread( "5.jpg", CV_LOAD_IMAGE_GRAYSCALE );

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


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

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


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


extractor.compute( object, kp_object, des_object );
extractor.compute( object1, kp_object1, des_object1 );
extractor.compute( object2, kp_object2, des_object2 );


FlannBasedMatcher matcher;
 CvCapture* cap = cvCreateCameraCapture(0);
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty( cap, CV_CAP_PROP_FRAME_HEIGHT, 240 );

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)
{

        frame = cvQueryFrame(cap);

    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<DMatch > good_matches;
    std::vector<DMatch > good_matches1;
    std::vector<DMatch > good_matches2;


    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_object1, 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_object2, 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 \n ",vect[0],vect[1],vect[2]);

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("Forward \n");
    ko=2;}
else if (ok==1 && vect[ok]>ko)
    {printf("Turn Left \n");
    ko=2;}
else if (ok==2 && vect[ok]>ko)
    {printf("Turn Right \n");
    ko=2;}



}        



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

    key = waitKey(1);  
}

}
4

1 回答 1

1

有时 SURF 找不到任何特征点,因此它可能会返回一个空描述符。您应该检查数据描述符:

if(des_object.empty()||des_object1.empty()||des_object2.empty()) { std::cout<<"Empty descriptor(s)."<<std::endl; return -1; }

于 2014-04-29T11:05:29.500 回答