The question is not really clear. What do you mean by "convert" a point cloud to a 2d image?
I will assume that by "convert" you mean project.
In opencv a point cloud, or any 3d point for that matter can be projected onto a 2d image using projectpoints
: http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#projectpoints.
This is based on the pinhole camera model, take a look here for example:
http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/FUSIELLO4/tutorial.html
You can alsot take a look at this question:
OpenCV's projectPoints function
Bare in mind that you will not be able to reconstruct you original 3d data (since the depth info was lost in the projection process)
To simplify things, we can use a "perfect" projection model (no camera lens distortions) with an arbitrary focal length (if you wish to display the image, you need to play around with the focal length according to your data, such that the projected points' values are in not too high, for example not above 2048, which is the width of a 2k resolution image).
Here's an example:
#include <string>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
std::vector<cv::Point3d> Generate3DPoints();
int main(int argc, char* argv[])
{
// Read 3D points
std::vector<cv::Point3d> objectPoints = Generate3DPoints();
std::vector<cv::Point2d> imagePoints;
int f = 5; //focal length
for (unsigned int i = 0; i < objectPoints.size(); i++)
{
cv::Point3d orig_point = objectPoints[i];
imagePoints.push_back(cv::Point2d(
f*orig_point.x / orig_point.z, //x' = f*x/z
f*orig_point.y / orig_point.z) //y' = f*y/z
);
}
}
std::vector<cv::Point3d> Generate3DPoints()
{
std::vector<cv::Point3d> points;
double x, y, z;
x = .5; y = .5; z = -.5;
points.push_back(cv::Point3d(x, y, z));
x = .5; y = .5; z = .5;
points.push_back(cv::Point3d(x, y, z));
x = -.5; y = .5; z = .5;
points.push_back(cv::Point3d(x, y, z));
x = -.5; y = .5; z = -.5;
points.push_back(cv::Point3d(x, y, z));
x = .5; y = -.5; z = -.5;
points.push_back(cv::Point3d(x, y, z));
x = -.5; y = -.5; z = -.5;
points.push_back(cv::Point3d(x, y, z));
x = -.5; y = -.5; z = .5;
points.push_back(cv::Point3d(x, y, z));
for (unsigned int i = 0; i < points.size(); ++i)
{
std::cout << points[i] << std::endl << std::endl;
}
return points;
}