2020-01-30 00:51:18 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.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 notice for more information.
|
|
|
|
//============================================================================
|
|
|
|
#ifndef vtk_m_filter_ImageDifference_hxx
|
|
|
|
#define vtk_m_filter_ImageDifference_hxx
|
|
|
|
|
|
|
|
#include <vtkm/filter/ImageDifference.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/Algorithm.h>
|
2021-07-15 14:53:52 +00:00
|
|
|
#include <vtkm/cont/ArrayCopy.h>
|
2020-01-30 00:51:18 +00:00
|
|
|
#include <vtkm/cont/ArrayPortalToIterators.h>
|
|
|
|
#include <vtkm/cont/Logging.h>
|
|
|
|
|
|
|
|
#include <vtkm/worklet/AveragePointNeighborhood.h>
|
|
|
|
#include <vtkm/worklet/ImageDifference.h>
|
|
|
|
|
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace filter
|
|
|
|
{
|
|
|
|
|
2021-01-05 20:09:01 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
struct GreaterThanThreshold
|
|
|
|
{
|
|
|
|
GreaterThanThreshold(const vtkm::FloatDefault& thresholdError)
|
|
|
|
: ThresholdError(thresholdError)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
VTKM_EXEC_CONT bool operator()(const vtkm::FloatDefault& x) const { return x > ThresholdError; }
|
|
|
|
vtkm::FloatDefault ThresholdError;
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
2020-01-30 00:51:18 +00:00
|
|
|
inline VTKM_CONT ImageDifference::ImageDifference()
|
|
|
|
: vtkm::filter::FilterField<ImageDifference>()
|
2021-01-05 20:09:01 +00:00
|
|
|
, AverageRadius(0)
|
|
|
|
, PixelShiftRadius(0)
|
2021-03-18 20:46:03 +00:00
|
|
|
, AllowedPixelErrorRatio(0.00025f)
|
2021-01-05 20:09:01 +00:00
|
|
|
, PixelDiffThreshold(0.05f)
|
2020-01-30 00:51:18 +00:00
|
|
|
, ImageDiffWithinThreshold(true)
|
|
|
|
, SecondaryFieldName("image-2")
|
|
|
|
, SecondaryFieldAssociation(vtkm::cont::Field::Association::ANY)
|
|
|
|
, ThresholdFieldName("threshold-output")
|
|
|
|
{
|
|
|
|
this->SetPrimaryField("image-1");
|
|
|
|
this->SetOutputFieldName("image-diff");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename StorageType, typename DerivedPolicy>
|
|
|
|
inline VTKM_CONT vtkm::cont::DataSet ImageDifference::DoExecute(
|
|
|
|
const vtkm::cont::DataSet& input,
|
|
|
|
const vtkm::cont::ArrayHandle<T, StorageType>& primary,
|
|
|
|
const vtkm::filter::FieldMetadata& fieldMetadata,
|
|
|
|
vtkm::filter::PolicyBase<DerivedPolicy> policy)
|
|
|
|
{
|
2020-05-28 20:36:15 +00:00
|
|
|
this->ImageDiffWithinThreshold = true;
|
2020-01-30 00:51:18 +00:00
|
|
|
if (!fieldMetadata.IsPointField())
|
|
|
|
{
|
|
|
|
throw vtkm::cont::ErrorFilterExecution("Point field expected.");
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Performing Image Difference");
|
|
|
|
|
|
|
|
vtkm::cont::Field secondaryField;
|
|
|
|
secondaryField = input.GetField(this->SecondaryFieldName, this->SecondaryFieldAssociation);
|
|
|
|
auto secondary = vtkm::filter::ApplyPolicyFieldOfType<T>(secondaryField, policy, *this);
|
|
|
|
|
|
|
|
auto cellSet = vtkm::filter::ApplyPolicyCellSetStructured(input.GetCellSet(), policy, *this);
|
|
|
|
vtkm::cont::ArrayHandle<T, StorageType> diffOutput;
|
|
|
|
vtkm::cont::ArrayHandle<T, StorageType> primaryOutput;
|
|
|
|
vtkm::cont::ArrayHandle<T, StorageType> secondaryOutput;
|
2020-12-09 22:56:06 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::FloatDefault> thresholdOutput;
|
2020-01-30 00:51:18 +00:00
|
|
|
|
2021-01-05 20:09:01 +00:00
|
|
|
if (this->AverageRadius > 0)
|
2020-01-30 00:51:18 +00:00
|
|
|
{
|
2021-01-05 20:09:01 +00:00
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info,
|
|
|
|
"Performing Average with radius: " << this->AverageRadius);
|
|
|
|
auto averageWorklet = vtkm::worklet::AveragePointNeighborhood(this->AverageRadius);
|
2020-01-30 00:51:18 +00:00
|
|
|
this->Invoke(averageWorklet, cellSet, primary, primaryOutput);
|
|
|
|
this->Invoke(averageWorklet, cellSet, secondary, secondaryOutput);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Not performing average");
|
|
|
|
primaryOutput = primary;
|
2021-07-15 14:53:52 +00:00
|
|
|
vtkm::cont::ArrayCopyShallowIfPossible(secondaryField.GetData(), secondaryOutput);
|
2020-01-30 00:51:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 20:09:01 +00:00
|
|
|
if (this->PixelShiftRadius > 0)
|
2020-01-30 00:51:18 +00:00
|
|
|
{
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Diffing image in Neighborhood");
|
2021-01-05 20:09:01 +00:00
|
|
|
auto diffWorklet =
|
|
|
|
vtkm::worklet::ImageDifferenceNeighborhood(this->PixelShiftRadius, this->PixelDiffThreshold);
|
2020-01-30 00:51:18 +00:00
|
|
|
this->Invoke(diffWorklet, cellSet, primaryOutput, secondaryOutput, diffOutput, thresholdOutput);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Diffing image directly");
|
|
|
|
auto diffWorklet = vtkm::worklet::ImageDifference();
|
|
|
|
this->Invoke(diffWorklet, primaryOutput, secondaryOutput, diffOutput, thresholdOutput);
|
|
|
|
}
|
|
|
|
|
2021-01-05 20:09:01 +00:00
|
|
|
|
2021-01-14 20:12:57 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::FloatDefault> errorPixels;
|
2021-01-05 20:09:01 +00:00
|
|
|
vtkm::cont::Algorithm::CopyIf(thresholdOutput,
|
|
|
|
thresholdOutput,
|
|
|
|
errorPixels,
|
|
|
|
detail::GreaterThanThreshold(this->PixelDiffThreshold));
|
|
|
|
if (errorPixels.GetNumberOfValues() >
|
|
|
|
thresholdOutput.GetNumberOfValues() * this->AllowedPixelErrorRatio)
|
2020-01-30 00:51:18 +00:00
|
|
|
{
|
|
|
|
this->ImageDiffWithinThreshold = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_LOG_S(vtkm::cont::LogLevel::Info,
|
2021-01-05 20:09:01 +00:00
|
|
|
"Difference within threshold: "
|
|
|
|
<< this->ImageDiffWithinThreshold
|
|
|
|
<< ", for pixels outside threshold: " << errorPixels.GetNumberOfValues()
|
2021-03-17 18:57:46 +00:00
|
|
|
<< ", with a total number of pixesl: " << thresholdOutput.GetNumberOfValues()
|
|
|
|
<< ", and an allowable pixel error ratio: " << this->AllowedPixelErrorRatio
|
|
|
|
<< ", with a total summed threshold error: "
|
2021-01-05 20:09:01 +00:00
|
|
|
<< vtkm::cont::Algorithm::Reduce(errorPixels, static_cast<FloatDefault>(0)));
|
2020-01-30 00:51:18 +00:00
|
|
|
|
|
|
|
vtkm::cont::DataSet clone;
|
|
|
|
clone.CopyStructure(input);
|
|
|
|
clone.AddField(fieldMetadata.AsField(this->GetOutputFieldName(), diffOutput));
|
|
|
|
clone.AddField(fieldMetadata.AsField(this->GetThresholdFieldName(), thresholdOutput));
|
|
|
|
|
|
|
|
VTKM_ASSERT(clone.HasField(this->GetOutputFieldName(), fieldMetadata.GetAssociation()));
|
|
|
|
VTKM_ASSERT(clone.HasField(this->GetThresholdFieldName(), fieldMetadata.GetAssociation()));
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace filter
|
|
|
|
} // namespace vtkm
|
|
|
|
|
|
|
|
#endif // vtk_m_filter_ImageDifference_hxx
|