1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include "open3d/t/io/ImageIO.h"
#include <gtest/gtest.h>
#include "open3d/core/Device.h"
#include "open3d/core/Dtype.h"
#include "open3d/core/SizeVector.h"
#include "open3d/core/Tensor.h"
#include "open3d/t/geometry/Image.h"
#include "open3d/utility/FileSystem.h"
#include "tests/Tests.h"
namespace open3d {
namespace tests {
namespace {
t::geometry::Image CreateTestImage() {
core::Tensor t(core::SizeVector{150, 100, 3}, core::UInt8);
t.Slice(2, 0, 1).Fill(250);
t.Slice(2, 1, 2).Fill(150);
t.Slice(2, 2, 3).Fill(200);
return t::geometry::Image(t);
}
void WriteTestImage(const std::string path, const t::geometry::Image image) {
t::io::WriteImage(path + "/test_imageio.png", image);
t::io::WriteImage(path + "/test_imageio.jpg", image);
}
} // namespace
// Write test image.
TEST(ImageIO, WriteImage) {
t::geometry::Image test_img = CreateTestImage();
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
EXPECT_TRUE(t::io::WriteImage(tmp_path + "/test_imageio.png", test_img));
EXPECT_TRUE(t::io::WriteImage(tmp_path + "/test_imageio.jpg", test_img));
}
TEST(ImageIO, CreateImageFromFile) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
std::shared_ptr<t::geometry::Image> img_png =
t::io::CreateImageFromFile(tmp_path + "/test_imageio.png");
std::shared_ptr<t::geometry::Image> img_jpg =
t::io::CreateImageFromFile(tmp_path + "/test_imageio.jpg");
EXPECT_EQ(img_png->GetRows(), 150);
EXPECT_EQ(img_png->GetCols(), 100);
EXPECT_EQ(img_png->GetChannels(), 3);
EXPECT_EQ(img_png->GetDtype(), core::UInt8);
EXPECT_EQ(img_png->GetDevice(), img_jpg->GetDevice());
t::geometry::Image test_img = CreateTestImage();
EXPECT_EQ(img_jpg->GetRows(), 150);
EXPECT_EQ(img_jpg->GetCols(), 100);
EXPECT_EQ(img_jpg->GetChannels(), 3);
EXPECT_EQ(img_jpg->GetDtype(), core::UInt8);
EXPECT_EQ(img_jpg->GetDevice(), test_img.GetDevice());
EXPECT_FALSE(img_jpg->AsTensor().IsSame(test_img.AsTensor()));
EXPECT_TRUE(img_jpg->AsTensor().AllClose(test_img.AsTensor()));
EXPECT_TRUE(img_png->AsTensor().AllClose(test_img.AsTensor()));
}
TEST(ImageIO, ReadImage) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
t::geometry::Image img;
EXPECT_TRUE(t::io::ReadImage(tmp_path + "/test_imageio.png", img));
t::geometry::Image test_img = CreateTestImage();
EXPECT_EQ(img.GetRows(), 150);
EXPECT_EQ(img.GetCols(), 100);
EXPECT_EQ(img.GetChannels(), 3);
EXPECT_EQ(img.GetDtype(), core::UInt8);
EXPECT_EQ(img.GetDevice(), test_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(test_img.AsTensor()));
EXPECT_TRUE(t::io::ReadImage(tmp_path + "/test_imageio.jpg", img));
EXPECT_EQ(img.GetRows(), 150);
EXPECT_EQ(img.GetCols(), 100);
EXPECT_EQ(img.GetChannels(), 3);
EXPECT_EQ(img.GetDtype(), core::UInt8);
EXPECT_EQ(img.GetDevice(), test_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(test_img.AsTensor()));
}
TEST(ImageIO, ReadImageFromPNG) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
t::geometry::Image img;
EXPECT_TRUE(t::io::ReadImageFromPNG(tmp_path + "/test_imageio.png", img));
t::geometry::Image test_img = CreateTestImage();
EXPECT_EQ(img.GetRows(), 150);
EXPECT_EQ(img.GetCols(), 100);
EXPECT_EQ(img.GetChannels(), 3);
EXPECT_EQ(img.GetDtype(), core::UInt8);
EXPECT_EQ(img.GetDevice(), test_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(test_img.AsTensor()));
}
TEST(ImageIO, WriteImageToPNG) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
t::geometry::Image img = CreateTestImage();
EXPECT_TRUE(t::io::WriteImageToPNG(tmp_path + "/test_imageio.png", img));
t::geometry::Image read_img =
*(t::io::CreateImageFromFile(tmp_path + "/test_imageio.png"));
EXPECT_EQ(img.GetRows(), read_img.GetRows());
EXPECT_EQ(img.GetCols(), read_img.GetCols());
EXPECT_EQ(img.GetChannels(), read_img.GetChannels());
EXPECT_EQ(img.GetDtype(), read_img.GetDtype());
EXPECT_EQ(img.GetDevice(), read_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(read_img.AsTensor()));
}
TEST(ImageIO, ReadImageFromJPG) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
t::geometry::Image img;
EXPECT_TRUE(t::io::ReadImageFromJPG(tmp_path + "/test_imageio.jpg", img));
t::geometry::Image test_img = CreateTestImage();
EXPECT_EQ(img.GetRows(), 150);
EXPECT_EQ(img.GetCols(), 100);
EXPECT_EQ(img.GetChannels(), 3);
EXPECT_EQ(img.GetDtype(), core::UInt8);
EXPECT_EQ(img.GetDevice(), test_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(test_img.AsTensor()));
}
TEST(ImageIO, WriteImageToJPG) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
WriteTestImage(tmp_path, CreateTestImage());
t::geometry::Image img = CreateTestImage();
EXPECT_TRUE(t::io::WriteImageToJPG(tmp_path + "/test_imageio.jpg", img));
t::geometry::Image read_img =
*(t::io::CreateImageFromFile(tmp_path + "/test_imageio.png"));
EXPECT_EQ(img.GetRows(), read_img.GetRows());
EXPECT_EQ(img.GetCols(), read_img.GetCols());
EXPECT_EQ(img.GetChannels(), read_img.GetChannels());
EXPECT_EQ(img.GetDtype(), read_img.GetDtype());
EXPECT_EQ(img.GetDevice(), read_img.GetDevice());
EXPECT_TRUE(img.AsTensor().AllClose(read_img.AsTensor()));
}
// JPG supports only UInt8, and PNG supports Bool, UInt8 and UInt16.
// All other data types are expected to fail.
TEST(ImageIO, DifferentDtype) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::UInt8)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::UInt16)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Float32)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Float64)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Int32)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Int64)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Bool)));
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::UInt8)));
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::UInt16)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Float32)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Float64)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Int32)));
EXPECT_FALSE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Int64)));
EXPECT_TRUE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Bool)));
}
TEST(ImageIO, CornerCases) {
const std::string tmp_path = utility::filesystem::GetTempDirectoryPath();
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 0, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(0, 200, 3, core::UInt8)));
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 1, core::UInt8)));
// Wrong extension
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.pg",
t::geometry::Image(100, 0, 3, core::UInt8)));
}
} // namespace tests
} // namespace open3d
|