Skip to content

Commit

Permalink
Merge pull request #382 from clEsperanto/mean-intensity-map
Browse files Browse the repository at this point in the history
add mean intensity map
  • Loading branch information
StRigaud authored Oct 18, 2024
2 parents a227679 + 1ae9731 commit d3b3493
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions clic/include/tier4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ auto
threshold_otsu_func(const Device::Pointer & device, const Array::Pointer & src, Array::Pointer dst) -> Array::Pointer;


/**
* @name mean_intensity_map
* @brief Takes an image and a corresponding label map, determines the mean
* intensity per label and replaces every label with the that number.
*
* This results in a parametric image expressing mean object intensity.
*
* @param device Device to perform the operation on. [const Device::Pointer &]
* @param src intensity image [const Array::Pointer &]
* @param labels label image [const Array::Pointer &]
* @param dst Parametric image computed[Array::Pointer ( = None )]
* @return Array::Pointer
*
* @note 'label measurement', 'map', 'in assistant', 'combine'
* @see https://clij.github.io/clij2-docs/reference_meanIntensityMap
*/
auto
mean_intensity_map_func(const Device::Pointer & device,
const Array::Pointer & src,
const Array::Pointer & labels,
Array::Pointer dst) -> Array::Pointer;


/**
* @name pixel_count_map
* @brief Takes a label map, determines the number of pixels per label and replaces every label with the that number.
Expand Down
16 changes: 16 additions & 0 deletions clic/src/tier4/parametrics_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,21 @@ extension_ratio_map_func(const Device::Pointer & device, const Array::Pointer &
return tier1::replace_values_func(device, src, values, dst);
}

auto
mean_intensity_map_func(const Device::Pointer & device,
const Array::Pointer & src,
const Array::Pointer & labels,
Array::Pointer dst) -> Array::Pointer
{
tier0::create_like(src, dst, dType::FLOAT);
auto props = tier3::statistics_of_background_and_labelled_pixels_func(device, src, labels);

auto values = cle::Array::create(props["mean_intensity"].size(), 1, 1, 1, dType::FLOAT, mType::BUFFER, device);
values->writeFrom(props["mean_intensity"].data());

tier1::set_column_func(device, values, 0, 0);
return tier1::replace_values_func(device, labels, values, dst);
}


} // namespace cle::tier4
53 changes: 53 additions & 0 deletions tests/tier4/test_mean_intensity_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "cle.hpp"

#include <array>
#include <gtest/gtest.h>

class TestMeanIntensityMap : public ::testing::TestWithParam<std::string>
{
protected:
std::array<float, 3 * 3 * 1> intensity = { 1, 1, 2, 4, 0, 0, 5, 3, 0 };

std::array<uint32_t, 3 * 3 * 1> label = { 1, 1, 2, 1, 0, 0, 3, 3, 0 };

std::array<float, 3 * 3 * 1> valid = { 2, 2, 2, 2, 0, 0, 4, 4, 0 };
};

TEST_P(TestMeanIntensityMap, execute)
{

std::string param = GetParam();
cle::BackendManager::getInstance().setBackend(param);
auto device = cle::BackendManager::getInstance().getBackend().getDevice("", "all");
device->setWaitToFinish(true);

auto gpu_intensity = cle::Array::create(3, 3, 1, 2, cle::dType::FLOAT, cle::mType::BUFFER, device);
gpu_intensity->writeFrom(intensity.data());
auto gpu_label = cle::Array::create(3, 3, 1, 2, cle::dType::LABEL, cle::mType::BUFFER, device);
gpu_label->writeFrom(label.data());

auto gpu_output = cle::tier4::mean_intensity_map_func(device, gpu_intensity, gpu_label, nullptr);

std::vector<float> output(gpu_output->size());
gpu_output->readTo(output.data());
for (int i = 0; i < output.size(); i++)
{
EXPECT_EQ(output[i], valid[i]);
}
}


std::vector<std::string>
getParameters()
{
std::vector<std::string> parameters;
#if USE_OPENCL
parameters.push_back("opencl");
#endif
#if USE_CUDA
parameters.push_back("cuda");
#endif
return parameters;
}

INSTANTIATE_TEST_SUITE_P(InstantiationName, TestMeanIntensityMap, ::testing::ValuesIn(getParameters()));

0 comments on commit d3b3493

Please sign in to comment.