KullbackLeiblerSupervizedDistanceChDet.cxx 3.78 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 20
 * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
 *
 * 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.
 */
21 22 23 24


#include <iostream>

25 26 27 28
#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "otbCommandProgressUpdate.h"
29 30 31

#include "otbKullbackLeiblerSupervizedDistanceImageFilter.h"

OTB Bot's avatar
OTB Bot committed
32
int main(int argc, char * argv[])
33
{
34
  try
OTB Bot's avatar
OTB Bot committed
35
    {
36
    if( argc < 5 )
OTB Bot's avatar
OTB Bot committed
37
      {
38 39 40
      std::cerr << "Usage: " << argv[0];
      std::cerr << "inputNameImage1 inputNameImage2 ROIImageName outputName ";
      std::cerr << "[winSize=35]";
41
      return EXIT_FAILURE;
OTB Bot's avatar
OTB Bot committed
42
      }
43
    /*
44
     *  Types declaration
45 46 47 48
     */

    const unsigned int Dimension = 2;

OTB Bot's avatar
OTB Bot committed
49
    typedef double        InputPixelType;
50 51
    typedef unsigned char TrainingPixelType;

OTB Bot's avatar
OTB Bot committed
52 53
    typedef otb::Image<InputPixelType, Dimension> ImageType;
    typedef otb::ImageFileReader<ImageType>       ReaderType;
54

OTB Bot's avatar
OTB Bot committed
55 56
    typedef otb::Image<TrainingPixelType, Dimension> TrainingImageType;
    typedef otb::ImageFileReader<TrainingImageType>  TrainingReaderType;
57 58

    /*
59
     * Extract command line parameters
60 61
     */

62 63 64 65
    std::string inputImageFileName1 = argv[1];
    std::string inputImageFileName2 = argv[2];
    std::string inputTrainingImageFileName = argv[3];
    std::string outputImageFileName = argv[4];
66 67

    int winSize = 35;
68
    if ( argc == 6 )
69
      winSize = std::stoi(argv[5]);
70 71 72 73 74

    /*
     *  JustDoIt
     */

OTB Bot's avatar
OTB Bot committed
75
    ImageType::Pointer  img1 = ImageType::New();
76
    ReaderType::Pointer reader1 = ReaderType::New();
OTB Bot's avatar
OTB Bot committed
77
    reader1->SetFileName(inputImageFileName1);
78 79 80
    //reader1->Update();
    img1 = reader1->GetOutput();

OTB Bot's avatar
OTB Bot committed
81
    ImageType::Pointer  img2 = ImageType::New();
82
    ReaderType::Pointer reader2 = ReaderType::New();
OTB Bot's avatar
OTB Bot committed
83
    reader2->SetFileName(inputImageFileName2);
84 85 86
    //reader2->Update();
    img2 = reader2->GetOutput();

OTB Bot's avatar
OTB Bot committed
87
    TrainingImageType::Pointer  imgRoi = TrainingImageType::New();
88
    TrainingReaderType::Pointer readerRoi = TrainingReaderType::New();
OTB Bot's avatar
OTB Bot committed
89
    readerRoi->SetFileName(inputTrainingImageFileName);
90 91 92 93
    //readerRoi->Update();
    imgRoi = readerRoi->GetOutput();

    typedef otb::KullbackLeiblerSupervizedDistanceImageFilter<
OTB Bot's avatar
OTB Bot committed
94
        ImageType, ImageType, TrainingImageType, ImageType> FilterType;
95

96
    FilterType::Pointer changeDetector = FilterType::New();
OTB Bot's avatar
OTB Bot committed
97 98 99
    changeDetector->SetRadius((winSize - 1) / 2);
    changeDetector->SetInput1(img1);
    changeDetector->SetInput2(img2);
100

101 102
    // Once img1 and img2 are connected, it is time to connect imgRoi
    // to perform the parameters estimation of the non-changed area.
103

OTB Bot's avatar
OTB Bot committed
104
    changeDetector->SetTrainingArea(imgRoi);
105

OTB Bot's avatar
OTB Bot committed
106
    typedef otb::CommandProgressUpdate<FilterType> CommandType;
107
    CommandType::Pointer observer = CommandType::New();
OTB Bot's avatar
OTB Bot committed
108
    changeDetector->AddObserver(itk::ProgressEvent(), observer);
109

OTB Bot's avatar
OTB Bot committed
110
    typedef otb::ImageFileWriter<ImageType> WriterType;
111
    WriterType::Pointer writer = WriterType::New();
OTB Bot's avatar
OTB Bot committed
112 113
    writer->SetFileName(outputImageFileName);
    writer->SetInput(changeDetector->GetOutput());
114

115
    writer->Update();
116

OTB Bot's avatar
OTB Bot committed
117 118 119
    }
  catch (itk::ExceptionObject& err)
    {
120 121 122
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl << std::endl;
    return EXIT_FAILURE;
OTB Bot's avatar
OTB Bot committed
123
    }
124 125

  return EXIT_SUCCESS;
126
}