| 29 | logger dlog("test.matrix"); |
| 30 | |
| 31 | void test_max_point_interpolated() |
| 32 | { |
| 33 | struct peak_case |
| 34 | { |
| 35 | long rows; |
| 36 | long columns; |
| 37 | double x; |
| 38 | double y; |
| 39 | }; |
| 40 | const peak_case cases[] = { |
| 41 | {9, 9, 5.25, 2.75}, |
| 42 | {9, 9, 2.25, 6.125}, |
| 43 | {4, 10, 7.25, 1.75}, |
| 44 | {10, 3, 1.25, 7.75} |
| 45 | }; |
| 46 | |
| 47 | for (const auto& peak : cases) |
| 48 | { |
| 49 | matrix<double> m(peak.rows, peak.columns); |
| 50 | for (long r = 0; r < m.nr(); ++r) |
| 51 | { |
| 52 | for (long c = 0; c < m.nc(); ++c) |
| 53 | { |
| 54 | const double x = c - peak.x; |
| 55 | const double y = r - peak.y; |
| 56 | // A strictly concave quadratic with a known maximum, including |
| 57 | // a cross term so both coordinates contribute to the fit. |
| 58 | m(r,c) = 1000 - 3*x*x - 0.4*x*y - 2*y*y; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const dpoint expected(peak.x, peak.y); |
| 63 | DLIB_TEST(length(max_point_interpolated(m) - expected) < 1e-10); |
| 64 | DLIB_TEST(length(max_point_interpolated(trans(m)) - |
| 65 | dpoint(peak.y, peak.x)) < 1e-10); |
| 66 | |
| 67 | matrix<double,0,0,default_memory_manager,column_major_layout> column_major = m; |
| 68 | DLIB_TEST(length(max_point_interpolated(column_major) - expected) < 1e-10); |
| 69 | matrix<float> single_precision = matrix_cast<float>(m); |
| 70 | DLIB_TEST(length(max_point_interpolated(single_precision) - expected) < 1e-4); |
| 71 | } |
| 72 | |
| 73 | // Border maxima must not require a 3x3 neighborhood. |
| 74 | matrix<double> border(4,7); |
| 75 | const point corners[] = {point(0,0), point(6,0), point(0,3), point(6,3)}; |
| 76 | for (const auto& corner : corners) |
| 77 | { |
| 78 | border = 0; |
| 79 | border(corner.y(),corner.x()) = 1; |
| 80 | DLIB_TEST(max_point_interpolated(border) == dpoint(corner)); |
| 81 | } |
| 82 | |
| 83 | matrix<double,1,5> row; |
| 84 | for (long c = 0; c < row.nc(); ++c) |
| 85 | row(c) = 10 - (c-2.25)*(c-2.25); |
| 86 | DLIB_TEST(length(max_point_interpolated(row) - dpoint(2.25,0)) < 1e-10); |
| 87 | DLIB_TEST(length(max_point_interpolated(trans(row)) - dpoint(0,2.25)) < 1e-10); |
| 88 | matrix<double,1,1> singleton; |
no test coverage detected