Thursday, 24 March 2016

Opencv C++ Code with Example for Feature Extraction and Detection using SURF Detector

This OpenCV C++ Tutorial is about feature detection using SURF Detector.
Object Detection and Recognition has been of prime importance in Computer Vision.Thus many algorithms and techniques are being proposed to enable machines to detect and recognize objects.

So one of the easiest method what we can think of is storing whole of an image in a Matrix and comparing it with the background image.But storing whole of the image in the matrix an comparing it pixel by pixel is cumbersome, since the pixels values will change with the change in the lightening condition,rotation,size of the image etc.

So the most common  logic for all of these methods of object detection is feature recognition.Features are nothing but points of interest in an image.Thus we extract  and compare features with other images to search for the desired object/objects in the given image frame.
Now ,how should we determine these points of interest (features) in an image?What are its characteristics?

Characteristic of Features are:-
  • Geometric Invariance:Rotation,Scaling,Translation etc.
    • Scale Invariant i.e able to detect image at any scale irrespective of its distance from the webcam
    • Rotation Invariant i.e able to detect image rotated at any angle with respective to original image
    • Translation Invariant i.e Even if the image translates in the background, it should be able to detect it.Since on translation the background of an image may change.
  • Photometric Invariance(Brightness,Exposure) i.e irrespective of the lightening condition, it should be able to detect the desired image.

Now what is SURF?
SURF stands for Speeded Up Robust Features. It is an algorithm which extracts some unique keypoints and descriptors from an image.
In SURF,We use determinant of Hessian Matrix for feature detection.
Also, in SURF Laplacian of Gaussian (LOG) is approximated with Box Filter.Thus convolution with Box filters can be easily evaluated with the help of Integral Images.

Prerequisite Concepts:-
  • Laplacian of Gaussian
  • Box Filter
  • Scale Space
  • Integral Image
Advantages of Object detection using SURF
1. It is scale and rotation invariant.
2. Also,it doesn't require that long and tedious training, which is need in that of OpenCV Haar training.Also Haar is not rotation invariant.Thus,providing an edge over Haar Training.
3. It is several times faster than SIFT(Scale Invariant Feature Transform). Disadvantage:-
4. The detection process is little slow,as compared to that of Haar Training.Thus needing long time to detect the objects.

Here are few of the syntaxes used in the below code:-
FeatureDetector::detect
It detects keypoints in an image Syntax:
C++: void FeatureDetector::detect(const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() )
C++: void FeatureDetector::detect(const vector<Mat>& images, vector<vector<KeyPoint>>& keypoints, const vector<Mat>& masks=vector<Mat>() )
Parameters:
image – Image.
images – Image set.
keypoints – The detected keypoints. In the second variant of the method keypoints[i] is a set of keypoints detected in images[i] .
mask – Mask specifying where to look for keypoints (optional). It must be a 8-bit integer matrix with non-zero values in the region of interest.
masks – Masks for each input image specifying where to look for keypoints (optional). masks[i] is a mask for images[i].

SURF::SURF
The SURF extractor constructors.
Syntax:
C++: SURF::SURF()
C++: SURF::SURF(double hessianThreshold, int nOctaves=4, int nOctaveLayers=2, bool extended=true, bool upright=false )
Parameters:
  • hessianThreshold – Threshold for the keypoint detector. Only features, whose hessian is larger than hessianThreshold are retained by the detector. Therefore, the larger the value, the less keypoints you will get. A good default value could be from 300 to 500, depending from the image contrast.
  • nOctaves – The number of a gaussian pyramid octaves that the detector uses. It is set to 4 by default. If you want to get very large features, use the larger value. If you want just small features, decrease it.
  • nOctaveLayers – The number of images within each octave of a gaussian pyramid. It is set to 2 by default.
  • extended – 0 means that the basic descriptors (64 elements each) shall be computed
    1 means that the extended descriptors (128 elements each) shall be computed
  • upright – 0 means that detector computes orientation of each feature.
    1 means that the orientation is not computed (which is much, much faster). For example, if you match images from a stereo pair, or do image stitching, the matched features likely have very similar angles, and you can speed up feature extraction by setting upright=1..
Here is the OpenCV C++ Code with example to extract interest points with the help of SURF :
//OPENCV C++ Tutorial:Feature Detector Using SURF Detector
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"

using namespace cv;
using namespace std;

int main()
{
  Mat image1 = imread( "C:\\Users\\arjun\\Desktop\\opencv-logo.jpg", CV_LOAD_IMAGE_GRAYSCALE );
 
  if( !image1.data)
  { 
   cout<< " --(!) Error reading images " << endl; 
   return -1; 
  }

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;
  SurfFeatureDetector detector( minHessian);
  std::vector<KeyPoint> keypoints_1;
  detector.detect( image1, keypoints_1 );
  
  //--Step2: Draw keypoints
  Mat img_keypoints_surf; 
  drawKeypoints( image1, keypoints_1, img_keypoints_surf, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
 
  //--Step3: Show detected (drawn) keypoints
  imshow("Keypoints 1", img_keypoints_surf );
  waitKey(0);

  return 0;
  }


Input:
OpenCV-SURF-Input

Output:
OpenCV SURF Feature Extraction Output

No comments:

Post a Comment