otbImageToFastSIFTKeyPointSetFilterOutputDescriptorAscii.cxx 4.3 KB
Newer Older
1
/*
Julien Michel's avatar
Julien Michel committed
2
 * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22 23 24 25 26

#include <iomanip>
#include <iostream>
#include <fstream>
#include <algorithm>

27 28 29 30 31 32 33
#include "otbSiftFastImageFilter.h"
#include "otbImageFileReader.h"
#include "itkPointSet.h"
#include "itkVariableLengthVector.h"
#include "itkRGBPixel.h"
#include "itkImageRegionIterator.h"

OTB Bot's avatar
STYLE  
OTB Bot committed
34
bool CMPData(std::vector<float>  a, std::vector<float>  b)
35
{
OTB Bot's avatar
STYLE  
OTB Bot committed
36
  return lexicographical_compare(a.begin(), a.begin() + 2, b.begin(), b.begin() + 2);
37 38
}

39
int otbImageToFastSIFTKeyPointSetFilterOutputDescriptorAscii(int itkNotUsed(argc), char * argv[])
40 41 42 43 44 45 46
{
  const char * infname = argv[1];
  const char * outfname = argv[2];

  const unsigned int scales = atoi(argv[3]);

  typedef float RealType;
OTB Bot's avatar
STYLE  
OTB Bot committed
47
  const unsigned int Dimension = 2;
48

OTB Bot's avatar
STYLE  
OTB Bot committed
49 50 51 52 53
  typedef otb::Image<RealType, Dimension>                   ImageType;
  typedef itk::VariableLengthVector<RealType>               RealVectorType;
  typedef otb::ImageFileReader<ImageType>                   ReaderType;
  typedef itk::PointSet<RealVectorType, Dimension>          PointSetType;
  typedef otb::SiftFastImageFilter<ImageType, PointSetType> ImageToFastSIFTKeyPointSetFilterType;
54

55
  // Iterator types
OTB Bot's avatar
STYLE  
OTB Bot committed
56 57
  typedef PointSetType::PointsContainer    PointsContainerType;
  typedef PointsContainerType::Iterator    PointsIteratorType;
58 59 60
  typedef PointSetType::PointDataContainer PointDataContainerType;
  typedef PointDataContainerType::Iterator PointDataIteratorType;

OTB Bot's avatar
STYLE  
OTB Bot committed
61
  typedef std::vector<float>          siftDataVector;
62 63
  typedef std::vector<siftDataVector> ImageDataType;   //Kind of PointSet with vectors

64
  // Instantiating object
OTB Bot's avatar
STYLE  
OTB Bot committed
65
  ReaderType::Pointer                           reader = ReaderType::New();
66 67
  ImageToFastSIFTKeyPointSetFilterType::Pointer filter = ImageToFastSIFTKeyPointSetFilterType::New();

Emmanuel Christophe's avatar
Emmanuel Christophe committed
68
  //Instantiation of std::vector for lexicographiacal sorting
OTB Bot's avatar
STYLE  
OTB Bot committed
69
  ImageDataType imageData;
70

71
  reader->SetFileName(infname);
72
  filter->SetInput(reader->GetOutput());
Otmane Lahlou's avatar
Otmane Lahlou committed
73
  filter->SetScalesNumber(scales);
74 75
  filter->Update();

OTB Bot's avatar
STYLE  
OTB Bot committed
76
  PointsIteratorType    pIt = filter->GetOutput()->GetPoints()->Begin();
77 78 79 80
  PointDataIteratorType pDataIt = filter->GetOutput()->GetPointData()->Begin();

  std::ofstream outfile(outfname);

OTB Bot's avatar
STYLE  
OTB Bot committed
81
  outfile << "Number of scales: " << scales << std::endl;
82
  outfile << "Number of SIFT key points: " << filter->GetOutput()->GetNumberOfPoints() << std::endl;
83

OTB Bot's avatar
STYLE  
OTB Bot committed
84 85
  if (filter->GetOutput()->GetPointData()->Size() != filter->GetOutput()->GetPoints()->Size()) return EXIT_FAILURE;
  if (filter->GetOutput()->GetPointData()->Size() == 0) return EXIT_FAILURE;
86

87
  // Copy the PointSet to std::vector< std::vector >
OTB Bot's avatar
STYLE  
OTB Bot committed
88 89
  while (pIt != filter->GetOutput()->GetPoints()->End() &&  pDataIt != filter->GetOutput()->GetPointData()->End())
    {
90 91 92 93 94
    siftDataVector siftData;

    siftData.push_back(pIt.Value()[0]);
    siftData.push_back(pIt.Value()[1]);

OTB Bot's avatar
STYLE  
OTB Bot committed
95
    unsigned int lIterDesc = 0;
96
    while (lIterDesc < pDataIt.Value().Size())
OTB Bot's avatar
STYLE  
OTB Bot committed
97
      {
98 99
      siftData.push_back(pDataIt.Value()[lIterDesc]);
      lIterDesc++;
OTB Bot's avatar
STYLE  
OTB Bot committed
100
      }
101

102 103 104
    imageData.push_back(siftData);
    ++pIt;
    ++pDataIt;
OTB Bot's avatar
STYLE  
OTB Bot committed
105
    }
106 107

  //Sorting the vectors
108
  ImageDataType::iterator itData;
OTB Bot's avatar
STYLE  
OTB Bot committed
109
  sort(imageData.begin(), imageData.end(), CMPData);
110 111

  itData = imageData.begin();
112
  unsigned int stopVal = static_cast<unsigned int>(filter->GetOutput()->GetPointData()->Begin().Value().Size());
113

OTB Bot's avatar
STYLE  
OTB Bot committed
114 115 116 117 118
  while (itData != imageData.end())
    {
    unsigned int itDescriptor = 0;
    outfile << "[ ";
    while (itDescriptor < stopVal)
119
    //while (itDescriptor < static_cast<int>((*itData).size()-2) )
OTB Bot's avatar
STYLE  
OTB Bot committed
120 121
      {
      outfile << std::fixed << std::setprecision(4) << (*itData)[itDescriptor + 2] << " ";
122
      itDescriptor++;
OTB Bot's avatar
STYLE  
OTB Bot committed
123
      }
124 125
    outfile << "]" << std::endl;
    ++itData;
OTB Bot's avatar
STYLE  
OTB Bot committed
126
    }
127

128
  outfile.close();
129

130 131
  return EXIT_SUCCESS;
}