|
a |
|
b/src/mapping.cpp |
|
|
1 |
#include <Rcpp.h> |
|
|
2 |
|
|
|
3 |
// OpenCV |
|
|
4 |
#include <opencv2/opencv.hpp> |
|
|
5 |
#include "opencv2/xfeatures2d.hpp" |
|
|
6 |
#include "opencv2/features2d.hpp" |
|
|
7 |
#include "opencv2/shape/shape_transformer.hpp" |
|
|
8 |
|
|
|
9 |
// Internal functions |
|
|
10 |
#include "auxiliary.h" |
|
|
11 |
|
|
|
12 |
using namespace Rcpp; |
|
|
13 |
using namespace std; |
|
|
14 |
using namespace cv; |
|
|
15 |
using namespace cv::xfeatures2d; |
|
|
16 |
|
|
|
17 |
// [[Rcpp::export]] |
|
|
18 |
Rcpp::NumericMatrix applyMapping(Rcpp::NumericMatrix coords, Rcpp::List mapping) |
|
|
19 |
{ |
|
|
20 |
// Get coordinates as Point2f |
|
|
21 |
std::vector<cv::Point2f> coords_mat = numericMatrixToPoint2f(coords); |
|
|
22 |
std::vector<cv::Point2f> coords_temp; |
|
|
23 |
|
|
|
24 |
// seed |
|
|
25 |
cv::setRNGSeed(0); |
|
|
26 |
RNG rng(12345); |
|
|
27 |
Scalar value; |
|
|
28 |
|
|
|
29 |
// list |
|
|
30 |
int n = mapping.size(); |
|
|
31 |
|
|
|
32 |
// iterate over the list |
|
|
33 |
for(int i=0; i<n; i++) { |
|
|
34 |
|
|
|
35 |
// get the current mapping |
|
|
36 |
Rcpp::List cur_mapping = mapping[i]; |
|
|
37 |
|
|
|
38 |
// Get transformation matrix |
|
|
39 |
cv::Mat h = numericMatrixToMat(cur_mapping[0]); |
|
|
40 |
if(h.cols > 0){ |
|
|
41 |
|
|
|
42 |
// transform coordinates |
|
|
43 |
cv::perspectiveTransform(coords_mat, coords_temp, h); |
|
|
44 |
coords_mat = coords_temp; |
|
|
45 |
|
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
// non-rigid warping |
|
|
49 |
if(cur_mapping[1] != R_NilValue){ |
|
|
50 |
|
|
|
51 |
// Get landmarks as Point2f |
|
|
52 |
Rcpp::List keypoints = cur_mapping[1]; |
|
|
53 |
std::vector<cv::Point2f> ref_mat = numericMatrixToPoint2f(keypoints[0]); |
|
|
54 |
std::vector<cv::Point2f> query_mat = numericMatrixToPoint2f(keypoints[1]); |
|
|
55 |
|
|
|
56 |
// get matches |
|
|
57 |
std::vector<cv::DMatch> matches; |
|
|
58 |
for (unsigned int i = 0; i < ref_mat.size(); i++) |
|
|
59 |
matches.push_back(cv::DMatch(i, i, 0)); |
|
|
60 |
|
|
|
61 |
// calculate transformation |
|
|
62 |
Ptr<ThinPlateSplineShapeTransformer> tps = cv::createThinPlateSplineShapeTransformer(0); |
|
|
63 |
tps->estimateTransformation(query_mat, ref_mat, matches); |
|
|
64 |
|
|
|
65 |
// apply transformation to coordinates |
|
|
66 |
tps->applyTransformation(coords_mat, coords_temp); |
|
|
67 |
|
|
|
68 |
} else { |
|
|
69 |
|
|
|
70 |
coords_temp = coords_mat; |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
// set initial coordinates and registered one |
|
|
74 |
coords_mat = coords_temp; |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
// return registered coordinates as numeric matrix |
|
|
78 |
Rcpp::NumericMatrix coords_regToMat = point2fToNumericMatrix(coords_mat); |
|
|
79 |
|
|
|
80 |
// return |
|
|
81 |
return coords_regToMat; |
|
|
82 |
} |