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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
* Copyright (C) 1999-2011 Insight Software Consortium
* Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
*
* 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.
*/
/* Example usage:
./ExtractROI Input/IMAGERY_SSECH.tif Output/ROI_IMAGERY_RGB.png Output/ROI_IMAGERY_MIR.png 0 0 100 100
*/
// This example shows the use of the
// \doxygen{otb}{MultiChannelExtractROI} and
// \doxygen{otb}{MultiToMonoChannelExtractROI} which allow the
// extraction of ROIs from multiband images stored into
// \doxygen{otb}{VectorImage}s. The first one povides a Vector Image
// as output, while the second one provides a classical
// \doxygen{otb}{Image} with a scalar pixel type. The present example
// shows how to extract a ROI from a 4-band SPOT 5 image and to
// produce a first multi-band 3-channel image and a second
// mono-channel one for the SWIR band.
// We start by including the needed header files.
// \index{otb::ExtractROI!header}
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "otbMultiChannelExtractROI.h"
#include "otbMultiToMonoChannelExtractROI.h"
int main(int argc, char* argv[])
{
// R\'ecuperation de arguments
if (argc < 7)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile outputImageFileRGB outputImageFileLastBand startX startY sizeX sizeY" << std::endl;
return EXIT_FAILURE;
}
// The program arguments define the image file names as well as the
// rectangular area to be extracted.
const char* inputFilename = argv[1];
const char* outputFilenameRGB = argv[2];
const char* outputFilenameMIR = argv[3];
unsigned int startX((unsigned int)::atoi(argv[4]));
unsigned int startY((unsigned int)::atoi(argv[5]));
unsigned int sizeX((unsigned int)::atoi(argv[6]));
unsigned int sizeY((unsigned int)::atoi(argv[7]));
// As usual, we define the input and output pixel types.
using InputPixelType = unsigned char;
using OutputPixelType = unsigned char;
// First of all, we extract the multiband part by using the
// \doxygen{otb}{MultiChannelExtractROI} class, which is templated
// over the input and output pixel types. This class in not
// templated over the images types in order to force these images
// to be of \doxygen{otb}{VectorImage} type.
using ExtractROIFilterType = otb::MultiChannelExtractROI<InputPixelType, OutputPixelType>;
// We create the extractor filter by using the \code{New} method of
// the class and we set its parameters.
ExtractROIFilterType::Pointer extractROIFilter = ExtractROIFilterType::New();
extractROIFilter->SetStartX(startX);
extractROIFilter->SetStartY(startY);
extractROIFilter->SetSizeX(sizeX);
extractROIFilter->SetSizeY(sizeY);
// We must tell the filter which are the channels to be used. When
// selecting contiguous bands, we can use the
// \code{SetFirstChannel} and the \code{SetLastChannel}. Otherwise,
// we select individual channels by using the \code{SetChannel} method.
extractROIFilter->SetFirstChannel(1);
extractROIFilter->SetLastChannel(3);
// We will use the OTB readers and writers for file access.
using ReaderType = otb::ImageFileReader<ExtractROIFilterType::InputImageType>;
using WriterType = otb::ImageFileWriter<ExtractROIFilterType::InputImageType>;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
// Since the number of bands of the input image is dynamically set
// at runtime, the \code{UpdateOutputInformation} method of the reader must be
// called before using the extractor filter.
reader->SetFileName(inputFilename);
reader->UpdateOutputInformation();
writer->SetFileName(outputFilenameRGB);
// We can then build the pipeline as usual.
extractROIFilter->SetInput(reader->GetOutput());
writer->SetInput(extractROIFilter->GetOutput());
// And execute the pipeline by calling the \code{Update} method of
// the writer.
writer->Update();
// The usage of the \doxygen{otb}{MultiToMonoChannelExtractROI} is
// similar to the one of the \doxygen{otb}{MultiChannelExtractROI}
// described above.
//
// The goal now is to extract an ROI from a multi-band image and
// generate a mono-channel image as output.
//
// We could use the \doxygen{otb}{MultiChannelExtractROI} and
// select a single channel, but using the
// \doxygen{otb}{MultiToMonoChannelExtractROI} we generate a
// \doxygen{otb}{Image} instead of an
// \doxygen{otb}{VectorImage}. This is useful from a computing and
// memory usage point of view.
// This class is also templated over the pixel types.
using ExtractROIMonoFilterType = otb::MultiToMonoChannelExtractROI<InputPixelType, OutputPixelType>;
ExtractROIMonoFilterType::Pointer extractROIMonoFilter = ExtractROIMonoFilterType::New();
extractROIMonoFilter->SetStartX(startX);
extractROIMonoFilter->SetStartY(startY);
extractROIMonoFilter->SetSizeX(sizeX);
extractROIMonoFilter->SetSizeY(sizeY);
// For this filter, only one output channel has to be selected.
extractROIMonoFilter->SetChannel(4);
using monoReaderType = otb::ImageFileReader<ExtractROIMonoFilterType::InputImageType>;
using monoWriterType = otb::ImageFileWriter<ExtractROIMonoFilterType::OutputImageType>;
monoReaderType::Pointer monoReader = monoReaderType::New();
monoWriterType::Pointer monoWriter = monoWriterType::New();
monoReader->SetFileName(inputFilename);
monoReader->Update(); // Needed to know the number of channels in the image
monoWriter->SetFileName(outputFilenameMIR);
extractROIMonoFilter->SetInput(monoReader->GetOutput());
monoWriter->SetInput(extractROIMonoFilter->GetOutput());
monoWriter->Update();
// \begin{figure}
// \center
// \includegraphics[width=0.44\textwidth]{IMAGERY_SSECH.eps}
// \itkcaption[Initial SPOT 5 image]{Quicklook of the original SPOT
// 5 image.}
// \label{fig:IMAGERY_SSECH}
// \end{figure}
//
// \begin{figure}
// \center
// \includegraphics[width=0.44\textwidth]{ROI_IMAGERY_RGB.eps}
// \includegraphics[width=0.44\textwidth]{ROI_IMAGERY_MIR.eps}
// \itkcaption[ROI of a SPOT5 image]{Result of the
// extraction. Left: 3-channel image. Right: mono-band image.}
// \label{fig:ROI_IMAGERY}
// \end{figure}
//
// Figure \ref{fig:ROI_IMAGERY} illustrates the result of the
// application of both extraction filters on the image presented in
// figure \ref{fig:IMAGERY_SSECH}.
return EXIT_SUCCESS;
}
|