|
| 1 | + |
| 2 | +#include "precomp.hpp" |
| 3 | +#include "opencv2/rgbd/twist.hpp" |
| 4 | + |
| 5 | +namespace cv |
| 6 | +{ |
| 7 | +namespace rgbd |
| 8 | +{ |
| 9 | + |
| 10 | +Twist::Twist() |
| 11 | +{ |
| 12 | + _optflow = DISOpticalFlow::create(DISOpticalFlow::PRESET_MEDIUM); |
| 13 | + _flow = new cv::Mat(); |
| 14 | +} |
| 15 | + |
| 16 | +void Twist::interactionMatrix(const cv::Mat& uv, const cv::Mat& depth, const cv::Mat& K, cv::Mat& J) |
| 17 | +{ |
| 18 | + CV_Assert(uv.cols == depth.cols); |
| 19 | + CV_Assert(depth.type() == CV_32F); // Validate depth input type |
| 20 | + |
| 21 | + J.create(depth.cols * 2, 6, CV_32F); |
| 22 | + J.setTo(0); |
| 23 | + |
| 24 | + cv::Mat Kinv; |
| 25 | + cv::invert(K, Kinv); |
| 26 | + |
| 27 | + cv::Mat xy(3, 1, CV_32F); |
| 28 | + cv::Mat Jp(2, 6, CV_32F); |
| 29 | + for (int i = 0; i < uv.cols; i++) |
| 30 | + { |
| 31 | + const float z = depth.at<float>(i); |
| 32 | + if (cv::abs(z) < 0.001f) |
| 33 | + continue; |
| 34 | + |
| 35 | + const cv::Point3f p(uv.at<float>(0, i), uv.at<float>(1, i), 1.0); |
| 36 | + |
| 37 | + // convert to normalized image-plane coordinates |
| 38 | + xy = Kinv * cv::Mat(p); |
| 39 | + float x = xy.at<float>(0); |
| 40 | + float y = xy.at<float>(1); |
| 41 | + |
| 42 | + // 2x6 Jacobian for this point |
| 43 | + Jp = (cv::Mat_<float>(2, 6) << -1 / z, 0.0, x / z, x * y, -(1 + x * x), y, 0.0, -1 / z, |
| 44 | + y / z, 1 + y * y, -x * y, -x); |
| 45 | + |
| 46 | + Jp = K(cv::Rect(0, 0, 2, 2)) * Jp; |
| 47 | + |
| 48 | + // push into Jacobian |
| 49 | + Jp.copyTo(J(cv::Rect(0, 2 * i, 6, 2))); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +cv::Vec6d Twist::compute(const cv::Mat& im0, const cv::Mat& im1, const cv::Mat depths0, const cv::Mat& K, |
| 54 | + const double dt) |
| 55 | +{ |
| 56 | + _optflow->calc(im0, im1, *_flow); |
| 57 | + |
| 58 | + const int N = (im0.cols * im0.rows) * 0.1; |
| 59 | + cv::Mat uv(2, N, CV_32F); |
| 60 | + cv::Mat depth(1, N, CV_32F); |
| 61 | + cv::Mat flow(1, 2 * N, CV_32F); |
| 62 | + for (int i = 0; i < N; i++) |
| 63 | + { |
| 64 | + int x = rand() % im0.cols; |
| 65 | + int y = rand() % im0.rows; |
| 66 | + uv.at<float>(0, i) = x; |
| 67 | + uv.at<float>(1, i) = y; |
| 68 | + depth.at<float>(i) = depths0.at<float>(y, x); |
| 69 | + flow.at<float>(0, 2 * i) = _flow->at<cv::Point2f>(y, x).x; |
| 70 | + flow.at<float>(0, 2 * i + 1) = _flow->at<cv::Point2f>(y, x).y; |
| 71 | + } |
| 72 | + cv::Mat J; |
| 73 | + interactionMatrix(uv, depth, K, J); |
| 74 | + |
| 75 | + cv::Mat Jinv; |
| 76 | + cv::invert(J, Jinv, cv::DECOMP_SVD); |
| 77 | + cv::Mat duv = flow / dt; |
| 78 | + cv::Mat twist = Jinv * duv.t(); |
| 79 | + return twist; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace rgbd |
| 83 | +} // namespace cv |
0 commit comments