radius_matching.cpp 5.27 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
#include <iostream>
43
#include <opencv2/opencv_modules.hpp>
44 45 46

#ifdef HAVE_OPENCV_FEATURES2D

47
#include <opencv2/line_descriptor.hpp>
48
#include <opencv2/core/utility.hpp>
49 50 51 52 53 54 55
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>

#include <vector>

using namespace cv;
56
using namespace cv::line_descriptor;
57 58

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

static const char* keys =
biagio montesano's avatar
biagio montesano committed
62
{ "{@image_path | | Image path }" };
63 64 65

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

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

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

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

  /* 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];
90
    std::cout << image_path.str().c_str() << std::endl;
biagio montesano's avatar
biagio montesano committed
91 92 93 94 95 96 97 98 99

    /* 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 );
    }
100 101

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

biagio montesano's avatar
biagio montesano committed
107 108
    descriptorsMat.push_back( computedDescr );
    linesMat.push_back( lines );
109

biagio montesano's avatar
biagio montesano committed
110
  }
111

biagio montesano's avatar
biagio montesano committed
112 113 114 115 116 117 118 119 120 121
  /* 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] );
  }
122

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

biagio montesano's avatar
biagio montesano committed
125
  /* create a BinaryDescriptorMatcher object */
126
  Ptr < BinaryDescriptorMatcher > bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
127

biagio montesano's avatar
biagio montesano committed
128 129
  /* populate matcher */
  bdm->add( descriptorsMat );
130

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

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

  }
144 145

}
146 147 148 149 150 151 152 153 154 155

#else

int main()
{
    std::cerr << "OpenCV was built without features2d module" << std::endl;
    return 0;
}

#endif // HAVE_OPENCV_FEATURES2D