2011-11-23 18:05:24 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2011-11-28 19:50:46 +08:00
|
|
|
#include <sstream>
|
2011-11-23 18:05:24 +08:00
|
|
|
#include "opencv2/core/core.hpp"
|
2011-11-30 14:20:29 +08:00
|
|
|
#include "opencv2/core/opengl_interop.hpp"
|
2011-11-23 18:05:24 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
2011-11-28 19:50:46 +08:00
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
2011-11-23 18:05:24 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::gpu;
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
class PointCloudRenderer
|
2011-11-23 18:05:24 +08:00
|
|
|
{
|
2011-11-30 14:20:29 +08:00
|
|
|
public:
|
|
|
|
PointCloudRenderer(const Mat& points, const Mat& img, double scale);
|
|
|
|
|
|
|
|
void onMouseEvent(int event, int x, int y, int flags);
|
|
|
|
void draw();
|
|
|
|
void update(int key, double aspect);
|
|
|
|
|
|
|
|
int fov_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int mouse_dx_;
|
|
|
|
int mouse_dy_;
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
double yaw_;
|
|
|
|
double pitch_;
|
|
|
|
Point3d pos_;
|
|
|
|
|
|
|
|
TickMeter tm_;
|
|
|
|
static const int step_;
|
|
|
|
int frame_;
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
GlCamera camera_;
|
|
|
|
GlArrays pointCloud_;
|
|
|
|
string fps_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool stop = false;
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void mouseCallback(int event, int x, int y, int flags, void* userdata)
|
2011-11-23 18:05:24 +08:00
|
|
|
{
|
2011-11-30 14:20:29 +08:00
|
|
|
if (stop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);
|
|
|
|
renderer->onMouseEvent(event, x, y, flags);
|
2011-11-23 18:05:24 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void openGlDrawCallback(void* userdata)
|
2011-11-23 18:05:24 +08:00
|
|
|
{
|
2011-11-30 14:20:29 +08:00
|
|
|
if (stop)
|
|
|
|
return;
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);
|
|
|
|
renderer->draw();
|
2011-11-23 18:05:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[])
|
|
|
|
{
|
|
|
|
const char* keys =
|
|
|
|
"{ l | left | | left image file name }"
|
|
|
|
"{ r | right | | right image file name }"
|
|
|
|
"{ i | intrinsic | | intrinsic camera parameters file name }"
|
|
|
|
"{ e | extrinsic | | extrinsic camera parameters file name }"
|
|
|
|
"{ d | ndisp | 256 | number of disparities }"
|
|
|
|
"{ s | scale | 1.0 | scale factor for point cloud }"
|
|
|
|
"{ h | help | false | print help message }";
|
|
|
|
|
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
|
|
|
|
2011-11-28 16:23:26 +08:00
|
|
|
if (cmd.get<bool>("help"))
|
2011-11-23 18:05:24 +08:00
|
|
|
{
|
|
|
|
cout << "Avaible options:" << endl;
|
|
|
|
cmd.printParams();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
string left = cmd.get<string>("left");
|
|
|
|
string right = cmd.get<string>("right");
|
|
|
|
string intrinsic = cmd.get<string>("intrinsic");
|
|
|
|
string extrinsic = cmd.get<string>("extrinsic");
|
|
|
|
int ndisp = cmd.get<int>("ndisp");
|
|
|
|
double scale = cmd.get<double>("scale");
|
|
|
|
|
|
|
|
if (left.empty() || right.empty())
|
|
|
|
{
|
|
|
|
cout << "Missed input images" << endl;
|
|
|
|
cout << "Avaible options:" << endl;
|
|
|
|
cmd.printParams();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intrinsic.empty() ^ extrinsic.empty())
|
|
|
|
{
|
|
|
|
cout << "Boss camera parameters must be specified" << endl;
|
|
|
|
cout << "Avaible options:" << endl;
|
|
|
|
cmd.printParams();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat imgLeftColor = imread(left, IMREAD_COLOR);
|
|
|
|
Mat imgRightColor = imread(right, IMREAD_COLOR);
|
|
|
|
|
|
|
|
if (imgLeftColor.empty())
|
|
|
|
{
|
|
|
|
cout << "Can't load image " << left << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imgRightColor.empty())
|
|
|
|
{
|
|
|
|
cout << "Can't load image " << right << endl;
|
|
|
|
return -1;
|
2011-11-28 16:23:26 +08:00
|
|
|
}
|
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
Mat Q = Mat::eye(4, 4, CV_32F);
|
|
|
|
if (!intrinsic.empty() && !extrinsic.empty())
|
|
|
|
{
|
|
|
|
FileStorage fs;
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
// reading intrinsic parameters
|
|
|
|
fs.open(intrinsic, CV_STORAGE_READ);
|
|
|
|
if (!fs.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Failed to open file " << intrinsic << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
Mat M1, D1, M2, D2;
|
|
|
|
fs["M1"] >> M1;
|
|
|
|
fs["D1"] >> D1;
|
|
|
|
fs["M2"] >> M2;
|
|
|
|
fs["D2"] >> D2;
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
// reading extrinsic parameters
|
|
|
|
fs.open(extrinsic, CV_STORAGE_READ);
|
|
|
|
if (!fs.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Failed to open file " << extrinsic << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
Mat R, T, R1, P1, R2, P2;
|
|
|
|
fs["R"] >> R;
|
|
|
|
fs["T"] >> T;
|
|
|
|
|
|
|
|
Size img_size = imgLeftColor.size();
|
|
|
|
|
|
|
|
Rect roi1, roi2;
|
|
|
|
stereoRectify(M1, D1, M2, D2, img_size, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, -1, img_size, &roi1, &roi2);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
Mat map11, map12, map21, map22;
|
|
|
|
initUndistortRectifyMap(M1, D1, R1, P1, img_size, CV_16SC2, map11, map12);
|
|
|
|
initUndistortRectifyMap(M2, D2, R2, P2, img_size, CV_16SC2, map21, map22);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
Mat img1r, img2r;
|
|
|
|
remap(imgLeftColor, img1r, map11, map12, INTER_LINEAR);
|
|
|
|
remap(imgRightColor, img2r, map21, map22, INTER_LINEAR);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-23 18:05:24 +08:00
|
|
|
imgLeftColor = img1r(roi1);
|
|
|
|
imgRightColor = img2r(roi2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat imgLeftGray, imgRightGray;
|
|
|
|
cvtColor(imgLeftColor, imgLeftGray, COLOR_BGR2GRAY);
|
|
|
|
cvtColor(imgRightColor, imgRightGray, COLOR_BGR2GRAY);
|
|
|
|
|
|
|
|
cvtColor(imgLeftColor, imgLeftColor, COLOR_BGR2RGB);
|
|
|
|
|
|
|
|
Mat disp, points;
|
|
|
|
|
|
|
|
StereoBM bm(0, ndisp);
|
|
|
|
|
|
|
|
bm(imgLeftGray, imgRightGray, disp);
|
|
|
|
disp.convertTo(disp, CV_8U, 1.0 / 16.0);
|
|
|
|
|
|
|
|
disp = disp(Range(21, disp.rows - 21), Range(ndisp, disp.cols - 21)).clone();
|
|
|
|
imgLeftColor = imgLeftColor(Range(21, imgLeftColor.rows - 21), Range(ndisp, imgLeftColor.cols - 21)).clone();
|
|
|
|
|
|
|
|
reprojectImageTo3D(disp, points, Q);
|
|
|
|
|
2011-11-28 19:50:46 +08:00
|
|
|
const string windowName = "OpenGL Sample";
|
|
|
|
|
|
|
|
namedWindow(windowName, WINDOW_OPENGL);
|
2011-11-30 14:20:29 +08:00
|
|
|
resizeWindow(windowName, 400, 400);
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
PointCloudRenderer renderer(points, imgLeftColor, scale);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
createTrackbar("Fov", windowName, &renderer.fov_, 100);
|
|
|
|
setMouseCallback(windowName, mouseCallback, &renderer);
|
|
|
|
setOpenGlDrawCallback(windowName, openGlDrawCallback, &renderer);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2012-03-17 05:21:04 +08:00
|
|
|
for(;;)
|
2011-11-30 14:20:29 +08:00
|
|
|
{
|
2011-12-13 14:25:37 +08:00
|
|
|
int key = waitKey(10);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
if (key >= 0)
|
|
|
|
key = key & 0xff;
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
if (key == 27)
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
double aspect = getWindowProperty(windowName, WND_PROP_ASPECT_RATIO);
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
key = tolower(key);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
renderer.update(key, aspect);
|
|
|
|
|
|
|
|
updateWindow(windowName);
|
|
|
|
}
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2011-11-28 19:50:46 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
const int PointCloudRenderer::step_ = 20;
|
|
|
|
|
|
|
|
PointCloudRenderer::PointCloudRenderer(const Mat& points, const Mat& img, double scale)
|
|
|
|
{
|
|
|
|
mouse_dx_ = 0;
|
|
|
|
mouse_dy_ = 0;
|
|
|
|
|
|
|
|
fov_ = 0;
|
|
|
|
yaw_ = 0.0;
|
|
|
|
pitch_ = 0.0;
|
|
|
|
|
|
|
|
frame_ = 0;
|
|
|
|
|
|
|
|
camera_.setScale(Point3d(scale, scale, scale));
|
|
|
|
|
|
|
|
pointCloud_.setVertexArray(points);
|
|
|
|
pointCloud_.setColorArray(img, false);
|
|
|
|
|
|
|
|
tm_.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int clamp(int val, int minVal, int maxVal)
|
|
|
|
{
|
|
|
|
return max(min(val, maxVal), minVal);
|
|
|
|
}
|
|
|
|
|
2012-02-22 18:00:53 +08:00
|
|
|
void PointCloudRenderer::onMouseEvent(int event, int x, int y, int /*flags*/)
|
2011-11-30 14:20:29 +08:00
|
|
|
{
|
|
|
|
static int oldx = x;
|
|
|
|
static int oldy = y;
|
|
|
|
static bool moving = false;
|
|
|
|
|
|
|
|
if (event == EVENT_LBUTTONDOWN)
|
2011-11-23 18:05:24 +08:00
|
|
|
{
|
2011-11-30 14:20:29 +08:00
|
|
|
oldx = x;
|
|
|
|
oldy = y;
|
|
|
|
moving = true;
|
|
|
|
}
|
|
|
|
else if (event == EVENT_LBUTTONUP)
|
|
|
|
{
|
|
|
|
moving = false;
|
|
|
|
}
|
2011-11-28 19:50:46 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
if (moving)
|
|
|
|
{
|
|
|
|
mouse_dx_ = oldx - x;
|
|
|
|
mouse_dy_ = oldy - y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mouse_dx_ = 0;
|
|
|
|
mouse_dy_ = 0;
|
|
|
|
}
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
const int mouseClamp = 300;
|
|
|
|
mouse_dx_ = clamp(mouse_dx_, -mouseClamp, mouseClamp);
|
|
|
|
mouse_dy_ = clamp(mouse_dy_, -mouseClamp, mouseClamp);
|
|
|
|
}
|
2011-11-28 16:23:26 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static Point3d rotate(Point3d v, double yaw, double pitch)
|
2011-11-30 14:20:29 +08:00
|
|
|
{
|
|
|
|
Point3d t1;
|
|
|
|
t1.x = v.x * cos(-yaw / 180.0 * CV_PI) - v.z * sin(-yaw / 180.0 * CV_PI);
|
|
|
|
t1.y = v.y;
|
|
|
|
t1.z = v.x * sin(-yaw / 180.0 * CV_PI) + v.z * cos(-yaw / 180.0 * CV_PI);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
Point3d t2;
|
|
|
|
t2.x = t1.x;
|
|
|
|
t2.y = t1.y * cos(pitch / 180.0 * CV_PI) - t1.z * sin(pitch / 180.0 * CV_PI);
|
|
|
|
t2.z = t1.y * sin(pitch / 180.0 * CV_PI) + t1.z * cos(pitch / 180.0 * CV_PI);
|
|
|
|
|
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PointCloudRenderer::update(int key, double aspect)
|
|
|
|
{
|
|
|
|
const Point3d dirVec(0.0, 0.0, -1.0);
|
|
|
|
const Point3d upVec(0.0, 1.0, 0.0);
|
|
|
|
const Point3d leftVec(-1.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
const double posStep = 0.1;
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-12-12 21:59:34 +08:00
|
|
|
const double mouseStep = 0.001;
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
camera_.setPerspectiveProjection(30.0 + fov_ / 100.0 * 40.0, aspect, 0.1, 1000.0);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
yaw_ += mouse_dx_ * mouseStep;
|
|
|
|
pitch_ += mouse_dy_ * mouseStep;
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
if (key == 'w')
|
|
|
|
pos_ += posStep * rotate(dirVec, yaw_, pitch_);
|
|
|
|
else if (key == 's')
|
|
|
|
pos_ -= posStep * rotate(dirVec, yaw_, pitch_);
|
|
|
|
else if (key == 'a')
|
|
|
|
pos_ += posStep * rotate(leftVec, yaw_, pitch_);
|
|
|
|
else if (key == 'd')
|
|
|
|
pos_ -= posStep * rotate(leftVec, yaw_, pitch_);
|
|
|
|
else if (key == 'q')
|
|
|
|
pos_ += posStep * rotate(upVec, yaw_, pitch_);
|
|
|
|
else if (key == 'e')
|
|
|
|
pos_ -= posStep * rotate(upVec, yaw_, pitch_);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
camera_.setCameraPos(pos_, yaw_, pitch_, 0.0);
|
2011-11-23 18:05:24 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
tm_.stop();
|
|
|
|
|
|
|
|
if (frame_++ >= step_)
|
|
|
|
{
|
|
|
|
ostringstream ostr;
|
|
|
|
ostr << "FPS: " << step_ / tm_.getTimeSec();
|
|
|
|
fps_ = ostr.str();
|
2011-12-13 14:25:37 +08:00
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
frame_ = 0;
|
|
|
|
tm_.reset();
|
2011-11-23 18:05:24 +08:00
|
|
|
}
|
|
|
|
|
2011-11-30 14:20:29 +08:00
|
|
|
tm_.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PointCloudRenderer::draw()
|
|
|
|
{
|
|
|
|
camera_.setupProjectionMatrix();
|
|
|
|
camera_.setupModelViewMatrix();
|
|
|
|
|
|
|
|
render(pointCloud_);
|
|
|
|
|
|
|
|
render(fps_, GlFont::get("Courier New", 16), Scalar::all(255), Point2d(3.0, 0.0));
|
2011-11-23 18:05:24 +08:00
|
|
|
}
|