Added tests for compression

This commit is contained in:
matlabbe
2026-05-16 17:36:56 -07:00
parent 69e943f80c
commit a0d891bff0
3 changed files with 242 additions and 13 deletions
+52 -13
View File
@@ -37,29 +37,51 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace rtabmap {
/**
* Compress image or data
* @class CompressionThread
* @brief Background thread to compress or uncompress images and generic matrices.
*
* In compress mode, pass a source matrix to the constructor with an optional image
* format (".png", ".jpg", ".rvl", or empty for zlib data). In uncompress mode, pass
* compressed bytes and set @c isImage accordingly. Call @ref UThread::start() then
* @ref UThread::join() to obtain the result from @ref getCompressedData() or
* @ref getUncompressedData().
*
* Example compression:
* cv::Mat image;// an image
* CompressionThread ct(image);
* ct.start();
* ct.join();
* std::vector<unsigned char> bytes = ct.getCompressedData();
* @code
* cv::Mat image;
* CompressionThread ct(image, ".png");
* ct.start();
* ct.join();
* cv::Mat bytes = ct.getCompressedData();
* @endcode
*
* Example uncompression
* std::vector<unsigned char> bytes;// a compressed image
* CompressionThread ct(bytes);
* ct.start();
* ct.join();
* cv::Mat image = ct.getUncompressedData();
* Example uncompression:
* @code
* cv::Mat bytes;
* CompressionThread ct(bytes, true);
* ct.start();
* ct.join();
* cv::Mat image = ct.getUncompressedData();
* @endcode
*/
class RTABMAP_CORE_EXPORT CompressionThread : public UThread
{
public:
// format : ".png" ".jpg" "" (empty is general)
/**
* @brief Constructs a thread in compress mode.
* @param mat Source image or data matrix to compress.
* @param format Image format: @c ".png", @c ".jpg", @c ".rvl", or empty for zlib (@ref compressData2).
*/
CompressionThread(const cv::Mat & mat, const std::string & format = "");
/**
* @brief Constructs a thread in uncompress mode.
* @param bytes Compressed bytes (@c CV_8UC1).
* @param isImage If true, decode as image; otherwise decode as zlib data.
*/
CompressionThread(const cv::Mat & bytes, bool isImage);
/** @return Compressed output (@c CV_8UC1), valid after compress mode completes. */
const cv::Mat & getCompressedData() const {return compressedData_;}
/** @return Uncompressed output, valid after uncompress mode completes. */
cv::Mat & getUncompressedData() {return uncompressedData_;}
protected:
virtual void mainLoop();
@@ -71,24 +93,41 @@ private:
bool compressMode_;
};
/** @brief Encodes @p image to a byte buffer (OpenCV @c imencode or RVL for depth). */
std::vector<unsigned char> RTABMAP_CORE_EXPORT compressImage(const cv::Mat & image, const std::string & format = ".png");
/** @brief Same as @ref compressImage() but returns a @c CV_8UC1 row matrix. */
cv::Mat RTABMAP_CORE_EXPORT compressImage2(const cv::Mat & image, const std::string & format = ".png");
/** @brief Decodes compressed image bytes to a @cv::Mat. */
cv::Mat RTABMAP_CORE_EXPORT uncompressImage(const cv::Mat & bytes);
/** @brief Decodes compressed image bytes to a @cv::Mat. */
cv::Mat RTABMAP_CORE_EXPORT uncompressImage(const std::vector<unsigned char> & bytes);
/** @brief Compresses a matrix with zlib; appends rows, cols and type at the end. */
std::vector<unsigned char> RTABMAP_CORE_EXPORT compressData(const cv::Mat & data);
/** @brief Same as @ref compressData() but returns a @c CV_8UC1 row matrix. */
cv::Mat RTABMAP_CORE_EXPORT compressData2(const cv::Mat & data);
/** @brief Restores a matrix compressed with @ref compressData() or @ref compressData2(). */
cv::Mat RTABMAP_CORE_EXPORT uncompressData(const cv::Mat & bytes);
/** @brief Restores a matrix compressed with @ref compressData() or @ref compressData2(). */
cv::Mat RTABMAP_CORE_EXPORT uncompressData(const std::vector<unsigned char> & bytes);
/** @brief Restores a matrix from a raw compressed buffer. */
cv::Mat RTABMAP_CORE_EXPORT uncompressData(const unsigned char * bytes, unsigned long size);
/** @brief Compresses a null-terminated string using @ref compressData2(). */
cv::Mat RTABMAP_CORE_EXPORT compressString(const std::string & str);
/** @brief Decompresses a string produced by @ref compressString(). */
std::string RTABMAP_CORE_EXPORT uncompressString(const cv::Mat & bytes);
/**
* @brief Detects the compression format of depth image bytes.
* @return @c ".rvl" if the buffer has an RVL signature, otherwise @c ".png".
*/
std::string RTABMAP_CORE_EXPORT compressedDepthFormat(const cv::Mat & bytes);
/** @overload */
std::string RTABMAP_CORE_EXPORT compressedDepthFormat(const std::vector<unsigned char> & bytes);
/** @overload */
std::string RTABMAP_CORE_EXPORT compressedDepthFormat(const unsigned char * bytes, size_t size);
} /* namespace rtabmap */
+5
View File
@@ -111,6 +111,11 @@ add_executable(test_geodeticcoords test_geodeticcoords.cpp)
target_link_libraries(test_geodeticcoords gtest_main rtabmap_core)
gtest_discover_tests(test_geodeticcoords)
#Compression.h
add_executable(test_compression test_compression.cpp)
target_link_libraries(test_compression gtest_main rtabmap_core)
gtest_discover_tests(test_compression)
#Signature.h
add_executable(test_signature test_signature.cpp)
target_link_libraries(test_signature gtest_main rtabmap_core)
+185
View File
@@ -0,0 +1,185 @@
#include <gtest/gtest.h>
#include <rtabmap/core/Compression.h>
#include <opencv2/core.hpp>
using namespace rtabmap;
namespace {
void expectMatEqual(const cv::Mat & a, const cv::Mat & b)
{
ASSERT_FALSE(a.empty());
ASSERT_FALSE(b.empty());
ASSERT_EQ(a.rows, b.rows);
ASSERT_EQ(a.cols, b.cols);
ASSERT_EQ(a.type(), b.type());
ASSERT_EQ(a.channels(), b.channels());
if(a.depth() == CV_32F)
{
for(int r = 0; r < a.rows; ++r)
{
for(int c = 0; c < a.cols; ++c)
{
EXPECT_NEAR(a.at<float>(r, c), b.at<float>(r, c), 1e-5f);
}
}
}
else if(a.channels() == 1)
{
EXPECT_EQ(cv::countNonZero(a != b), 0);
}
else
{
EXPECT_EQ(cv::norm(a, b, cv::NORM_INF), 0);
}
}
} // namespace
TEST(CompressionTest, CompressImagePngRoundTrip)
{
const cv::Mat image = (cv::Mat_<uchar>(2, 3) << 10, 20, 30, 40, 50, 60);
const std::vector<unsigned char> bytes = compressImage(image, ".png");
ASSERT_FALSE(bytes.empty());
EXPECT_EQ(compressedDepthFormat(bytes), ".png");
const cv::Mat restored = uncompressImage(bytes);
expectMatEqual(restored, image);
}
TEST(CompressionTest, CompressImage2AndVectorOverloadMatch)
{
const cv::Mat image = cv::Mat::ones(8, 8, CV_8UC1) * 127;
const cv::Mat bytesMat = compressImage2(image, ".png");
const std::vector<unsigned char> bytesVec = compressImage(image, ".png");
ASSERT_FALSE(bytesMat.empty());
ASSERT_EQ(bytesMat.type(), CV_8UC1);
ASSERT_EQ(bytesVec.size(), static_cast<size_t>(bytesMat.cols));
const cv::Mat restoredFromMat = uncompressImage(bytesMat);
const cv::Mat restoredFromVec = uncompressImage(bytesVec);
expectMatEqual(restoredFromMat, image);
expectMatEqual(restoredFromVec, image);
}
TEST(CompressionTest, CompressImage2AndVectorOverloadMatchRgb)
{
cv::Mat image(16, 16, CV_8UC3);
for(int r = 0; r < image.rows; ++r)
{
for(int c = 0; c < image.cols; ++c)
{
image.at<cv::Vec3b>(r, c) = cv::Vec3b(
static_cast<uchar>(r * 10),
static_cast<uchar>(c * 10),
static_cast<uchar>((r + c) * 5));
}
}
const cv::Mat bytesMat = compressImage2(image, ".png");
const std::vector<unsigned char> bytesVec = compressImage(image, ".png");
ASSERT_FALSE(bytesMat.empty());
ASSERT_EQ(bytesMat.type(), CV_8UC1);
ASSERT_EQ(bytesVec.size(), static_cast<size_t>(bytesMat.cols));
EXPECT_LT(bytesMat.total() * bytesMat.elemSize(), image.total() * image.elemSize());
const cv::Mat restoredFromMat = uncompressImage(bytesMat);
const cv::Mat restoredFromVec = uncompressImage(bytesVec);
expectMatEqual(restoredFromMat, image);
expectMatEqual(restoredFromVec, image);
}
TEST(CompressionTest, CompressImageRvlRoundTrip)
{
cv::Mat depth(4, 5, CV_16UC1);
for(int r = 0; r < depth.rows; ++r)
{
for(int c = 0; c < depth.cols; ++c)
{
depth.at<uint16_t>(r, c) = static_cast<uint16_t>(1000 + r * 10 + c);
}
}
const cv::Mat bytes = compressImage2(depth, ".rvl");
ASSERT_FALSE(bytes.empty());
EXPECT_EQ(compressedDepthFormat(bytes), ".rvl");
const cv::Mat restored = uncompressImage(bytes);
expectMatEqual(restored, depth);
}
TEST(CompressionTest, CompressDataRoundTrip)
{
const cv::Mat data = (cv::Mat_<float>(2, 2) << 1.f, 2.f, 3.f, 4.f);
const std::vector<unsigned char> bytes = compressData(data);
ASSERT_FALSE(bytes.empty());
const cv::Mat restored = uncompressData(bytes);
expectMatEqual(restored, data);
}
TEST(CompressionTest, CompressData2RoundTrip)
{
const cv::Mat data = (cv::Mat_<double>(1, 4) << 1.0, -2.0, 3.5, 4.25);
const cv::Mat bytes = compressData2(data);
ASSERT_FALSE(bytes.empty());
ASSERT_EQ(bytes.type(), CV_8UC1);
const cv::Mat restored = uncompressData(bytes);
expectMatEqual(restored, data);
}
TEST(CompressionTest, CompressStringRoundTrip)
{
const std::string text = "rtabmap compression test";
const cv::Mat bytes = compressString(text);
ASSERT_FALSE(bytes.empty());
EXPECT_EQ(uncompressString(bytes), text);
}
TEST(CompressionTest, EmptyInputReturnsEmptyOutput)
{
EXPECT_TRUE(compressImage(cv::Mat(), ".png").empty());
EXPECT_TRUE(compressImage2(cv::Mat(), ".png").empty());
EXPECT_TRUE(compressData(cv::Mat()).empty());
EXPECT_TRUE(compressData2(cv::Mat()).empty());
EXPECT_TRUE(uncompressImage(cv::Mat()).empty());
EXPECT_TRUE(uncompressData(cv::Mat()).empty());
EXPECT_TRUE(compressedDepthFormat(cv::Mat()).empty());
EXPECT_EQ(uncompressString(cv::Mat()), "");
}
TEST(CompressionTest, CompressionThreadUncompressImage)
{
const cv::Mat image = cv::Mat::ones(16, 16, CV_8UC1) * 200;
const cv::Mat compressed = compressImage2(image, ".png");
ASSERT_FALSE(compressed.empty());
EXPECT_LT(compressed.total() * compressed.elemSize(), image.total() * image.elemSize());
CompressionThread uncompressThread(compressed, true);
uncompressThread.start();
uncompressThread.join();
expectMatEqual(uncompressThread.getUncompressedData(), image);
}
TEST(CompressionTest, CompressionThreadDataRoundTrip)
{
const cv::Mat data = (cv::Mat_<int>(2, 3) << 1, 2, 3, 4, 5, 6);
CompressionThread compressThread(data);
compressThread.start();
compressThread.join();
const cv::Mat compressed = compressThread.getCompressedData();
ASSERT_FALSE(compressed.empty());
CompressionThread uncompressThread(compressed, false);
uncompressThread.start();
uncompressThread.join();
expectMatEqual(uncompressThread.getUncompressedData(), data);
}