Skip to content
Snippets Groups Projects
Commit ae60ce3c authored by Sebastian Hahta's avatar Sebastian Hahta
Browse files

bug/rectification

parent b566ee2e
No related branches found
No related tags found
No related merge requests found
...@@ -140,25 +140,29 @@ void StereoRectification::rectify(cv::InputOutputArray im, Channel c) { ...@@ -140,25 +140,29 @@ void StereoRectification::rectify(cv::InputOutputArray im, Channel c) {
} }
cv::Mat StereoRectification::getPose(Channel c) { cv::Mat StereoRectification::getPose(Channel c) {
// NOTE: FTL poses are camera-to-world transformations while the parameters
// in calibration are world-to-camera. cv::stereoRectify() rotation
// is unrectified-to-rectified.
using ftl::calibration::transform::inverse; using ftl::calibration::transform::inverse;
if (enabled_ && valid_) { if (enabled_ && valid_) {
cv::Mat T = cv::Mat::eye(4, 4, CV_64FC1); cv::Mat R = cv::Mat::eye(4, 4, CV_64FC1);
if (c == Channel::Left) { if (c == Channel::Left) {
R_l_.copyTo(T(cv::Rect(0, 0, 3, 3))); R_l_.copyTo(R(cv::Rect(0, 0, 3, 3)));
return calib_left_.extrinsic.matrix() * inverse(T); return inverse(R * calib_left_.extrinsic.matrix());
} }
else if (c == Channel::Right) { else if (c == Channel::Right) {
R_r_.copyTo(T(cv::Rect(0, 0, 3, 3))); R_r_.copyTo(R(cv::Rect(0, 0, 3, 3)));
return calib_right_.extrinsic.matrix() * inverse(T); return inverse(R * calib_right_.extrinsic.matrix());
} }
} }
else { else {
if (c == Channel::Left) { if (c == Channel::Left) {
return calib_left_.extrinsic.matrix(); return inverse(calib_left_.extrinsic.matrix());
} }
else if (c == Channel::Right) { else if (c == Channel::Right) {
return calib_right_.extrinsic.matrix(); return inverse(calib_right_.extrinsic.matrix());
} }
} }
throw ftl::exception("Invalid channel, expected Left or Right"); throw ftl::exception("Invalid channel, expected Left or Right");
......
...@@ -21,12 +21,15 @@ namespace detail { ...@@ -21,12 +21,15 @@ namespace detail {
/** /**
* Stereo rectification. Performs rectification for left and right channels. * Stereo rectification. Performs rectification for left and right channels.
* Rectified image is same size as input image. * Rectified image is same size as input image. Camera parameters reported by
* getPose() and cameraMatrix() are for rectified camera (if enabled and valid
* calibration set).
*/ */
class StereoRectification : public ftl::Configurable { class StereoRectification : public ftl::Configurable {
public: public:
StereoRectification(nlohmann::json &config, cv::Size image_size); StereoRectification(nlohmann::json &config, cv::Size image_size);
/** Set OpenCV interpolation mode, see cv::InterpolationFlags */
void setInterpolation(int interpolation); void setInterpolation(int interpolation);
void setSize(cv::Size); void setSize(cv::Size);
...@@ -38,26 +41,23 @@ public: ...@@ -38,26 +41,23 @@ public:
void rectify(cv::InputOutputArray im, ftl::codecs::Channel c); void rectify(cv::InputOutputArray im, ftl::codecs::Channel c);
/** /** Enable/disable rectification. TODO: move outside (to stereovideo)?
* Enable/disable rectification.
*/ */
void setEnabled(bool enabled); void setEnabled(bool enabled);
bool enabled(); bool enabled();
/** /** Get camera pose (camera to world). Returns rectified pose if
* Get camera pose (rectified if enabled and valid) * rectification is enabled (and valid calibration is set).
*/ */
cv::Mat getPose(ftl::codecs::Channel c = ftl::codecs::Channel::Left); cv::Mat getPose(ftl::codecs::Channel c = ftl::codecs::Channel::Left);
/** /** Get intrinsic matrix. */
* Get intrinsic matrix.
*/
cv::Mat cameraMatrix(ftl::codecs::Channel c = ftl::codecs::Channel::Left); cv::Mat cameraMatrix(ftl::codecs::Channel c = ftl::codecs::Channel::Left);
cv::Mat cameraMatrix(cv::Size size, ftl::codecs::Channel c = ftl::codecs::Channel::Left); cv::Mat cameraMatrix(cv::Size size, ftl::codecs::Channel c = ftl::codecs::Channel::Left);
/** Stereo baseline */ /** Stereo baseline. In same unit as calibration (usually meters) */
double baseline(); double baseline();
/** Disparity offset */ /** Disparity offset (pixels) */
double doff(); double doff();
double doff(cv::Size); double doff(cv::Size);
...@@ -65,13 +65,12 @@ protected: ...@@ -65,13 +65,12 @@ protected:
void calculateParameters(); void calculateParameters();
private: private:
cv::Size calib_resolution_;
ftl::calibration::CalibrationData::Calibration calib_left_; ftl::calibration::CalibrationData::Calibration calib_left_;
ftl::calibration::CalibrationData::Calibration calib_right_; ftl::calibration::CalibrationData::Calibration calib_right_;
cv::Size image_resolution_; cv::Size image_resolution_;
// rectification parameters and maps // rectification parameters
bool enabled_; bool enabled_;
bool valid_; bool valid_;
int interpolation_; int interpolation_;
...@@ -82,12 +81,13 @@ private: ...@@ -82,12 +81,13 @@ private:
cv::Mat P_l_; cv::Mat P_l_;
cv::Mat P_r_; cv::Mat P_r_;
// rectification maps for cv::remap(); should be CV_16SC2 if remap done on
// CPU and CV_32SC2 for GPU (generated by calculateParameters(), used by
// rectify())
std::pair<cv::Mat,cv::Mat> map_l_; std::pair<cv::Mat,cv::Mat> map_l_;
std::pair<cv::Mat,cv::Mat> map_r_; std::pair<cv::Mat,cv::Mat> map_r_;
// temporary buffers // temporary buffers for left/right
// cv::cuda::HostMem tmp_l_;
// cv::cuda::HostMem tmp_r_;
cv::Mat tmp_l_; cv::Mat tmp_l_;
cv::Mat tmp_r_; cv::Mat tmp_r_;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment