From 3f163bd4332351d8e93c6443f31dfe665980b330 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 8 Nov 2020 10:22:40 -0500 Subject: [PATCH] Fix pixels being rounded to int values in some cases (#2228) --- dlib/matrix/matrix_utilities.h | 2 +- dlib/test/image.cpp | 17 +++++++++++++++++ dlib/test/pixel.cpp | 8 +++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/dlib/matrix/matrix_utilities.h b/dlib/matrix/matrix_utilities.h index e28ac3bce..18f52c479 100644 --- a/dlib/matrix/matrix_utilities.h +++ b/dlib/matrix/matrix_utilities.h @@ -3049,7 +3049,7 @@ namespace dlib const M& m ) { - pixel = static_cast(m(0)); + pixel = static_cast

(m(0)); } }; diff --git a/dlib/test/image.cpp b/dlib/test/image.cpp index dfd6965fd..dc8fc6bb5 100644 --- a/dlib/test/image.cpp +++ b/dlib/test/image.cpp @@ -1754,6 +1754,23 @@ namespace << " error: " << max(abs(chip-255)) ); } + // So the same as above, but for an image with float values that are all the same to make + // sure noting funny happens for float images. + { + print_spinner(); + const long nr = 53; + const long nc = 67; + const long size = 8*9; + const double angle = 30*pi/180; + + matrix img(501,501), chip; + img = 1234.5; + chip_details details(centered_rect(center(get_rect(img)),nr,nc), size, angle); + extract_image_chip(img, details, chip); + DLIB_TEST_MSG(max(abs(chip-1234.5))==0,"nr: " << nr << " nc: "<< nc << " size: " << size << " angle: " << angle + << " error: " << max(abs(chip-255)) ); + } + { // Make sure that the interpolation in extract_image_chip() keeps stuff in the diff --git a/dlib/test/pixel.cpp b/dlib/test/pixel.cpp index 40772b130..775f6c2b9 100644 --- a/dlib/test/pixel.cpp +++ b/dlib/test/pixel.cpp @@ -449,7 +449,7 @@ namespace p_gray = 8; p_schar = 8; p_int = 8; - p_float = 8; + p_float = 8.5; p_hsi.h = 9; p_hsi.s = 10; @@ -501,7 +501,7 @@ namespace DLIB_TEST(p_gray == 8); DLIB_TEST(p_schar == 8); DLIB_TEST(p_int == 8); - DLIB_TEST(p_float == 8); + DLIB_TEST(p_float == 8.5); DLIB_TEST(p_hsi.h == 9); DLIB_TEST(p_hsi.s == 10); @@ -550,7 +550,7 @@ namespace DLIB_TEST(p_gray == 8); DLIB_TEST(p_int == 8); - DLIB_TEST(p_float == 8); + DLIB_TEST(p_float == 8.5); DLIB_TEST(p_schar == 8); DLIB_TEST(p_hsi.h == 9); @@ -566,6 +566,7 @@ namespace assign_pixel(p_rgb,0); assign_pixel(p_lab,0); + vector_to_pixel(p_float, m_float); vector_to_pixel(p_gray, m_gray); vector_to_pixel(p_hsi, m_hsi); vector_to_pixel(p_rgb, m_rgb); @@ -581,6 +582,7 @@ namespace DLIB_TEST(p_rgba.alpha == 7); DLIB_TEST(p_gray == 8); + DLIB_TEST(p_float == 8.5); DLIB_TEST(p_hsi.h == 9); DLIB_TEST(p_hsi.s == 10);