Multispectral.cxx 6.12 KB
 Sébastien Dinot committed Mar 08, 2017 1 /*  Julien Michel committed Jan 14, 2019 2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)  Sébastien Dinot committed Mar 08, 2017 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. */  Emmanuel Christophe committed Feb 22, 2008 20 21   Victor Poughon committed Feb 20, 2019 22 23 24 25 /* Example usage: ./Multispectral Input/qb_RoadExtract.tif Output/qb_blue.tif Output/qb_shiftscale.tif */  Emmanuel Christophe committed Feb 22, 2008 26 27  // First, we are going to use \doxygen{otb}{VectorImage} instead of the now  Manuel Grizonnet committed Sep 15, 2016 28 // traditional \doxygen{otb}{Image}. So we include the required header:  Emmanuel Christophe committed Feb 22, 2008 29 30 31  // We also include some other header which will be useful later. Note that we  Emmanuel Christophe committed Dec 06, 2008 32 // are still using the \doxygen{otb}{Image} in this example for some of the  Emmanuel Christophe committed Feb 22, 2008 33 34 35 // output. #include "otbImageFileReader.h"  Julien Michel committed Jan 08, 2013 36 #include "otbImageFileWriter.h"  Emmanuel Christophe committed Feb 22, 2008 37 38 39 40 #include "otbMultiToMonoChannelExtractROI.h" #include "itkShiftScaleImageFilter.h" #include "otbPerBandVectorImageFilter.h"  Victor Poughon committed Feb 20, 2019 41 int main(int argc, char* argv[])  Emmanuel Christophe committed Feb 22, 2008 42 {  Julien Malik committed Jun 08, 2011 43  if (argc != 4)  Victor Poughon committed Feb 20, 2019 44 45 46 47  { std::cerr << "Usage: " << argv[0] << " " << std::endl; }  Emmanuel Christophe committed Feb 22, 2008 48  // We want to read a multispectral image so we declare the image type and the  Emmanuel Christophe committed Dec 06, 2008 49 50 51  // reader. As we have done in the previous example we get the filename from // the command line.  Victor Poughon committed May 16, 2019 52 53  using PixelType = unsigned short; using VectorImageType = otb::VectorImage;  Emmanuel Christophe committed Feb 22, 2008 54   Victor Poughon committed May 16, 2019 55 56  using ReaderType = otb::ImageFileReader; ReaderType::Pointer reader = ReaderType::New();  Emmanuel Christophe committed Dec 06, 2008 57   Emmanuel Christophe committed Feb 22, 2008 58  reader->SetFileName(argv[1]);  Emmanuel Christophe committed Dec 06, 2008 59   Emmanuel Christophe committed Feb 22, 2008 60  // Sometime, you need to process only one spectral band of the image. To get  Emmanuel Christophe committed Dec 06, 2008 61  // only one of the spectral band we use the  Guillaume Pasero committed Feb 02, 2015 62  // \doxygen{otb}{MultiToMonoChannelExtractROI}. The declaration is as usual:  Victor Poughon committed Feb 20, 2019 63   Victor Poughon committed May 16, 2019 64 65  using ExtractChannelType = otb::MultiToMonoChannelExtractROI; ExtractChannelType::Pointer extractChannel = ExtractChannelType::New();  Emmanuel Christophe committed Dec 06, 2008 66  // We need to pass the parameters to the filter for the extraction. This  Rashad Kanavath committed Jan 04, 2016 67  // filter also allow extracting only a spatial subset of the image. However,  Emmanuel Christophe committed Feb 22, 2008 68 69  // we will extract the whole channel in this case. //  Emmanuel Christophe committed Dec 06, 2008 70 71  // To do that, we need to pass the desired region using the // \code{SetExtractionRegion()} (method such as \code{SetStartX},  Emmanuel Christophe committed Feb 22, 2008 72  // \code{SetSizeX} are also available). We get the region from the reader with  Emmanuel Christophe committed Dec 06, 2008 73 74 75  // the \code{GetLargestPossibleRegion()} method. Before doing that we need to // read the metadata from the file: this is done by calling the // \code{UpdateOutputInformation()} on the reader's output. The difference with the  Emmanuel Christophe committed Feb 22, 2008 76 77  // \code{Update()} is that the pixel array is not allocated (yet !) and reduce // the memory usage.  Emmanuel Christophe committed Dec 06, 2008 78   Emmanuel Christophe committed Feb 22, 2008 79  reader->UpdateOutputInformation();  Victor Poughon committed Feb 20, 2019 80  extractChannel->SetExtractionRegion(reader->GetOutput()->GetLargestPossibleRegion());  Emmanuel Christophe committed Dec 06, 2008 81 82  // We chose the channel number to extract (starting from 1) and we plug the  Emmanuel Christophe committed Feb 22, 2008 83  // pipeline.  Emmanuel Christophe committed Dec 06, 2008 84   Emmanuel Christophe committed Feb 22, 2008 85 86  extractChannel->SetChannel(3); extractChannel->SetInput(reader->GetOutput());  Emmanuel Christophe committed Dec 06, 2008 87   Emmanuel Christophe committed Feb 22, 2008 88  // To output this image, we need a writer. As the output of the  Emmanuel Christophe committed Dec 06, 2008 89  // \doxygen{otb}{MultiToMonoChannelExtractROI} is a \doxygen{otb}{Image}, we  Emmanuel Christophe committed Feb 22, 2008 90  // need to template the writer with this type.  Emmanuel Christophe committed Dec 06, 2008 91   Victor Poughon committed May 16, 2019 92 93 94  using ImageType = otb::Image; using WriterType = otb::ImageFileWriter; WriterType::Pointer writer = WriterType::New();  Emmanuel Christophe committed Feb 22, 2008 95 96 97  writer->SetFileName(argv[2]); writer->SetInput(extractChannel->GetOutput());  Emmanuel Christophe committed Dec 06, 2008 98   Emmanuel Christophe committed Feb 22, 2008 99  writer->Update();  Emmanuel Christophe committed Dec 06, 2008 100 101  // After this, we have a one band image that we can process with most OTB  Emmanuel Christophe committed Feb 22, 2008 102 103  // filters. //  Emmanuel Christophe committed Dec 06, 2008 104  // In some situation, you may want to apply the same process to all bands of  Emmanuel Christophe committed Feb 22, 2008 105 106 107 108 109 110 111  // the image. You don't have to extract each band and process them separately. // There is several situations: // // \begin{itemize} // \item the filter (or the combination of filters) you want to use are doing // operations that are well defined for \doxygen{itk}{VariableLengthVector} // (which is the pixel type), then you don't have to do anything special.  Emmanuel Christophe committed Dec 06, 2008 112  // \item if this is not working, you can look for the equivalent filter  Emmanuel Christophe committed Feb 22, 2008 113 114  // specially designed for vector images. // \item some of the filter you need to use applies operations undefined for  Emmanuel Christophe committed Dec 06, 2008 115 116  // \doxygen{itk}{VariableLengthVector}, then you can use the // \doxygen{otb}{PerBandVectorImageFilter} specially designed for this  Emmanuel Christophe committed Feb 22, 2008 117 118 119 120 121  // purpose. // \end{itemize} // // Let's see how this filter is working. We chose to apply the // \doxygen{itk}{ShiftScaleImageFilter} to each of the spectral band. We start  Emmanuel Christophe committed Dec 06, 2008 122  // by declaring the filter on a normal \doxygen{otb}{Image}. Note that we  Emmanuel Christophe committed Feb 22, 2008 123  // don't need to specify any input for this filter.  Emmanuel Christophe committed Dec 06, 2008 124   Victor Poughon committed May 16, 2019 125 126  using ShiftScaleType = itk::ShiftScaleImageFilter; ShiftScaleType::Pointer shiftScale = ShiftScaleType::New();  Emmanuel Christophe committed Feb 22, 2008 127 128  shiftScale->SetScale(0.5); shiftScale->SetShift(10);  Emmanuel Christophe committed Dec 06, 2008 129 130  // We declare the \doxygen{otb}{PerBandVectorImageFilter} which has three  Emmanuel Christophe committed Feb 22, 2008 131 132 133 134 135  // template: the input image type, the output image type and the filter type // to apply to each band. // // The filter is selected using the \code{SetFilter()} method and the input // by the usual \code{SetInput()} method.  Emmanuel Christophe committed Dec 06, 2008 136   Victor Poughon committed May 16, 2019 137 138  using VectorFilterType = otb::PerBandVectorImageFilter; VectorFilterType::Pointer vectorFilter = VectorFilterType::New();  Emmanuel Christophe committed Feb 22, 2008 139  vectorFilter->SetFilter(shiftScale);  Emmanuel Christophe committed Dec 06, 2008 140   Emmanuel Christophe committed Feb 22, 2008 141  vectorFilter->SetInput(reader->GetOutput());  Emmanuel Christophe committed Dec 06, 2008 142   Emmanuel Christophe committed Feb 22, 2008 143 144  // Now, we just have to save the image using a writer templated over an // \doxygen{otb}{VectorImage}:  Emmanuel Christophe committed Dec 06, 2008 145   Victor Poughon committed May 16, 2019 146 147  using VectorWriterType = otb::ImageFileWriter; VectorWriterType::Pointer writerVector = VectorWriterType::New();  Emmanuel Christophe committed Feb 22, 2008 148 149 150  writerVector->SetFileName(argv[3]); writerVector->SetInput(vectorFilter->GetOutput());  Emmanuel Christophe committed Dec 06, 2008 151   Emmanuel Christophe committed Feb 22, 2008 152  writerVector->Update();  Emmanuel Christophe committed Dec 06, 2008 153 154  return EXIT_SUCCESS;  Emmanuel Christophe committed Feb 22, 2008 155 }