Test matrix convolution.
#include <visp3/core/vpMatrix.h>
#include <visp3/core/vpTime.h>
namespace {
bool compareMatrix(
const vpMatrix &m,
const double *
const array) {
for (
unsigned int i = 0; i < m.
getRows(); i++) {
for (
unsigned int j = 0; j < m.
getCols(); j++) {
return false;
}
}
return true;
}
}
int main()
{
try {
{
A[0][0] = 16; A[0][1] = 2; A[0][2] = 3; A[0][3] = 13;
A[1][0] = 5; A[1][1] = 11; A[1][2] = 10; A[1][3] = 8;
A[2][0] = 9; A[2][1] = 7; A[2][2] = 6; A[2][3] = 12;
A[3][0] = 4; A[3][1] = 14; A[3][2] = 15; A[3][3] = 1;
B[0][0] = 1; B[0][1] = 3;
B[1][0] = 4; B[1][1] = 2;
{
double ground_truth[5*5] = {16, 50, 9, 22, 39,
69, 66, 59, 96, 50,
29, 88, 89, 82, 52,
40, 72, 95, 106, 27,
16, 64, 88, 34, 2};
std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
if (res.
getRows() != 5 || res.
getCols() != 5 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[4*4] = { 66, 59, 96, 50,
88, 89, 82, 52,
72, 95, 106, 27,
64, 88, 34, 2};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
if (res.
getRows() != 4 || res.
getCols() != 4 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[3*3] = { 66, 59, 96,
88, 89, 82,
72, 95, 106};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
if (res.
getRows() != 3 || res.
getCols() != 3 || !compareMatrix(res, ground_truth)) {
}
}
}
{
for (
unsigned int i = 0; i < A.
getRows(); i++)
for (
unsigned int j = 0; j < A.
getCols(); j++)
for (
unsigned int i = 0; i < B.
getRows(); i++)
for (
unsigned int j = 0; j < B.
getCols(); j++)
{
double ground_truth[5*7] = { 0, 0, 1, 2, 3, 4, 5,
0, 8, 14, 20, 26, 32, 26,
12, 36, 50, 64, 78, 92, 58,
24, 64, 86, 108, 130, 152, 90,
36, 84, 97, 110, 123, 136, 77};
std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
if (res.
getRows() != 5 || res.
getCols() != 7 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[2*6] = { 36, 50, 64, 78, 92, 58,
64, 86, 108, 130, 152, 90};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
if (res.
getRows() != 2 || res.
getCols() != 6 || !compareMatrix(res, ground_truth)) {
}
}
{
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
}
}
{
double ground_truth[5*7] = { 0, 0, 1, 2, 3, 4, 5,
0, 8, 14, 20, 26, 32, 26,
12, 36, 50, 64, 78, 92, 58,
24, 64, 86, 108, 130, 152, 90,
36, 84, 97, 110, 123, 136, 77};
std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, full):\n" << res << std::endl;
if (res.
getRows() != 5 || res.
getCols() != 7 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[4*2] = { 20, 26,
64, 78,
108, 130,
110, 123};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, same):\n" << res << std::endl;
if (res.
getRows() != 4 || res.
getCols() != 2 || !compareMatrix(res, ground_truth)) {
}
}
{
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(B, A, valid):\n" << res << std::endl;
}
}
}
{
A[0][0] = 16; A[0][1] = 2; A[0][2] = 3; A[0][3] = 13;
A[1][0] = 5; A[1][1] = 11; A[1][2] = 10; A[1][3] = 8;
A[2][0] = 9; A[2][1] = 7; A[2][2] = 6; A[2][3] = 12;
A[3][0] = 4; A[3][1] = 14; A[3][2] = 15; A[3][3] = 1;
B[0][0] = 8; B[0][1] = 1; B[0][2] = 6;
B[1][0] = 3; B[1][1] = 5; B[1][2] = 7;
B[2][0] = 4; B[2][1] = 9; B[2][2] = 2;
{
double ground_truth[6*6] = { 128, 32, 122, 119, 31, 78,
88, 179, 252, 208, 154, 139,
151, 275, 291, 378, 281, 154,
79, 271, 423, 366, 285, 106,
48, 171, 248, 292, 230, 31,
16, 92, 194, 167, 39, 2};
std::cout << "A:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, full):\n" << res << std::endl;
if (res.
getRows() != 6 || res.
getCols() != 6 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[4*4] = { 179, 252, 208, 154,
275, 291, 378, 281,
271, 423, 366, 285,
171, 248, 292, 230};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, same):\n" << res << std::endl;
if (res.
getRows() != 4 || res.
getCols() != 4 || !compareMatrix(res, ground_truth)) {
}
}
{
double ground_truth[2*2] = { 291, 378,
423, 366};
std::cout << "\nA:\n" << A << "\nB:\n" << B << "\nvpMatrix::conv2(A, B, valid):\n" << res << std::endl;
if (res.
getRows() != 2 || res.
getCols() != 2 || !compareMatrix(res, ground_truth)) {
}
}
}
std::cout <<
"Catch an exception: " << e.
what() << std::endl;
return EXIT_FAILURE;
}
}