From 448a826a0da01c06b46e63685031dc81414f8145 Mon Sep 17 00:00:00 2001
From: Remi Cresson <remi.cresson@inrae.fr>
Date: Fri, 21 Apr 2023 16:43:40 +0200
Subject: [PATCH] ADD: output background value

---
 app/otbTensorflowModelServe.cxx               | 12 ++++++++++-
 include/otbTensorflowMultisourceModelFilter.h | 21 +++++++++++--------
 .../otbTensorflowMultisourceModelFilter.hxx   |  2 +-
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx
index 6189e8f..59969e3 100644
--- a/app/otbTensorflowModelServe.cxx
+++ b/app/otbTensorflowModelServe.cxx
@@ -192,7 +192,12 @@ public:
     SetDefaultParameterFloat                 ("output.spcscale", 1.0);
     SetParameterDescription                  ("output.spcscale", "The output image size/scale and spacing*scale where size and spacing corresponds to the first input");
     AddParameter(ParameterType_StringList,    "output.names",    "Names of the output tensors");
-    MandatoryOff                            ("output.names");
+    MandatoryOff                             ("output.names");
+
+    // Output background value
+    AddParameter(ParameterType_Float,         "output.bv", "Output background value");
+    SetDefaultParameterFloat                 ("output.bv", 0.0);
+    SetParameterDescription                  ("output.bv", "The value used when one input has only no-data values in its receptive field");
 
     // Output Field of Expression
     AddParameter(ParameterType_Int,           "output.efieldx", "The output expression field (width)");
@@ -297,6 +302,11 @@ public:
       m_TFFilter->SetFullyConvolutional(true);
     }
 
+    // Output background value
+    const float outBV = GetParameterFloat("output.bv");
+    otbAppLogINFO("Setting background value to " << outBV);
+    m_TFFilter->SetOutputBackgroundValue(outBV);
+
     // Output field of expression
     FloatVectorImageType::SizeType foe;
     foe[0] = GetParameterInt("output.efieldx");
diff --git a/include/otbTensorflowMultisourceModelFilter.h b/include/otbTensorflowMultisourceModelFilter.h
index bdf9a02..ce855b2 100644
--- a/include/otbTensorflowMultisourceModelFilter.h
+++ b/include/otbTensorflowMultisourceModelFilter.h
@@ -132,6 +132,8 @@ public:
   itkGetMacro(FullyConvolutional, bool);
   itkSetMacro(OutputSpacingScale, float);
   itkGetMacro(OutputSpacingScale, float);
+  itkSetMacro(OutputBackgroundValue, OutputInternalPixelType);
+  itkGetMacro(OutputBackgroundValue, OutputInternalPixelType);
 
 protected:
   TensorflowMultisourceModelFilter();
@@ -162,17 +164,18 @@ private:
   void
   operator=(const Self &); // purposely not implemented
 
-  SizeType m_OutputGridSize;      // Output grid size
-  bool     m_ForceOutputGridSize; // Force output grid size
-  bool     m_FullyConvolutional;  // Convolution mode
-  float    m_OutputSpacingScale;  // scaling of the output spacings
+  SizeType                m_OutputGridSize;         // Output grid size
+  bool                    m_ForceOutputGridSize;    // Force output grid size
+  bool                    m_FullyConvolutional;     // Convolution mode
+  float                   m_OutputSpacingScale;     // scaling of the output spacings
+  OutputInternalPixelType m_OutputBackgroundValue;  // Output background value
 
   // Internal
-  SpacingType m_OutputSpacing; // Output image spacing
-  PointType   m_OutputOrigin;  // Output image origin
-  SizeType    m_OutputSize;    // Output image size
-  PixelType   m_NullPixel;     // Pixel filled with zeros
-
+  SpacingType             m_OutputSpacing;          // Output image spacing
+  PointType               m_OutputOrigin;           // Output image origin
+  SizeType                m_OutputSize;             // Output image size
+  PixelType               m_NullPixel;              // Pixel filled with zeros
+  
 }; // end class
 
 
diff --git a/include/otbTensorflowMultisourceModelFilter.hxx b/include/otbTensorflowMultisourceModelFilter.hxx
index 59105dd..0f32334 100644
--- a/include/otbTensorflowMultisourceModelFilter.hxx
+++ b/include/otbTensorflowMultisourceModelFilter.hxx
@@ -302,7 +302,7 @@ TensorflowMultisourceModelFilter<TInputImage, TOutputImage>::GenerateOutputInfor
 
   // Set null pixel
   m_NullPixel.SetSize(outputPtr->GetNumberOfComponentsPerPixel());
-  m_NullPixel.Fill(0);
+  m_NullPixel.Fill(m_OutputBackgroundValue);
 
   //////////////////////////////////////////////////////////////////////////////////////////
   //                        Set the tiling layout hint in metadata
-- 
GitLab