| 464 | { |
| 465 | |
| 466 | inline point_transform_projective find_projective_transform_basic ( |
| 467 | const std::vector<dlib::vector<double,2> >& from_points, |
| 468 | const std::vector<dlib::vector<double,2> >& to_points |
| 469 | ) |
| 470 | /*! |
| 471 | ensures |
| 472 | - Uses the system of equations approach to finding a projective transform. |
| 473 | This is "Method 3" from Estimating Projective Transformation Matrix by |
| 474 | Zhengyou Zhang. |
| 475 | - It should be emphasized that the find_projective_transform_basic() |
| 476 | routine, which uses the most popular method for finding projective |
| 477 | transformations, doesn't really work well when the minimum error solution |
| 478 | doesn't have zero error. In this case, it can deviate by a large amount |
| 479 | from the proper minimum mean squared error transformation. Therefore, |
| 480 | our overall strategy will be to use the solution from |
| 481 | find_projective_transform_basic() as a starting point for a BFGS based |
| 482 | non-linear optimizer which will optimize the correct mean squared error |
| 483 | criterion. |
| 484 | |
| 485 | A great essay on this subject is Homography Estimation by Elan Dubrofsky. |
| 486 | !*/ |
| 487 | { |
| 488 | // make sure requires clause is not broken |
| 489 | DLIB_ASSERT(from_points.size() == to_points.size() && |
| 490 | from_points.size() >= 4, |
| 491 | "\t point_transform_projective find_projective_transform_basic(from_points, to_points)" |
| 492 | << "\n\t Invalid inputs were given to this function." |
| 493 | << "\n\t from_points.size(): " << from_points.size() |
| 494 | << "\n\t to_points.size(): " << to_points.size() |
| 495 | ); |
| 496 | |
| 497 | matrix<double,9,9> accum, u, v; |
| 498 | matrix<double,9,1> w; |
| 499 | matrix<double,2,9> B; |
| 500 | accum = 0; |
| 501 | B = 0; |
| 502 | for (unsigned long i = 0; i < from_points.size(); ++i) |
| 503 | { |
| 504 | dlib::vector<double,3> f = from_points[i]; |
| 505 | f.z() = 1; |
| 506 | dlib::vector<double,3> t = to_points[i]; |
| 507 | t.z() = 1; |
| 508 | |
| 509 | set_subm(B,0,0,1,3) = t.y()*trans(f); |
| 510 | set_subm(B,1,0,1,3) = trans(f); |
| 511 | |
| 512 | set_subm(B,0,3,1,3) = -t.x()*trans(f); |
| 513 | set_subm(B,1,6,1,3) = -t.x()*trans(f); |
| 514 | |
| 515 | accum += trans(B)*B; |
| 516 | } |
| 517 | |
| 518 | svd2(true, false, accum, u, w, v); |
| 519 | long j = index_of_min(w); |
| 520 | |
| 521 | return point_transform_projective(reshape(colm(u,j),3,3)); |
| 522 | } |
| 523 |
no test coverage detected