radius_matching.cpp 5.06 KB
Newer Older
biagio montesano's avatar
biagio montesano committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*M///////////////////////////////////////////////////////////////////////////////////////
 //
 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 //
 //  By downloading, copying, installing or using the software you agree to this license.
 //  If you do not agree to this license, do not download, install,
 //  copy or use the software.
 //
 //
 //                           License Agreement
 //                For Open Source Computer Vision Library
 //
 // Copyright (C) 2014, Biagio Montesano, all rights reserved.
 // Third party copyrights are property of their respective owners.
 //
 // Redistribution and use in source and binary forms, with or without modification,
 // are permitted provided that the following conditions are met:
 //
 //   * Redistribution's of source code must retain the above copyright notice,
 //     this list of conditions and the following disclaimer.
 //
 //   * Redistribution's in binary form must reproduce the above copyright notice,
 //     this list of conditions and the following disclaimer in the documentation
 //     and/or other materials provided with the distribution.
 //
 //   * The name of the copyright holders may not be used to endorse or promote products
 //     derived from this software without specific prior written permission.
 //
 // This software is provided by the copyright holders and contributors "as is" and
 // any express or implied warranties, including, but not limited to, the implied
 // warranties of merchantability and fitness for a particular purpose are disclaimed.
 // In no event shall the Intel Corporation or contributors be liable for any direct,
 // indirect, incidental, special, exemplary, or consequential damages
 // (including, but not limited to, procurement of substitute goods or services;
 // loss of use, data, or profits; or business interruption) however caused
 // and on any theory of liability, whether in contract, strict liability,
 // or tort (including negligence or otherwise) arising in any way out of
 // the use of this software, even if advised of the possibility of such damage.
 //
 //M*/

42 43 44 45 46 47 48 49 50 51 52
#include <opencv2/line_descriptor.hpp>

#include "opencv2/core/utility.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>

#include <iostream>
#include <vector>

using namespace cv;
53
using namespace cv::line_descriptor;
54 55

static const std::string images[] =
biagio montesano's avatar
biagio montesano committed
56
{ "cameraman.jpg", "church.jpg", "church2.png", "einstein.jpg", "stuff.jpg" };
57 58

static const char* keys =
biagio montesano's avatar
biagio montesano committed
59
{ "{@image_path | | Image path }" };
60 61 62

static void help()
{
biagio montesano's avatar
biagio montesano committed
63
  std::cout << "\nThis example shows the functionalities of radius matching " << "Please, run this sample using a command in the form\n"
64
      << "./example_line_descriptor_radius_matching <path_to_input_images>/" << std::endl;
65 66 67 68
}

int main( int argc, char** argv )
{
biagio montesano's avatar
biagio montesano committed
69 70
  /* get parameters from comand line */
  CommandLineParser parser( argc, argv, keys );
71
  String pathToImages = parser.get < String > ( 0 );
biagio montesano's avatar
biagio montesano committed
72 73 74

  /* create structures for hosting KeyLines and descriptors */
  int num_elements = sizeof ( images ) / sizeof ( images[0] );
75 76
  std::vector < Mat > descriptorsMat;
  std::vector < std::vector<KeyLine> > linesMat;
biagio montesano's avatar
biagio montesano committed
77 78

  /*create a pointer to a BinaryDescriptor object */
79
  Ptr < BinaryDescriptor > bd = BinaryDescriptor::createBinaryDescriptor();
biagio montesano's avatar
biagio montesano committed
80 81 82 83 84 85 86

  /* compute lines and descriptors */
  for ( int i = 0; i < num_elements; i++ )
  {
    /* get path to image */
    std::stringstream image_path;
    image_path << pathToImages << images[i];
87
    std::cout << image_path.str().c_str() << std::endl;
biagio montesano's avatar
biagio montesano committed
88 89 90 91 92 93 94 95 96

    /* load image */
    Mat loadedImage = imread( image_path.str().c_str(), 1 );
    if( loadedImage.data == NULL )
    {
      std::cout << "Could not load images." << std::endl;
      help();
      exit( -1 );
    }
97 98

    /* compute lines and descriptors */
99
    std::vector < KeyLine > lines;
biagio montesano's avatar
biagio montesano committed
100 101 102
    Mat computedDescr;
    bd->detect( loadedImage, lines );
    bd->compute( loadedImage, lines, computedDescr );
103

biagio montesano's avatar
biagio montesano committed
104 105
    descriptorsMat.push_back( computedDescr );
    linesMat.push_back( lines );
106

biagio montesano's avatar
biagio montesano committed
107
  }
108

biagio montesano's avatar
biagio montesano committed
109 110 111 112 113 114 115 116 117 118
  /* compose a queries matrix */
  Mat queries;
  for ( size_t j = 0; j < descriptorsMat.size(); j++ )
  {
    if( descriptorsMat[j].rows >= 5 )
      queries.push_back( descriptorsMat[j].rowRange( 0, 5 ) );

    else if( descriptorsMat[j].rows > 0 && descriptorsMat[j].rows < 5 )
      queries.push_back( descriptorsMat[j] );
  }
119

biagio montesano's avatar
biagio montesano committed
120
  std::cout << "It has been generated a matrix of " << queries.rows << " descriptors" << std::endl;
121

biagio montesano's avatar
biagio montesano committed
122
  /* create a BinaryDescriptorMatcher object */
123
  Ptr < BinaryDescriptorMatcher > bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
124

biagio montesano's avatar
biagio montesano committed
125 126
  /* populate matcher */
  bdm->add( descriptorsMat );
127

biagio montesano's avatar
biagio montesano committed
128
  /* compute matches */
129
  std::vector < std::vector<DMatch> > matches;
biagio montesano's avatar
biagio montesano committed
130
  bdm->radiusMatch( queries, matches, 30 );
131 132
  std::cout << "size matches sample " << matches.size() << std::endl;

133
  for ( int i = 0; i < (int) matches.size(); i++ )
134
  {
135
    for ( int j = 0; j < (int) matches[i].size(); j++ )
136 137 138 139 140
    {
      std::cout << "match: " << matches[i][j].queryIdx << " " << matches[i][j].trainIdx << " " << matches[i][j].distance << std::endl;
    }

  }
141 142

}