Real Time object recognition and tracking with surf and c++ opencv 3.1

asked 2016-03-07 09:02:26 -0600

Widad gravatar image

updated 2016-03-16 08:13:33 -0600

Hello, I m working actually in computer vision project for real time object recognition and tracking by using c++ opencv 3.1 and surf , but the object is note tracking if it is far from the camera or if it is note detect a specific number of key points between the image of the object and the real object in the video from the camera.

this is the code

#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/xfeatures2d/cuda.hpp>
#include <opencv2/xfeatures2d/xfeatures2d.hpp>
//Name spaces used 
usingnamespace cv;
using namespace std;
int main(){
//turn performance analysis functions on if testing = true
bool testing=true;
double t; //timing variable
//load training image
Mat object = imread ("C:/opencvInputOutput/titi.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!object.data){
    cout<<"Can't open image";
    return -1;
}
namedWindow("Good Matches", CV_WINDOW_AUTOSIZE);
//SURF Detector, and descriptor parameters
int minHess=3000;
vector<KeyPoint> kpObject, kpImage;
Mat desObject, desImage;


//Performance measures calculations for report
if (testing)
{
    cout<<object.rows<<" "<<object.cols<<endl;

    //calculate integral image
    Mat iObject;
    integral(object, iObject);
    imshow("Good Matches", iObject);
    imwrite("C:/opencvInputOutput/IntegralImage.jpg", iObject);
    cvWaitKey(0);

    //calculate number of interest points, computation time as f(minHess)
    int minHessVector[]={100, 500, 1000, 1500, 2000, 2500, 3000,
                                3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500,
                                8000, 8500, 9000, 9500, 10000};
    int minH;
    std::ofstream file;
    file.open("C:/opencvInputOutput/TimingC.csv", std::ofstream::out);
    for (int i=0; i<20; i++)
    {

        minH=minHessVector[i];
        t = (double)getTickCount();

        //viens d'étre ajouter spécifique à opencv version 3.1
        Ptr<xfeatures2d::SURF> surf = xfeatures2d::SURF::create(minH);

        //version 2.4.10
        //SurfFeatureDetector detector(minH);
        //detector.detect(object, kpObject); for 2.4.10 version
        //for 3.1.0 version
        surf->detectAndCompute(object, Mat(), kpObject, desObject);
        //surf->detectAndCompute(object, Mat(), kpImage,  desImage);


        t = ((double)getTickCount() - t)/getTickFrequency();
        file<<minHess<<","<<kpObject.size()<<","<<t<<",";
        cout<<t<<" "<<kpObject.size()<<" "<<desObject.size()<<endl;

        t = (double)getTickCount();
        //SurfDescriptorExtractor extractor;
        //extractor.compute(object, kpObject, desObject);
        t = ((double)getTickCount() - t)/getTickFrequency();
        file<<t<<endl;
    }
    file.close();

//Display keypoints on training image
Mat interestPointObject=object;
for (unsigned int i=0; i<kpObject.size();i++)
{
    if(kpObject[i].octave)
    {
        circle(interestPointObject,kpObject[i].pt,kpObject[i].size,0);
        string octaveS;
        switch(kpObject[i].octave)
        {
        case 0:
            octaveS="0";
            break;
        case 1:
            octaveS='1';
            break;
        case 2:
            octaveS='2';
            break;
        default:
            break;

        }
        putText(interestPointObject, octaveS, kpObject[i].pt,
                FONT_HERSHEY_COMPLEX_SMALL, 1, cvScalar(0,0,250), 1, CV_AA);
    }

}
imshow("Good Matches",interestPointObject);
imwrite("C:/opencvInputOutput/SmobileIP2.jpg", interestPointObject);
cvWaitKey(50);


}


//SURF Detector, and descriptor parameters, match object initialization
minHess=2000;

Ptr<xfeatures2d::SURF> surf1 = xfeatures2d::SURF::create(minHess);
surf1->detectAndCompute(object, Mat(), kpObject, desObject);
//surf->detectAndCompute(object, Mat(), kpImage,  desImage);

/*SurfFeatureDetector detector(minHess);
detector.detect(object, kpObject);
SurfDescriptorExtractor extractor;
extractor.compute(object, kpObject, desObject);*/
FlannBasedMatcher matcher;


//Initialize video and display window
VideoCapture cap(0);  //camera 1 ...
(more)
edit retag flag offensive close merge delete

Comments

1

please cut down your wall of code to a minimal reproducable example

berak gravatar imageberak ( 2016-03-10 06:13:09 -0600 )edit
1

sloppiness: you're filling desImage , but try to compare des_image (which is empty)

berak gravatar imageberak ( 2016-03-10 06:17:18 -0600 )edit

thanks it worked for me but I don't Know why I must press the keyboard for pass to next step at every step

Widad gravatar imageWidad ( 2016-03-10 06:49:01 -0600 )edit

waitKey(0) waits infinite

berak gravatar imageberak ( 2016-03-10 07:00:01 -0600 )edit