|
| 1 | +#include <FAST/Testing.hpp> |
| 2 | +#include <FAST/Importers/ImageFileImporter.hpp> |
| 3 | +#include <FAST/Importers/WholeSlideImageImporter.hpp> |
| 4 | +#include <FAST/Data/ImagePyramid.hpp> |
| 5 | +#include <FAST/Algorithms/GaussianSmoothing/GaussianSmoothing.hpp> |
| 6 | +#include <FAST/Visualization/Plotting/LinePlotter.hpp> |
| 7 | +#include <FAST/Algorithms/ImageCropper/ImageCropper.hpp> |
| 8 | +#include "PSNR.hpp" |
| 9 | +#include "SSIM.hpp" |
| 10 | + |
| 11 | +using namespace fast; |
| 12 | + |
| 13 | +TEST_CASE("MSE on 2D images", "[fast][MSE][ImageComparison]") { |
| 14 | + auto importer1 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_0.mhd"); |
| 15 | + auto image1 = importer1->run()->getOutput<Image>(); |
| 16 | + auto importer2 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_20.mhd"); |
| 17 | + |
| 18 | + auto mse = MSE::create() |
| 19 | + ->connect(0, importer1) |
| 20 | + ->connect(1, importer2); |
| 21 | + CHECK_NOTHROW( |
| 22 | + mse->run(); |
| 23 | + ); |
| 24 | + |
| 25 | + auto output = mse->getOutput<Image>(); |
| 26 | + CHECK(mse->get() == Approx(676.59)); |
| 27 | + CHECK(output->getWidth() == image1->getWidth()); |
| 28 | + CHECK(output->getHeight() == image1->getHeight()); |
| 29 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 30 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 31 | +} |
| 32 | + |
| 33 | +TEST_CASE("MSE on 2D color images", "[fast][MSE][ImageComparison]") { |
| 34 | + auto importer1 = WholeSlideImageImporter::create(Config::getTestDataPath() + "WSI/CMU-1.svs"); |
| 35 | + auto wsi = importer1->run()->getOutput<ImagePyramid>(); |
| 36 | + auto access = wsi->getAccess(ACCESS_READ); |
| 37 | + auto image1 = access->getLevelAsImage(wsi->getNrOfLevels()-1); |
| 38 | + auto image2 = GaussianSmoothing::create()->connect(image1); |
| 39 | + |
| 40 | + auto mse = MSE::create()->connect(image1)->connect(1, image2); |
| 41 | + CHECK_NOTHROW( |
| 42 | + mse->run(); |
| 43 | + ); |
| 44 | + |
| 45 | + auto output = mse->getOutput<Image>(); |
| 46 | + CHECK(output->getWidth() == image1->getWidth()); |
| 47 | + CHECK(output->getHeight() == image1->getHeight()); |
| 48 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 49 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_CASE("MSE on 3D images", "[fast][MSE][ImageComparison]") { |
| 53 | + auto importer1 = ImageFileImporter::create(Config::getTestDataPath() + "US/Ball/US-3Dt_0.mhd"); |
| 54 | + auto image1 = importer1->run()->getOutput<Image>(); |
| 55 | + auto importer2 = ImageFileImporter::create(Config::getTestDataPath() + "US/Ball/US-3Dt_20.mhd"); |
| 56 | + |
| 57 | + auto mse = MSE::create() |
| 58 | + ->connect(0, importer1) |
| 59 | + ->connect(1, importer2); |
| 60 | + CHECK_NOTHROW( |
| 61 | + mse->run(); |
| 62 | + ); |
| 63 | + |
| 64 | + auto output = mse->getOutput<Image>(); |
| 65 | + CHECK(mse->get() == Approx(214.87)); |
| 66 | + CHECK(output->getWidth() == image1->getWidth()); |
| 67 | + CHECK(output->getHeight() == image1->getHeight()); |
| 68 | + CHECK(output->getDepth() == image1->getDepth()); |
| 69 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 70 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +TEST_CASE("PSNR on 2D image", "[fast][PSNR][ImageComparison]") { |
| 75 | + auto importer1 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_0.mhd"); |
| 76 | + auto image1 = importer1->run()->getOutput<Image>(); |
| 77 | + auto importer2 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_20.mhd"); |
| 78 | + |
| 79 | + auto psnr = PSNR::create(255) |
| 80 | + ->connect(0, importer1) |
| 81 | + ->connect(1, importer2); |
| 82 | + CHECK_NOTHROW( |
| 83 | + psnr->run(); |
| 84 | + ); |
| 85 | + |
| 86 | + auto output = psnr->getOutput<Image>(); |
| 87 | + CHECK(psnr->get() == Approx(19.8275)); |
| 88 | + CHECK(output->getWidth() == image1->getWidth()); |
| 89 | + CHECK(output->getHeight() == image1->getHeight()); |
| 90 | + CHECK(output->getDepth() == image1->getDepth()); |
| 91 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 92 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 93 | +} |
| 94 | + |
| 95 | +TEST_CASE("SSIM on 2D image", "[fast][SSIM][ImageComparison]") { |
| 96 | + auto importer1 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_0.mhd"); |
| 97 | + auto image1 = importer1->run()->getOutput<Image>(); |
| 98 | + auto importer2 = ImageFileImporter::create(Config::getTestDataPath() + "US/Heart/ApicalFourChamber/US-2D_20.mhd"); |
| 99 | + |
| 100 | + // Crop to be able to compare value with other implementations |
| 101 | + auto cropper1 = ImageCropper::create(Vector2i(image1->getWidth()-10, image1->getHeight()-10), Vector2i(5, 5))->connect(image1); |
| 102 | + auto cropper2 = ImageCropper::create(Vector2i(image1->getWidth()-10, image1->getHeight()-10), Vector2i(5, 5))->connect(importer2); |
| 103 | + image1 = cropper1->run()->getOutput<Image>(); |
| 104 | + |
| 105 | + auto ssim = SSIM::create(255, 0, Vector3i::Constant(11), Vector3f::Constant(1.5f)) |
| 106 | + ->connect(0, image1) |
| 107 | + ->connect(1, cropper2); |
| 108 | + ssim->enableRuntimeMeasurements(); |
| 109 | + ssim->run(); |
| 110 | + |
| 111 | + auto output = ssim->getOutput<Image>(); |
| 112 | + auto value = ssim->getOutput<FloatScalar>(1); |
| 113 | + std::cout << ssim->get() << std::endl; |
| 114 | + ssim->getAllRuntimes()->printAll(); |
| 115 | + |
| 116 | + CHECK(value->get() == ssim->get()); |
| 117 | + CHECK(ssim->get() == Approx(0.59).epsilon(0.01)); |
| 118 | + CHECK(output->getWidth() == image1->getWidth()); |
| 119 | + CHECK(output->getHeight() == image1->getHeight()); |
| 120 | + CHECK(output->getDepth() == image1->getDepth()); |
| 121 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 122 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 123 | +} |
| 124 | + |
| 125 | +TEST_CASE("SSIM on 2D color image", "[fast][SSIM][ImageComparison]") { |
| 126 | + auto importer1 = WholeSlideImageImporter::create(Config::getTestDataPath() + "WSI/CMU-1.svs"); |
| 127 | + auto wsi = importer1->run()->getOutput<ImagePyramid>(); |
| 128 | + auto access = wsi->getAccess(ACCESS_READ); |
| 129 | + auto image1 = access->getLevelAsImage(wsi->getNrOfLevels()-1); |
| 130 | + auto image2 = GaussianSmoothing::create()->connect(image1); |
| 131 | + |
| 132 | + auto ssim = SSIM::create(255)->connect(image1)->connect(1, image2); |
| 133 | + ssim->enableRuntimeMeasurements(); |
| 134 | + |
| 135 | + CHECK_NOTHROW( |
| 136 | + ssim->run(); |
| 137 | + ); |
| 138 | + |
| 139 | + auto output = ssim->getOutput<Image>(0); |
| 140 | + auto value = ssim->getOutput<FloatScalar>(1); |
| 141 | + ssim->getAllRuntimes()->printAll(); |
| 142 | + |
| 143 | + CHECK(value->get() == ssim->get()); |
| 144 | + CHECK(output->getWidth() == image1->getWidth()); |
| 145 | + CHECK(output->getHeight() == image1->getHeight()); |
| 146 | + CHECK(output->getDepth() == image1->getDepth()); |
| 147 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 148 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 149 | +} |
| 150 | + |
| 151 | +TEST_CASE("SSIM on 3D images", "[fast][SSIM][ImageComparison]") { |
| 152 | + auto importer1 = ImageFileImporter::create(Config::getTestDataPath() + "US/Ball/US-3Dt_0.mhd"); |
| 153 | + auto image1 = importer1->run()->getOutput<Image>(); |
| 154 | + auto importer2 = ImageFileImporter::create(Config::getTestDataPath() + "US/Ball/US-3Dt_20.mhd"); |
| 155 | + |
| 156 | + // Crop to be able to compare value with other implementations |
| 157 | + auto cropper1 = ImageCropper::create(Vector3i(image1->getWidth()-10, image1->getHeight()-10, image1->getDepth()-10), Vector3i(5, 5, 5))->connect(image1); |
| 158 | + auto cropper2 = ImageCropper::create(Vector3i(image1->getWidth()-10, image1->getHeight()-10, image1->getDepth()-10), Vector3i(5, 5, 5))->connect(importer2); |
| 159 | + image1 = cropper1->run()->getOutput<Image>(); |
| 160 | + |
| 161 | + auto ssim = SSIM::create(255) |
| 162 | + ->connect(0, image1) |
| 163 | + ->connect(1, cropper2); |
| 164 | + CHECK_NOTHROW( |
| 165 | + ssim->run(); |
| 166 | + ); |
| 167 | + std::cout << ssim->get() << std::endl; |
| 168 | + |
| 169 | + auto output = ssim->getOutput<Image>(0); |
| 170 | + auto value = ssim->getOutput<FloatScalar>(1); |
| 171 | + |
| 172 | + CHECK(value->get() == ssim->get()); |
| 173 | + CHECK(ssim->get() == Approx(0.73).epsilon(0.01)); |
| 174 | + CHECK(output->getWidth() == image1->getWidth()); |
| 175 | + CHECK(output->getHeight() == image1->getHeight()); |
| 176 | + CHECK(output->getDepth() == image1->getDepth()); |
| 177 | + CHECK(output->getDataType() == TYPE_FLOAT); |
| 178 | + CHECK(output->getNrOfChannels() == image1->getNrOfChannels()); |
| 179 | +} |
0 commit comments