2011-05-05 14:12:32 +02:00
|
|
|
#ifndef __OPENCV_WARPERS_HPP__
|
|
|
|
#define __OPENCV_WARPERS_HPP__
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
#include <opencv2/core/core.hpp>
|
|
|
|
#include <opencv2/imgproc/imgproc.hpp>
|
|
|
|
|
|
|
|
class Warper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { PLANE, CYLINDRICAL, SPHERICAL };
|
|
|
|
|
2011-05-05 14:12:32 +02:00
|
|
|
static cv::Ptr<Warper> createByCameraFocal(float focal, int type);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
virtual ~Warper() {}
|
|
|
|
|
2011-05-16 07:11:09 +02:00
|
|
|
cv::Point operator ()(const cv::Mat &src, float focal, const cv::Mat& R, cv::Mat &dst,
|
2011-05-04 13:09:42 +02:00
|
|
|
int interp_mode = cv::INTER_LINEAR, int border_mode = cv::BORDER_REFLECT);
|
|
|
|
|
|
|
|
protected:
|
2011-05-16 07:11:09 +02:00
|
|
|
virtual cv::Point warp(const cv::Mat &src, float focal, const cv::Mat& R, cv::Mat &dst,
|
2011-05-04 13:09:42 +02:00
|
|
|
int interp_mode, int border_mode) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ProjectorBase
|
|
|
|
{
|
2011-05-16 07:11:09 +02:00
|
|
|
void setCameraMatrix(const cv::Mat& R);
|
2011-05-04 13:09:42 +02:00
|
|
|
|
|
|
|
cv::Size size;
|
|
|
|
float focal;
|
2011-05-16 07:11:09 +02:00
|
|
|
float r[9];
|
|
|
|
float rinv[9];
|
2011-05-04 13:09:42 +02:00
|
|
|
float scale;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class P>
|
|
|
|
class WarperBase : public Warper
|
|
|
|
{
|
|
|
|
protected:
|
2011-05-16 07:11:09 +02:00
|
|
|
cv::Point warp(const cv::Mat &src, float focal, const cv::Mat &R, cv::Mat &dst,
|
2011-05-04 13:09:42 +02:00
|
|
|
int interp_mode, int border_mode);
|
|
|
|
|
|
|
|
// Detects ROI of the destination image. It's correct for any projection.
|
|
|
|
virtual void detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br);
|
|
|
|
|
|
|
|
// Detects ROI of the destination image by walking over image border.
|
|
|
|
// Correctness for any projection isn't guaranteed.
|
|
|
|
void detectResultRoiByBorder(cv::Point &dst_tl, cv::Point &dst_br);
|
|
|
|
|
|
|
|
cv::Size src_size_;
|
|
|
|
P projector_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PlaneProjector : ProjectorBase
|
|
|
|
{
|
|
|
|
void mapForward(float x, float y, float &u, float &v);
|
|
|
|
void mapBackward(float u, float v, float &x, float &y);
|
|
|
|
|
|
|
|
float plane_dist;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Projects image onto z = plane_dist plane
|
|
|
|
class PlaneWarper : public WarperBase<PlaneProjector>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PlaneWarper(float plane_dist = 1.f, float scale = 1.f)
|
|
|
|
{
|
|
|
|
projector_.plane_dist = plane_dist;
|
|
|
|
projector_.scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct SphericalProjector : ProjectorBase
|
|
|
|
{
|
|
|
|
void mapForward(float x, float y, float &u, float &v);
|
|
|
|
void mapBackward(float u, float v, float &x, float &y);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Projects image onto unit sphere with origin at (0, 0, 0).
|
|
|
|
// Poles are located at (0, -1, 0) and (0, 1, 0) points.
|
|
|
|
class SphericalWarper : public WarperBase<SphericalProjector>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SphericalWarper(float scale = 300.f) { projector_.scale = scale; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CylindricalProjector : ProjectorBase
|
|
|
|
{
|
|
|
|
void mapForward(float x, float y, float &u, float &v);
|
|
|
|
void mapBackward(float u, float v, float &x, float &y);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Projects image onto x * x + z * z = 1 cylinder
|
|
|
|
class CylindricalWarper : public WarperBase<CylindricalProjector>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CylindricalWarper(float scale = 300.f) { projector_.scale = scale; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void detectResultRoi(cv::Point &dst_tl, cv::Point &dst_br)
|
|
|
|
{
|
|
|
|
WarperBase<CylindricalProjector>::detectResultRoiByBorder(dst_tl, dst_br);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "warpers_inl.hpp"
|
|
|
|
|
2011-05-05 14:12:32 +02:00
|
|
|
#endif // __OPENCV_WARPERS_HPP__
|