Sift recognize object

asked 2016-05-23 23:56:23 -0600

updated 2016-05-24 02:14:41 -0600

LBerger gravatar image

I would like to detect 2 object using Sift algorithm. But i can't distinguish 2 object. Anyone can help me?? image description this is my code

#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/features2d/features2d.hpp>

using namespace cv;
using namespace std;

SiftFeatureDetector detector;
SiftDescriptorExtractor extractor;
FlannBasedMatcher matcher;

Mat img_object, img_scene;
Mat img_object_g, img_scene_g;

vector<KeyPoint> keypoints_object, keypoints_scene;
Mat descriptors_object, descriptors_scene;

//////////////////////////////////////////////////////////////////////////////////
cv_bridge::CvImagePtr cv_ptr;
void image_cb (const sensor_msgs::ImageConstPtr& msg) {

double t0 = getTickCount ();
cv_ptr = cv_bridge::toCvCopy (msg, sensor_msgs::image_encodings::BGR8);
img_scene = cv_ptr->image;
cvtColor (img_scene, img_scene_g, CV_BGR2GRAY);

detector.detect (img_scene_g, keypoints_scene);
extractor.compute (img_scene_g, keypoints_scene, descriptors_scene);

std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );

double max_dist = 0; double min_dist = 100;
for( int i = 0; i < descriptors_object.rows; i++ ) {
    double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
}

std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_object.rows; i++ ) {
    if( matches[i].distance < 3*min_dist ) {
        good_matches.push_back( matches[i]);
    }
}

Mat img_displayed;
Mat img_displayed_1;


// ve cac duong thang va xoa cac diem khong tot
  drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,

           good_matches, img_displayed, Scalar::all(-1), Scalar::all(-1),
           vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );


//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i < good_matches.size(); i++ ) {
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
}
Mat H = findHomography( obj, scene, CV_RANSAC );

//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(img_object.cols/8,img_object.rows/8); obj_corners[1] = cvPoint( 

img_object.cols*7/8, img_object.rows/8 );
    obj_corners[2] = cvPoint( img_object.cols*7/8, img_object.rows*3/8  ); obj_corners[3] = cvPoint( img_object.cols/8, img_object.rows*3/8 );
    std::vector<Point2f> scene_corners(4);

 perspectiveTransform( obj_corners, scene_corners, H);
    //-- Draw lines between the corners (the mapped object in the scene - image_2 )
    line( img_displayed, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
    line( img_displayed, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
    line( img_displayed, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
    line( img_displayed, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

 //-- Get the corners from the image_2 ( the object to be "detected" )
    std::vector<Point2f> obj_corners_2(4);
    obj_corners_2[0] = cvPoint(0,img_object.rows/2); obj_corners_2[1] = cvPoint( img_object.cols, img_object.rows/2 );
    obj_corners_2[2] = cvPoint( img_object.cols, img_object.rows  ); obj_corners_2[3] = cvPoint( 0, img_object.rows );
    std::vector<Point2f> scene_corners_2(4);
  perspectiveTransform( obj_corners_2, scene_corners_2, H);
    //-- Draw lines between the corners (the mapped object in the scene - image_2 )
    line( img_displayed, scene_corners_2[0] + Point2f( img_object.cols, 0), scene_corners_2[1] + Point2f( img_object.cols, 0), Scalar(255, 0, 0), 4 );
    line( img_displayed, scene_corners_2[1] + Point2f( img_object.cols, 0), scene_corners_2[2] + Point2f( img_object.cols, 0), Scalar( 255, 0 ...
(more)
edit retag flag offensive close merge delete

Comments

Please use icon with "101010" to help reading your code

LBerger gravatar imageLBerger ( 2016-05-24 02:15:58 -0600 )edit