Test image warping.
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030300)
#include <catch_amalgamated.hpp>
#include <iostream>
#include <visp3/core/vpImageTools.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
namespace
{
static const double g_threshold_value = 0.5;
static const double g_threshold_percentage = 0.9;
static const double g_threshold_percentage_bilinear = 0.75;
static const double g_threshold_percentage_pers = 0.75;
static const double g_threshold_percentage_pers_bilinear = 0.65;
static const std::vector<std::string> interp_names = { "Nearest Neighbor", "Bilinear" };
static const std::vector<std::string> suffixes = { "_NN.png", "_bilinear.png" };
double threshold_percentage, double &percentage)
{
double nb_valid = 0;
return false;
}
for (
unsigned int i = 0; i < I1.
getHeight(); i++) {
for (
unsigned int j = 0; j < I1.
getWidth(); j++) {
nb_valid +=
vpMath::abs(I1[i][j] - I2[i][j]) < threshold_val ? 1 : 0;
}
}
percentage = nb_valid / I1.
getSize();
return percentage >= threshold_percentage;
}
double threshold_percentage, double &percentage)
{
double nb_valid = 0;
return false;
}
for (
unsigned int i = 0; i < I1.
getHeight(); i++) {
for (
unsigned int j = 0; j < I1.
getWidth(); j++) {
if (
vpMath::abs(I1[i][j].R - I2[i][j].R) < threshold_val) {
nb_valid++;
}
if (
vpMath::abs(I1[i][j].G - I2[i][j].G) < threshold_val) {
nb_valid++;
}
if (
vpMath::abs(I1[i][j].B - I2[i][j].B) < threshold_val) {
nb_valid++;
}
}
}
percentage = nb_valid / (3 * I1.
getSize());
return percentage >= threshold_percentage;
}
}
TEST_CASE("Affine warp on grayscale", "[warp_image]")
{
SECTION("Identity")
{
M.eye();
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Empty destination")
{
CHECK((I == I_affine));
}
SECTION("Initialized destination")
{
CHECK((I == I_affine));
}
}
}
}
SECTION("Rotation 45 deg")
{
M.eye();
M[0][0] = cos(theta);
M[0][1] = -sin(theta);
M[1][0] = sin(theta);
M[1][1] = cos(theta);
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref, I_affine, g_threshold_value, g_threshold_percentage, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " Ref): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_opencv, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " OpenCV): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_pil, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " PIL): " << percentage << std::endl;
CHECK(equal);
}
}
}
}
SECTION("SRT")
{
M.eye();
const double scale = 0.83;
M[0][0] = scale * cos(theta);
M[0][1] = -scale * sin(theta);
M[1][0] = scale * sin(theta);
M[1][1] = scale * cos(theta);
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref, I_affine, g_threshold_value, g_threshold_percentage, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " Ref): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_opencv, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " OpenCV): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_pil, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " PIL): " << percentage << std::endl;
CHECK(equal);
}
}
}
}
}
TEST_CASE("Affine warp on color", "[warp_image]")
{
SECTION("Identity")
{
M.eye();
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Empty destination")
{
CHECK((I == I_affine));
}
SECTION("Initialized destination")
{
CHECK((I == I_affine));
}
}
}
}
SECTION("Rotation 45 deg")
{
M.eye();
M[0][0] = cos(theta);
M[0][1] = -sin(theta);
M[1][0] = sin(theta);
M[1][1] = cos(theta);
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref, I_affine, g_threshold_value, g_threshold_percentage, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " Ref): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_opencv, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " OpenCV): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_pil, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (45 deg " << interp_names[i] << " PIL): " << percentage << std::endl;
CHECK(equal);
}
}
}
}
SECTION("SRT")
{
M.eye();
const double scale = 0.83;
M[0][0] = scale * cos(theta);
M[0][1] = -scale * sin(theta);
M[1][0] = scale * sin(theta);
M[1][1] = scale * cos(theta);
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " Ref): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_opencv, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " OpenCV): " << percentage << std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref_pil, I_affine, g_threshold_value,
(i == 0) ? g_threshold_percentage : g_threshold_percentage_bilinear, percentage);
std::cout << "Percentage valid pixels (SRT " << interp_names[i] << " PIL): " << percentage << std::endl;
CHECK(equal);
}
}
}
}
}
TEST_CASE("Perspective warp on grayscale", "[warp_image]")
{
SECTION("Identity")
{
M.eye();
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Empty destination")
{
CHECK((I == I_perspective));
}
SECTION("Initialized destination")
{
CHECK((I == I_perspective));
}
}
}
}
SECTION("Rotation 45 deg")
{
M.eye();
M[0][0] = cos(theta);
M[0][1] = -sin(theta);
M[1][0] = sin(theta);
M[1][1] = cos(theta);
SECTION("Nearest Neighbor")
{
double percentage = 0.0;
bool equal = almostEqual(I_ref, I_perspective, g_threshold_value, g_threshold_percentage, percentage);
std::cout << "Percentage valid pixels (persp 45 deg): " << percentage << std::endl;
CHECK(equal);
}
}
SECTION("Homography")
{
M.eye();
M[0][0] = 1.8548;
M[0][1] = -0.0402;
M[0][2] = 114.9;
M[1][0] = 1.1209;
M[1][1] = 4.0106;
M[1][2] = 111;
M[2][0] = 0.0022;
M[2][1] = 0.0064;
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " Ref): " << percentage
<< std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref_opencv, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " OpenCV): " << percentage
<< std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref_pil, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " PIL): " << percentage
<< std::endl;
CHECK(equal);
}
}
}
}
}
TEST_CASE("Perspective warp on color", "[warp_image]")
{
SECTION("Identity")
{
M.eye();
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Empty destination")
{
CHECK((I == I_perspective));
}
SECTION("Initialized destination")
{
CHECK((I == I_perspective));
}
}
}
}
SECTION("Homography")
{
M.eye();
M[0][0] = 1.8548;
M[0][1] = -0.0402;
M[0][2] = 114.9;
M[1][0] = 1.1209;
M[1][1] = 4.0106;
M[1][2] = 111;
M[2][0] = 0.0022;
M[2][1] = 0.0064;
for (size_t i = 0; i < interp_methods.size(); i++) {
SECTION(interp_names[i])
{
SECTION("Against reference implementation")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " Ref): " << percentage
<< std::endl;
CHECK(equal);
}
SECTION("Against OpenCV")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref_opencv, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " OpenCV): " << percentage
<< std::endl;
CHECK(equal);
}
SECTION("Against PIL")
{
double percentage = 0.0;
bool equal =
almostEqual(I_ref_pil, I_perspective, g_threshold_value,
(i == 0) ? g_threshold_percentage_pers : g_threshold_percentage_pers_bilinear, percentage);
std::cout << "Percentage valid pixels (Homography " << interp_names[i] << " PIL): " << percentage
<< std::endl;
CHECK(equal);
}
}
}
}
}
int main(int argc, char *argv[])
{
Catch::Session session;
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getSize() const
unsigned int getHeight() const
static double rad(double deg)
static Type abs(const Type &x)
Implementation of a matrix and operations on matrices.