Commit 5f089618 authored by Emmanuel Christophe's avatar Emmanuel Christophe

BUG: fix occasional segfault in seam carving

parent a5f3c65a
......@@ -61,7 +61,7 @@ class ITK_EXPORT AddCarvingPathFilter : public itk::ImageAndPathToImageFilter<TI
{
public:
/** Standard class typedefs. */
typedef AddCarvingPathFilter Self;
typedef AddCarvingPathFilter Self;
typedef itk::ImageAndPathToImageFilter<TInputImage,TInputPath,TOutputImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
......@@ -136,4 +136,3 @@ private:
#endif
#endif
......@@ -135,7 +135,9 @@ AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
}
}
if ( (flag==true) || ((index[dir0] != indexToAdd[dir0]) && (index[dir0] < static_cast<IndexValueType>(inputSize[dir0]))))
if ( (flag==true)
|| ((index[dir0] != indexToAdd[dir0])
&& (index[dir0] < static_cast<IndexValueType>(inputSize[dir0]))))
{
outputIterator.Set(inputIterator.Get());
++inputIterator;
......@@ -143,11 +145,24 @@ AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
else
{
flag = true;
OutputImagePixelType newValue;
newValue = (--inputIterator).Get();
newValue += (++inputIterator).Get();
newValue /= 2;
// newValue = 0; //TODO just for test
OutputImagePixelType newValue = itk::NumericTraits<OutputImagePixelType>::Zero;
int n = 0;
InputIteratorType tmpIterator = inputIterator;
--tmpIterator;
if (!tmpIterator.IsAtReverseEndOfLine())
{
newValue = tmpIterator.Get();
++n;
}
tmpIterator = inputIterator;
++tmpIterator;
if (!tmpIterator.IsAtEndOfLine())
{
newValue += tmpIterator.Get();
++n;
}
assert(n != 0);
newValue /= n;
outputIterator.Set(newValue);
}
++outputIterator;
......@@ -156,7 +171,8 @@ AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
if ((outputIterator.GetIndex())[dir0] != (inputIterator.GetIndex())[dir0]+1)
{
itkExceptionMacro(<< "Error 2: "<< (outputIterator.GetIndex())[dir0] << " , " << (inputIterator.GetIndex())[dir0]);
itkExceptionMacro(<< "Error 2: "<< (outputIterator.GetIndex())[dir0] << " , "
<< (inputIterator.GetIndex())[dir0]);
}
inputIterator.NextLine();
outputIterator.NextLine();
......@@ -182,7 +198,6 @@ AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
}
template <class TInputImage, class TInputPath,class TOutputImage>
void
AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
......@@ -263,4 +278,3 @@ AddCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
} // end namespace otb
#endif
......@@ -58,12 +58,12 @@ class ITK_EXPORT ImageToCarvingPathFilter
{
public:
/** standards typedefs */
typedef ImageToCarvingPathFilter Self;
typedef ImageToCarvingPathFilter Self;
typedef ImageToPathFilter<TInputImage, TOutputPath> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/// Creation througth the object factory
/// Creation through the object factory
itkNewMacro(Self);
/// Runtime information
itkTypeMacro(ImageToCarvingPathFilter,ImageToPathFilter);
......@@ -77,7 +77,6 @@ public:
typedef typename InputImageType::PixelType PixelType;
/** Set and Get foreground value */
itkSetMacro(ForegroundValue,PixelType);
itkGetConstMacro(ForegroundValue,PixelType);
......@@ -100,9 +99,9 @@ private:
ImageToCarvingPathFilter(const Self&); // purposely not implemented
void operator=(const Self&); // purposely not implemented
PixelType m_ForegroundValue;
PixelType m_ForegroundValue;
unsigned int m_Direction;
double m_EnergyPerPix;
double m_EnergyPerPix;
};
} // end namespace otb
......
......@@ -39,6 +39,7 @@ ImageToCarvingPathFilter<TInputImage, TOutputPath>
m_Direction = 0;
m_EnergyPerPix = 0.0;
}
/**
* Main computation method.
*/
......@@ -170,8 +171,6 @@ ImageToCarvingPathFilter<TInputImage, TOutputPath>
}
/** Follow the minima bottom-up or right-left
* (dynamic programming second step) */
......
......@@ -127,7 +127,7 @@ private:
void operator=(const Self&); //purposely not implemented
OutputImagePixelType m_Value;
unsigned int m_Direction;
unsigned int m_Direction;
};
} // end namespace otb
......@@ -137,4 +137,3 @@ private:
#endif
#endif
......@@ -220,9 +220,6 @@ RemoveCarvingPathFilter<TInputImage,TInputPath,TOutputImage>
outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
}
} // end namespace otb
#endif
......@@ -60,18 +60,17 @@
int main(int argc, char * argv[])
{
typedef float InputPixelType;
typedef float InputPixelType;
typedef unsigned char OutputPixelType;
const unsigned int Dimension = 2;
typedef otb::Image< InputPixelType, Dimension > ImageType;
typedef otb::Image< InputPixelType, Dimension > ImageType;
typedef otb::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::PolyLineParametricPath<Dimension> PathType;
typedef itk::PolyLineParametricPath<Dimension> PathType;
typedef otb::ImageFileReader< ImageType > ReaderType;
typedef otb::ImageFileWriter< OutputImageType > WriterType;
typedef itk::RescaleIntensityImageFilter
<ImageType, OutputImageType> RescalerType;
typedef otb::ImageFileReader< ImageType > ReaderType;
typedef otb::ImageFileWriter< OutputImageType > WriterType;
typedef itk::RescaleIntensityImageFilter<ImageType, OutputImageType> RescalerType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
......@@ -250,4 +249,3 @@ int main(int argc, char * argv[])
return EXIT_SUCCESS;
}
......@@ -61,13 +61,13 @@
int main(int argc, char * argv[])
{
typedef float InputPixelType;
typedef float InputPixelType;
typedef unsigned char OutputPixelType;
const unsigned int Dimension = 2;
typedef otb::Image< InputPixelType, Dimension > ImageType;
typedef otb::Image< InputPixelType, Dimension > ImageType;
typedef otb::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::PolyLineParametricPath<Dimension> PathType;
typedef itk::PolyLineParametricPath<Dimension> PathType;
// Software Guide : BeginLatex
//
......@@ -81,10 +81,9 @@ int main(int argc, char * argv[])
PathListType::Pointer pathList = PathListType::New();
// Software Guide : EndCodeSnippet
typedef otb::ImageFileReader< ImageType > ReaderType;
typedef otb::ImageFileWriter< OutputImageType > WriterType;
typedef itk::RescaleIntensityImageFilter
<ImageType, OutputImageType> RescalerType;
typedef otb::ImageFileReader< ImageType > ReaderType;
typedef otb::ImageFileWriter< OutputImageType > WriterType;
typedef itk::RescaleIntensityImageFilter<ImageType, OutputImageType> RescalerType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
......@@ -224,4 +223,3 @@ int main(int argc, char * argv[])
return EXIT_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment