# HG changeset patch # User Benjamin Golinvaux # Date 1581688772 -3600 # Node ID ddb6676bbcbfe07ae625f8a83e49961b2ed8c4e5 # Parent 1bb54005e2bd297d7bda2a575c2f7f6316ca5771 Added Histogram functions (new files: ImageToolbox...) diff -r 1bb54005e2bd -r ddb6676bbcbf Applications/Samples/CMakeLists.txt --- a/Applications/Samples/CMakeLists.txt Tue Feb 11 14:38:36 2020 +0100 +++ b/Applications/Samples/CMakeLists.txt Fri Feb 14 14:59:32 2020 +0100 @@ -251,6 +251,7 @@ add_executable(UnitTests ${GOOGLE_TEST_SOURCES} ${ORTHANC_STONE_ROOT}/UnitTestsSources/GenericToolboxTests.cpp + ${ORTHANC_STONE_ROOT}/UnitTestsSources/ImageToolboxTests.cpp ${ORTHANC_STONE_ROOT}/UnitTestsSources/PixelTestPatternsTests.cpp ${ORTHANC_STONE_ROOT}/UnitTestsSources/TestCommands.cpp ${ORTHANC_STONE_ROOT}/UnitTestsSources/TestMessageBroker.cpp diff -r 1bb54005e2bd -r ddb6676bbcbf Framework/Toolbox/ImageToolbox.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Toolbox/ImageToolbox.cpp Fri Feb 14 14:59:32 2020 +0100 @@ -0,0 +1,304 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2020 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + +#include "ImageToolbox.h" + +#include "../StoneException.h" + +#include +#include + +#include +#include + +#include +#include + +#include + +namespace OrthancStone +{ + namespace + { + using Orthanc::PixelTraits; + using Orthanc::PixelFormat; + using Orthanc::ImageAccessor; + using Orthanc::PixelFormat; + + template + class PixelBinner + { + // "PixelBinner requires an arithmetic (integer or floating-point) pixel format" + typedef typename PixelTraits::PixelType PixelType; + BOOST_STATIC_ASSERT(boost::is_arithmetic::value); + + public: + PixelBinner(HistogramData& hd, double minValue, double maxValue) + : hd_(hd) + , minValue_(minValue) + , maxValue_(maxValue) + , division_(1.0 / hd_.binSize) + { + ORTHANC_ASSERT(hd_.bins.size() > 0); + ORTHANC_ASSERT(maxValue > minValue); + } + + ORTHANC_FORCE_INLINE void AddPixel(PixelType p) + { + if (p <= minValue_) + { + hd_.bins[0] += 1; + } + else if (p >= maxValue_) + { + hd_.bins.back() += 1; + } + else + { + double distanceFromMin = p - minValue_; + size_t binIndex = static_cast( + std::floor(distanceFromMin * division_)); + if (binIndex >= hd_.bins.size()) + binIndex = hd_.bins.size() - 1; + hd_.bins[binIndex] += 1; + } + } + private: + HistogramData& hd_; + double minValue_; + double maxValue_; + double division_; + }; + + template + struct Histogram + { + typedef typename PixelTraits::PixelType PixelType; + + static void Apply(const Orthanc::ImageAccessor& img, HistogramData& hd, + double minValue = 0, + double maxValue = 0) + { + ORTHANC_ASSERT(Format == img.GetFormat(), + "Internal error. Wrong template histogram type"); + + const uint8_t* buffer = reinterpret_cast( + img.GetConstBuffer()); + + const size_t pitch = img.GetPitch(); + const size_t bytesPerPix = img.GetBytesPerPixel(); + + const size_t height = img.GetHeight(); + const size_t width = img.GetHeight(); + + if ((minValue == 0) && (maxValue == 0)) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + //ORTHANC_ASSERT(boost::is_integral::value, + // "Min and max values must be supplied for float-based histogram"); + // + //PixelTraits::SetMinValue(minValue); + //PixelTraits::SetMaxValue(maxValue); + } + + hd.minValue = minValue; + + // the following code is not really pretty but ensures + size_t numBins = static_cast( + std::ceil((maxValue - minValue) / hd.binSize)); + + hd.bins.resize(numBins); + std::fill(hd.bins.begin(), hd.bins.end(), 0); + + PixelBinner binner(hd, minValue, maxValue); + for (uint32_t y = 0; y < height; ++y) + { + const PixelType* curPix = reinterpret_cast( + img.GetConstRow(y)); + + for (uint32_t x = 0; x < width; x++, curPix++) + { + binner.AddPixel(*curPix); + } + } + } + }; + + + template + struct ComputeMinMax__ + { + typedef typename PixelTraits::PixelType PixelType; + + static void Apply(const Orthanc::ImageAccessor& img, + PixelType& minValue, PixelType& maxValue) + { + ORTHANC_ASSERT(Format == img.GetFormat(), + "Internal error. Wrong template histogram type"); + + const uint8_t* buffer = reinterpret_cast( + img.GetConstBuffer()); + + const size_t pitch = img.GetPitch(); + const size_t bytesPerPix = img.GetBytesPerPixel(); + + const size_t height = img.GetHeight(); + const size_t width = img.GetHeight(); + + if (height * width == 0) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); + } + + // min and max are crossed below. Think about it. This is OK :) + PixelTraits::SetMaxValue(minValue); + PixelTraits::SetMinValue(maxValue); + + for (uint32_t y = 0; y < height; ++y) + { + const PixelType* curPix = reinterpret_cast( + img.GetConstRow(y)); + + for (uint32_t x = 0; x < width; x++, curPix++) + { + if (*curPix <= minValue) + minValue = *curPix; + if (*curPix >= maxValue) + maxValue = *curPix; + } + } + } + }; + + template + void ComputeMinMax_(const Orthanc::ImageAccessor& img, + double& minValue, double& maxValue) + { + typedef typename PixelTraits::PixelType PixelType; + PixelType minValuePix = PixelType(); + PixelType maxValuePix = PixelType(); + ComputeMinMax__::Apply(img, minValuePix, maxValuePix); + minValue = static_cast(minValuePix); + maxValue = static_cast(maxValuePix); + } + + template + void ComputeHistogram_(const Orthanc::ImageAccessor& img, HistogramData& hd) + { + typedef typename PixelTraits::PixelType PixelType; + PixelType minValue = PixelType(); + PixelType maxValue = PixelType(); + ComputeMinMax__::Apply(img, minValue, maxValue); + + // make bins a little bigger to center integer pixel values + Histogram::Apply(img, hd, + static_cast(minValue) - 0.5, + static_cast(maxValue) + 0.5); + } + } + + void ComputeHistogram(const Orthanc::ImageAccessor& img, + HistogramData& hd, double binSize) + { + using namespace Orthanc; + + hd.binSize = binSize; + + // dynamic/static bridge + switch (img.GetFormat()) + { + case PixelFormat_Grayscale8: + ComputeHistogram_ (img, hd); + break; + case PixelFormat_Grayscale16: + ComputeHistogram_ (img, hd); + break; + case PixelFormat_SignedGrayscale16: + ComputeHistogram_(img, hd); + break; + case PixelFormat_Float32: + ComputeHistogram_ (img, hd); + break; + case PixelFormat_Grayscale32: + ComputeHistogram_ (img, hd); + break; + case PixelFormat_Grayscale64: + ComputeHistogram_ (img, hd); + break; + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); + } + } + + void ComputeMinMax(const Orthanc::ImageAccessor& img, + double& minValue, double& maxValue) + { + using namespace Orthanc; + + // dynamic/static bridge + switch (img.GetFormat()) + { + case PixelFormat_Grayscale8: + ComputeMinMax_ (img, minValue, maxValue); + break; + case PixelFormat_Grayscale16: + ComputeMinMax_ (img, minValue, maxValue); + break; + case PixelFormat_SignedGrayscale16: + ComputeMinMax_(img, minValue, maxValue); + break; + case PixelFormat_Float32: + ComputeMinMax_ (img, minValue, maxValue); + break; + case PixelFormat_Grayscale32: + ComputeMinMax_ (img, minValue, maxValue); + break; + case PixelFormat_Grayscale64: + ComputeMinMax_ (img, minValue, maxValue); + break; + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); + } + + } + + void DumpHistogramResult(std::string& s, const HistogramData& hd) + { + std::stringstream ss; + ss << "Histogram:\n"; + ss << "==========\n"; + ss << "\n"; + ss << "minValue : " << hd.minValue << "\n"; + ss << "binSize : " << hd.binSize << "\n"; + ss << "bins.size() : " << hd.bins.size() << "\n"; + ss << "bins :\n"; + double curBinStart = hd.minValue; + size_t pixCount = 0; + for (size_t i = 0; i < hd.bins.size(); ++i) + { + ss << "index: " << i << " (from " << curBinStart << " to " + << curBinStart + hd.binSize << ") : " << hd.bins[i] << " pixels\n"; + curBinStart += hd.binSize; + pixCount += hd.bins[i]; + } + ss << "total pix. count: " << pixCount << "\n"; + s = ss.str(); + } +} diff -r 1bb54005e2bd -r ddb6676bbcbf Framework/Toolbox/ImageToolbox.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Toolbox/ImageToolbox.h Fri Feb 14 14:59:32 2020 +0100 @@ -0,0 +1,78 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2020 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + + +#pragma once + +#include "../StoneEnumerations.h" +#include "LinearAlgebra.h" + +#include + +namespace OrthancStone +{ + + /** + This structure represents the result of an histogram computation + + bins[0] contains the values in [minValue , minValue + binSize [ + bins[1] contains the values in [minValue + binSize, minValue + 2*binSize [ + bins[2] contains the values in [minValue + 2*binSize, minValue + 3*binSize [ + ... + bins[N-1] contains the values in [minValue + (N-1)*binSize, minValue + N*binSize [ + + */ + struct HistogramData + { + std::vector bins; + double minValue; + double binSize; + }; + + /** + Dumps the supplied histogram to the supplied strings + */ + void DumpHistogramResult(std::string& s, const HistogramData& hd); + + /** + This will compute the histogram of the supplied image (count the number of + pixels). + + The image must contain arithmetic pixels (that is, having a single component, + integer or float). Compound pixel types like RGB, YUV are not supported and + will cause this function to throw an exception. + + The range of available values will be split in sets of size `binSize`, and + each set will contain the number of pixels in the given bin + (see HistogramResult above). + */ + void ComputeHistogram(const Orthanc::ImageAccessor& img, + HistogramData& hd, double binSize); + + + /** + Computes the min max values in an image + */ + void ComputeMinMax(const Orthanc::ImageAccessor& img, + double& minValue, double& maxValue); + +} + +extern int OrthancStone_Internals_dump_LoadTexture_histogram; diff -r 1bb54005e2bd -r ddb6676bbcbf Resources/CMake/OrthancStoneConfiguration.cmake --- a/Resources/CMake/OrthancStoneConfiguration.cmake Tue Feb 11 14:38:36 2020 +0100 +++ b/Resources/CMake/OrthancStoneConfiguration.cmake Fri Feb 14 14:59:32 2020 +0100 @@ -570,6 +570,8 @@ ${ORTHANC_STONE_ROOT}/Framework/Toolbox/GeometryToolbox.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/ImageGeometry.cpp ${ORTHANC_STONE_ROOT}/Framework/Toolbox/ImageGeometry.h + ${ORTHANC_STONE_ROOT}/Framework/Toolbox/ImageToolbox.cpp + ${ORTHANC_STONE_ROOT}/Framework/Toolbox/ImageToolbox.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/LinearAlgebra.cpp ${ORTHANC_STONE_ROOT}/Framework/Toolbox/LinearAlgebra.h ${ORTHANC_STONE_ROOT}/Framework/Toolbox/PixelTestPatterns.cpp diff -r 1bb54005e2bd -r ddb6676bbcbf UnitTestsSources/ImageToolboxTests.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UnitTestsSources/ImageToolboxTests.cpp Fri Feb 14 14:59:32 2020 +0100 @@ -0,0 +1,259 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2020 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + +#include + +// #include +// #include + +#include +#include + +#include "stdint.h" + +#include "gtest/gtest.h" + +#include + + +TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1) +{ + using OrthancStone::HistogramData; + using OrthancStone::DumpHistogramResult; + using OrthancStone::ComputeHistogram; + + const unsigned int W = 16; + const unsigned int H = 16; + + // 256/17 = 15,... + // 256 % 17 = 1 + // 0 will be 16 times + // 1 will be 15 times + // 2 will be 15 times + // ... + // 16 will be 15 times + + size_t pixCounter = 0; + + std::auto_ptr image(new Orthanc::Image( + Orthanc::PixelFormat_Grayscale8, W, H, false)); + + for (unsigned int y = 0; y < H; ++y) + { + uint8_t* buffer = reinterpret_cast(image->GetRow(y)); + for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) + { + *buffer = static_cast(pixCounter % 17); + } + } + + HistogramData hd; + ComputeHistogram(*image, hd, 1); + ASSERT_EQ(-0.5, hd.minValue); + ASSERT_EQ(17u, hd.bins.size()); + ASSERT_EQ(16u, hd.bins[0]); + for (size_t i = 1; i < hd.bins.size(); ++i) + ASSERT_EQ(15u, hd.bins[i]); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_FormatString) +{ + using OrthancStone::HistogramData; + using OrthancStone::DumpHistogramResult; + using OrthancStone::ComputeHistogram; + + const unsigned int W = 16; + const unsigned int H = 16; + + // 256/17 = 15,... + // 256 % 17 = 1 + // 0 will be 16 times + // 1 will be 15 times + // 2 will be 15 times + // ... + // 16 will be 15 times + + size_t pixCounter = 0; + + std::auto_ptr image(new Orthanc::Image( + Orthanc::PixelFormat_Grayscale8, W, H, false)); + + for (unsigned int y = 0; y < H; ++y) + { + uint8_t* buffer = reinterpret_cast(image->GetRow(y)); + for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) + { + *buffer = static_cast(pixCounter % 17); + } + } + + HistogramData hd; + ComputeHistogram(*image, hd, 1); + + // void DumpHistogramResult(std::string& s, const HistogramData& hd) + std::string s; + DumpHistogramResult(s, hd); + std::cout << s; +} + +template +void SimpleHisto_T_BinSize1_2() +{ + using OrthancStone::HistogramData; + using OrthancStone::DumpHistogramResult; + using OrthancStone::ComputeHistogram; + + const unsigned int W = 16; + const unsigned int H = 16; + + // 256/17 = 15,... + // 256 % 17 = 1 + // 0 will be 16 times + // 1 will be 15 times + // 2 will be 15 times + // ... + // 16 will be 15 times + + size_t pixCounter = 0; + + std::auto_ptr image(new Orthanc::Image( + Format, W, H, false)); + + typedef Orthanc::PixelTraits::PixelType PixelType; + + PixelType pixValue = 0; + + for (unsigned int y = 0; y < H; ++y) + { + PixelType* buffer = reinterpret_cast(image->GetRow(y)); + for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) + { + // 0..99 0..99 0..55 + *buffer = pixValue; + pixValue++; + if (pixValue >= 100) + pixValue = 0; + } + } + + HistogramData hd; + ComputeHistogram(*image, hd, 1); + ASSERT_EQ(-0.5, hd.minValue); + ASSERT_EQ(100u, hd.bins.size()); + for (size_t i = 0; i <= 55; ++i) + ASSERT_EQ(3u, hd.bins[i]); + for (size_t i = 56; i <= 99; ++i) + ASSERT_EQ(2u, hd.bins[i]); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_2) +{ + SimpleHisto_T_BinSize1_2(); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize1_2) +{ + SimpleHisto_T_BinSize1_2(); +} + +TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize1_2) +{ + SimpleHisto_T_BinSize1_2(); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize1_2) +{ + SimpleHisto_T_BinSize1_2(); +} + +template +void SimpleHisto_T_BinSize10_2() +{ + using OrthancStone::HistogramData; + using OrthancStone::DumpHistogramResult; + using OrthancStone::ComputeHistogram; + + const unsigned int W = 16; + const unsigned int H = 16; + + // 256/17 = 15,... + // 256 % 17 = 1 + // 0 will be 16 times + // 1 will be 15 times + // 2 will be 15 times + // ... + // 16 will be 15 times + + size_t pixCounter = 0; + + std::auto_ptr image(new Orthanc::Image( + Format, W, H, false)); + + typedef Orthanc::PixelTraits::PixelType PixelType; + + PixelType pixValue = 0; + + for (unsigned int y = 0; y < H; ++y) + { + PixelType* buffer = reinterpret_cast(image->GetRow(y)); + for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) + { + // 0..99 0..99 0..55 + *buffer = pixValue; + pixValue++; + if (pixValue >= 100) + pixValue = 0; + } + } + + HistogramData hd; + ComputeHistogram(*image, hd, 10); + ASSERT_EQ(-0.5, hd.minValue); + ASSERT_EQ(10u, hd.bins.size()); + + for (size_t i = 0; i <= 4; ++i) + ASSERT_EQ(30u, hd.bins[i]); + + ASSERT_EQ(26u, hd.bins[5]); + + for (size_t i = 6; i <= 9; ++i) + ASSERT_EQ(20u, hd.bins[i]); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize10_2) +{ + SimpleHisto_T_BinSize10_2(); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize10_2) +{ + SimpleHisto_T_BinSize10_2(); +} + +TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize10_2) +{ + SimpleHisto_T_BinSize10_2(); +} + +TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize10_2) +{ + SimpleHisto_T_BinSize10_2(); +} +