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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbPrintableImageFilter_h
#define otbPrintableImageFilter_h
#include "otbImage.h"
#include "itkImageToImageFilter.h"
#include "otbVectorRescaleIntensityImageFilter.h"
#include "otbMultiChannelExtractROI.h"
#include "itkBinaryFunctorImageFilter.h"
namespace otb
{
namespace Functor
{
/**
* \class MaskFunctor
* \brief Output is a InputPixel if MaskPixel is m_Background and a
* defined other value (m_ObjectColor) otherwise.
*
* \ingroup OTBImageManipulation
*/
template<class TInputPixel, class TMaskPixel, class TOutputPixel>
class ITK_EXPORT MaskFunctor
{
public:
MaskFunctor()
{
m_BackgroundValue = 0;
m_ObjectColor.SetSize(3);
m_ObjectColor.Fill(255);
};
~MaskFunctor(){}
typedef TInputPixel InputPixelType;
typedef TMaskPixel MaskPixelType;
typedef TOutputPixel OutputPixelType;
typedef typename OutputPixelType::ValueType OutputInternalPixelType;
MaskPixelType GetBackgroundValue()
{
return m_BackgroundValue;
}
void SetBackgroundValue(MaskPixelType val)
{
m_BackgroundValue = val;
}
OutputPixelType GetObjectColor()
{
return m_ObjectColor;
}
void SetObjectColor(OutputPixelType val)
{
m_ObjectColor = val;
}
inline OutputPixelType operator ()(InputPixelType inPix, MaskPixelType maskPix) const
{
OutputPixelType outPix;
if (maskPix == m_BackgroundValue)
{
outPix.SetSize(inPix.Size());
for (unsigned int i = 0; i < outPix.Size(); ++i)
{
outPix[i] = static_cast<OutputInternalPixelType>(inPix[i]);
}
}
else
{
outPix = m_ObjectColor;
}
return outPix;
}
protected:
MaskPixelType m_BackgroundValue;
OutputPixelType m_ObjectColor;
};
}
/**
* \class PrintableImageFilter
* \brief This class is a helper class to turn a vector image to a generic 8 bytes RGB image.
* A mask can be used to highlight some objects represented by the same value.
* The mask is a binary image. Background MaskValue is used to precise which
* value of the mask are objects (default 0).
* Output object color can be set using m_ObjectColor (default white).
* The output is a 3 channel image, each channel is a channel of the input image.
* They can be selected using m_ChannelList or SetChannel(int ch ) method.
*
* It is useful for publications for instance.
*
* \sa itkImageToImageFilter
*
* \ingroup OTBImageManipulation
**/
template <class TInputImage, class TMaskImage = otb::Image<unsigned char, 2> >
class ITK_EXPORT PrintableImageFilter :
public itk::ImageToImageFilter<TInputImage, otb::VectorImage<unsigned char, 2> >
{
public:
typedef PrintableImageFilter Self;
typedef itk::ImageToImageFilter
<TInputImage, otb::VectorImage<unsigned char, 2> > Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInputImage InputImageType;
typedef typename InputImageType::PixelType InputPixelType;
typedef typename InputImageType::InternalPixelType InputInternalPixelType;
// typedef typename itk::NumericTraits<InputPixelType>::ValueType InputInternalPixelType;
typedef unsigned char OutputInternalPixelType;
typedef VectorImage<OutputInternalPixelType, 2> OutputImageType;
typedef OutputImageType::PixelType OutputPixelType;
typedef TMaskImage MaskImageType;
typedef typename MaskImageType::Pointer MaskImagePointerType;
typedef typename MaskImageType::PixelType MaskPixelType;
typedef otb::MultiChannelExtractROI<InputInternalPixelType,
InputInternalPixelType> ChannelExtractorType;
typedef typename ChannelExtractorType::ChannelsType ChannelsType;
typedef VectorRescaleIntensityImageFilter
<typename ChannelExtractorType::OutputImageType, OutputImageType> VectorRescalerType;
typedef Functor::MaskFunctor<InputPixelType, MaskPixelType, OutputPixelType> FunctorType;
typedef itk::BinaryFunctorImageFilter<OutputImageType, MaskImageType,
OutputImageType, FunctorType> FunctorFilterType;
typedef typename FunctorFilterType::Pointer FunctorFilterPointerType;
/** Method for creation through object factory */
itkNewMacro(Self);
/** Run-time type information */
itkTypeMacro(PrintableImageFilter,
itk::ImageToImageFilter);
/** Display */
void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE;
void SetChannel(unsigned int channel);
const ChannelsType GetChannels(void) const;
otbSetObjectMemberMacro(Rescaler, AutomaticInputMinMaxComputation, bool);
otbGetObjectMemberMacro(Rescaler, AutomaticInputMinMaxComputation, bool);
otbSetObjectMemberMacro(Rescaler, InputMinimum, InputPixelType);
otbGetObjectMemberMacro(Rescaler, InputMinimum, InputPixelType);
otbSetObjectMemberMacro(Rescaler, InputMaximum, InputPixelType);
otbGetObjectMemberMacro(Rescaler, InputMaximum, InputPixelType);
/**
* If set, only pixels within the mask will be classified.
* \param mask The input mask.
*/
void SetInputMask(const MaskImageType * mask);
/**
* Get the input mask.
* \return The mask.
*/
MaskImageType * GetInputMask(void);
itkSetMacro(UseMask, bool);
itkGetMacro(UseMask, bool);
ChannelsType const GetChannelList()
{
return m_ChannelList;
}
/* Set the selected channel index (order is important) */
void SetChannelList(ChannelsType chList)
{
if (chList.size() != 3)
{
itkExceptionMacro(<< "Invalid channel list, size is " << chList.size() << " instead of 3");
}
m_ChannelList = chList;
this->Modified();
}
/** Output Mask Object color. */
void SetObjectColor(OutputPixelType val)
{
if (val.GetSize() != 3)
{
itkExceptionMacro(<< "Invalid object color, size is " << val.Size() << " instead of 3");
}
m_ObjectColor = val;
m_MaskFilter->GetFunctor().SetObjectColor(val);
this->Modified();
}
itkGetMacro(ObjectColor, OutputPixelType);
void SetBackgroundMaskValue(MaskPixelType val)
{
m_BackgroundMaskValue = val;
m_MaskFilter->GetFunctor().SetBackgroundValue(val);
this->Modified();
}
itkGetMacro(BackgroundMaskValue, MaskPixelType);
/** PrintableImageFilter can produce an image which is a different
* resolution than its input image. As such, PrintableImageFilter
* needs to provide an implementation for
* GenerateOutputInformation() in order to inform the pipeline
* execution model. The original documentation of this method is
* below.
*
* \sa ProcessObject::GenerateOutputInformaton() */
void GenerateOutputInformation() ITK_OVERRIDE;
protected:
PrintableImageFilter();
void BeforeGenerateData();
void GenerateData() ITK_OVERRIDE;
private:
PrintableImageFilter(Self &); // intentionally not implemented
void operator =(const Self&); // intentionally not implemented
typename VectorRescalerType::Pointer m_Rescaler;
typename ChannelExtractorType::Pointer m_Extractor;
// Foreground mask value
FunctorFilterPointerType m_MaskFilter;
// Objects (of the mask) will be displayer with the chosen color.
OutputPixelType m_ObjectColor;
// Use mask
bool m_UseMask;
// Used channel for output Image
ChannelsType m_ChannelList;
// Foreground mask value
//MaskPixelType m_ForegroundMaskValue;
// Background mask value
MaskPixelType m_BackgroundMaskValue;
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbPrintableImageFilter.txx"
#endif
#endif
|