Skip to content

Commit

Permalink
Added conversion to fiji montage as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Aug 6, 2024
1 parent fc48f11 commit 86a5ad8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "ITKImportFijiMontage.hpp"

#include "ITKImageProcessing/Common/ITKArrayHelper.hpp"
#include "ITKImageProcessing/Common/ReadImageUtils.hpp"
#include "ITKImageProcessing/Filters/ITKImageReaderFilter.hpp"

Expand Down Expand Up @@ -201,7 +200,7 @@ class IOHandler

// Ensure that we are dealing with in-core memory ONLY
const IDataArray* inputArrayPtr = m_DataStructure.getDataAs<IDataArray>(imageDataPath);
if(inputArrayPtr->getDataFormat() != "")
if(!inputArrayPtr->getDataFormat().empty())
{
return MakeErrorResult(-9999, fmt::format("Input Array '{}' utilizes out-of-core data. This is not supported within ITK filters.", imageDataPath.toString()));
}
Expand All @@ -213,8 +212,17 @@ class IOHandler
image->setSpacing(FloatVec3(1.0f, 1.0f, 1.0f));

// Use ITKUtils to read the image into the DataStructure
Result<> imageReaderResult =
cxItkImageReaderFilter::ReadImageExecute<cxItkImageReaderFilter::ReadImageIntoArrayFunctor>(bound.Filepath.string(), m_DataStructure, imageDataPath, bound.Filepath.string());
Result<> imageReaderResult;
if(m_InputValues->changeDataType)
{
imageReaderResult = cxItkImageReaderFilter::ReadImageExecute<cxItkImageReaderFilter::ReadImageIntoArrayFunctor>(bound.Filepath.string(), m_DataStructure, bound.Filepath.string(),
imageDataPath, m_InputValues->destType);
}
else
{
imageReaderResult =
cxItkImageReaderFilter::ReadImageExecute<cxItkImageReaderFilter::ReadImageIntoArrayFunctor>(bound.Filepath.string(), m_DataStructure, imageDataPath, bound.Filepath.string());
}
if(imageReaderResult.invalid())
{
for(const auto& error : imageReaderResult.errors())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct ITKIMAGEPROCESSING_EXPORT ITKImportFijiMontageInputValues
bool changeOrigin = false;
bool convertToGrayScale = false;
bool parentDataGroup = false;
bool changeDataType = false;
DataType destType = DataType::uint8;
fs::path inputFilePath = {};
IGeometry::LengthUnit lengthUnit = IGeometry::LengthUnit::Micrometer;
std::vector<float32> origin = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ IFilter::PreflightResult ITKImageReaderFilter::preflightImpl(const DataStructure
auto origin = filterArgs.value<VectorFloat64Parameter::ValueType>(k_Origin_Key);
auto spacing = filterArgs.value<VectorFloat64Parameter::ValueType>(k_Spacing_Key);
auto pChangeDataType = filterArgs.value<bool>(k_ChangeDataType_Key);
auto choiceType = filterArgs.value<ChoicesParameter::ValueType>(k_ImageDataType_Key);
auto pChoiceType = filterArgs.value<ChoicesParameter::ValueType>(k_ImageDataType_Key);

std::string fileNameString = fileName.string();

Expand All @@ -128,7 +128,7 @@ IFilter::PreflightResult ITKImageReaderFilter::preflightImpl(const DataStructure
imageReaderOptions.Origin = FloatVec3(static_cast<float32>(origin[0]), static_cast<float32>(origin[1]), static_cast<float32>(origin[2]));
imageReaderOptions.Spacing = FloatVec3(static_cast<float32>(spacing[0]), static_cast<float32>(spacing[1]), static_cast<float32>(spacing[2]));
imageReaderOptions.ChangeDataType = pChangeDataType;
imageReaderOptions.ImageDataType = ITK::detail::ConvertChoiceToDataType(choiceType);
imageReaderOptions.ImageDataType = ITK::detail::ConvertChoiceToDataType(pChoiceType);

Result<OutputActions> result = cxItkImageReaderFilter::ReadImagePreflight(fileNameString, imageGeomPath, cellDataName, imageDataArrayName, imageReaderOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Algorithms/ITKImportFijiMontage.hpp"

#include "ITKImageProcessing/Common/ITKArrayHelper.hpp"
#include "ITKImageProcessing/Filters/ITKImageReaderFilter.hpp"

#include "simplnx/Core/Application.hpp"
Expand Down Expand Up @@ -84,6 +85,9 @@ Parameters ITKImportFijiMontageFilter::parameters() const
std::make_unique<BoolParameter>(k_ConvertToGrayScale_Key, "Convert To GrayScale", "The filter will show an error if the images are already in grayscale format", false));
params.insert(std::make_unique<VectorFloat32Parameter>(k_ColorWeights_Key, "Color Weighting", "The luminosity values for the conversion", std::vector<float32>{0.2125f, 0.7154f, 0.0721f},
std::vector<std::string>(3)));
params.insertLinkableParameter(std::make_unique<BoolParameter>(k_ChangeDataType_Key, "Set Image Data Type", "Set the final created image data type.", false));
params.insert(std::make_unique<ChoicesParameter>(k_ImageDataType_Key, "Output Data Type", "Numeric Type of data to create", 0ULL,
ChoicesParameter::Choices{"uint8", "uint16", "uint32"})); // Sequence Dependent DO NOT REORDER
params.insertLinkableParameter(std::make_unique<BoolParameter>(k_ParentDataGroup_Key, "Parent Imported Images Under a DataGroup", "Create a new DataGroup to hold the imported images", true));

params.insertSeparator(Parameters::Separator{"Output Data Object(s)"});
Expand All @@ -93,6 +97,7 @@ Parameters ITKImportFijiMontageFilter::parameters() const
params.insert(std::make_unique<StringParameter>(k_ImageDataArrayName_Key, "Image DataArray Name", "The name of the import image data", "Image"));

// Associate the Linkable Parameter(s) to the children parameters that they control
params.linkParameters(k_ChangeDataType_Key, k_ImageDataType_Key, true);
params.linkParameters(k_ChangeOrigin_Key, k_Origin_Key, true);
params.linkParameters(k_ConvertToGrayScale_Key, k_ColorWeights_Key, true);
params.linkParameters(k_ParentDataGroup_Key, k_DataGroupName_Key, true);
Expand Down Expand Up @@ -120,6 +125,8 @@ IFilter::PreflightResult ITKImportFijiMontageFilter::preflightImpl(const DataStr
auto pDataContainerPathValue = filterArgs.value<StringParameter::ValueType>(k_DataContainerPath_Key);
auto pCellAttributeMatrixNameValue = filterArgs.value<StringParameter::ValueType>(k_CellAttributeMatrixName_Key);
auto pImageDataArrayNameValue = filterArgs.value<StringParameter::ValueType>(k_ImageDataArrayName_Key);
auto pChangeDataType = filterArgs.value<bool>(k_ChangeDataType_Key);
auto pChoiceType = filterArgs.value<ChoicesParameter::ValueType>(k_ImageDataType_Key);

PreflightResult preflightResult;
nx::core::Result<OutputActions> resultOutputActions = {};
Expand All @@ -132,6 +139,8 @@ IFilter::PreflightResult ITKImportFijiMontageFilter::preflightImpl(const DataStr
inputValues.inputFilePath = pInputFileValue;
inputValues.origin = pOriginValue;
inputValues.imagePrefix = pDataContainerPathValue;
inputValues.changeDataType = pChangeDataType;
inputValues.destType = ITK::detail::ConvertChoiceToDataType(pChoiceType);

// Read from the file if the input file has changed or the input file's time stamp is out of date.
if(pInputFileValue != s_HeaderCache[m_InstanceId].inputFile || s_HeaderCache[m_InstanceId].timeStamp < fs::last_write_time(pInputFileValue) || s_HeaderCache[m_InstanceId].valuesChanged(inputValues))
Expand Down Expand Up @@ -175,6 +184,8 @@ IFilter::PreflightResult ITKImportFijiMontageFilter::preflightImpl(const DataStr
imageImportArgs.insertOrAssign(ITKImageReaderFilter::k_ImageGeometryPath_Key, std::make_any<DataPath>(imageDataProxy.getParent().getParent()));
imageImportArgs.insertOrAssign(ITKImageReaderFilter::k_CellDataName_Key, std::make_any<std::string>(imageDataProxy.getParent().getTargetName()));
imageImportArgs.insertOrAssign(ITKImageReaderFilter::k_ImageDataArrayPath_Key, std::make_any<std::string>(imageDataProxy.getTargetName()));
imageImportArgs.insertOrAssign(ITKImageReaderFilter::k_ChangeDataType_Key, std::make_any<bool>(pChangeDataType));
imageImportArgs.insertOrAssign(ITKImageReaderFilter::k_ImageDataType_Key, std::make_any<ChoicesParameter::ValueType>(pChoiceType));

auto result = imageImportFilter.preflight(dataStructure, imageImportArgs, messageHandler, shouldCancel);
if(result.outputActions.invalid())
Expand Down Expand Up @@ -230,6 +241,8 @@ Result<> ITKImportFijiMontageFilter::executeImpl(DataStructure& dataStructure, c
inputValues.imagePrefix = filterArgs.value<StringParameter::ValueType>(k_DataContainerPath_Key);
inputValues.cellAMName = filterArgs.value<StringParameter::ValueType>(k_CellAttributeMatrixName_Key);
inputValues.imageDataArrayName = filterArgs.value<StringParameter::ValueType>(k_ImageDataArrayName_Key);
inputValues.changeDataType = filterArgs.value<bool>(k_ChangeDataType_Key);
inputValues.destType = ITK::detail::ConvertChoiceToDataType(filterArgs.value<ChoicesParameter::ValueType>(k_ImageDataType_Key));

ITKImportFijiMontage(dataStructure, messageHandler, shouldCancel, &inputValues, s_HeaderCache.find(m_InstanceId)->second)();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ITKIMAGEPROCESSING_EXPORT ITKImportFijiMontageFilter : public IFilter
static inline constexpr StringLiteral k_LengthUnit_Key = "length_unit_index";
static inline constexpr StringLiteral k_ChangeOrigin_Key = "change_origin";
static inline constexpr StringLiteral k_Origin_Key = "origin";
static inline constexpr StringLiteral k_ChangeDataType_Key = "change_image_data_type";
static inline constexpr StringLiteral k_ImageDataType_Key = "image_data_type_index";
static inline constexpr StringLiteral k_ParentDataGroup_Key = "parent_data_group";
static inline constexpr StringLiteral k_ConvertToGrayScale_Key = "convert_to_gray_scale";
static inline constexpr StringLiteral k_ColorWeights_Key = "color_weights";
Expand Down

0 comments on commit 86a5ad8

Please sign in to comment.