void virtualCameraSimulator(int argc, char ** argv)
{
int numberOfPixelInHeight,numberOfPixelInWidth;
double heightOfSensor, widthOfSensor;
double focalLength=0.5;
double mx, my, U0, V0;
numberOfPixelInHeight=600;
numberOfPixelInWidth=600;
heightOfSensor=10;
widthOfSensor=10;
my=(numberOfPixelInHeight)/heightOfSensor ;
U0=(numberOfPixelInHeight)/2 ;
mx=(numberOfPixelInWidth)/widthOfSensor;
V0=(numberOfPixelInWidth)/2;
cv::Mat cameraMatrix = (cv::Mat_<double>(3,3) <<
focalLength*mx, 0, V0,
0,focalLength*my,U0,
0,0,1);
cv::Mat distortionCoefficient= (cv::Mat_<double>(5,1) <<0, 0, 0, 0, 0);
////////////////////////////////////////////setting up virtual camera extrinsic ///////////////////////////////////////////////////////
cv::Mat cameraRotation=cv::Mat::eye(3,3, CV_64F);
double roll,pitch,yaw;
cv::Vec3d theta;
roll=0 ;
pitch=0;
yaw=0;
theta[0]=roll;
theta[1]=pitch;
theta[2]=yaw;
cameraRotation =eulerAnglesToRotationMatrix(theta);
double tx,ty,tz;
tx=0;
ty=0;
tz=0;
cv::Mat cameraTranslation = (cv::Mat_<double>(3,1) <<tx,ty,tz);
//////////////////////////////////////////projecting 3D points from world into virtual camera //////////////////////////////
std::vector<cv::Point3d> objectpoints;
std::vector<cv::Point2d> projectedPoints;
/*
Points are in the following form (OpenCV coordinate):
Z
▲
/
/
/ 1 2 X
|------------ ⯈
1| .
2| . . .
3| .
4| .
| Y
⯆
*/
std::string pathToPointFile(argv[1]);
io::CSVReader<3> in(pathToPointFile);
in.read_header(io::ignore_extra_column, "x", "y", "z");
double x,y,z;
pcl::PointXYZ point;
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> > pointsInCameraCloud(new pcl::PointCloud<pcl::PointXYZ>);
while(in.read_row(x,y,z))
{
objectpoints.push_back(cv::Point3d(x,y,z));
pointsInCameraCloud->push_back(pcl::PointXYZ(x,y,z));
}
cv::projectPoints(objectpoints, cameraRotation, cameraTranslation, cameraMatrix, distortionCoefficient, projectedPoints);
///////////////////////////////////////////////saving the image //////////////////////////////////////////
double U,V;
cv::Mat cameraImage=cv::Mat::zeros(numberOfPixelInHeight,numberOfPixelInWidth,CV_8UC1);
cv::line(cameraImage,cv::Point2d(numberOfPixelInWidth/2,0),cv::Point2d(numberOfPixelInWidth/2,numberOfPixelInHeight),cv::Scalar(255,255,255));
cv::line(cameraImage,cv::Point2d(0,numberOfPixelInHeight/2),cv::Point2d(numberOfPixelInWidth,numberOfPixelInHeight/2),cv::Scalar(255,255,255));
for(std::size_t i=0;i<projectedPoints.size();i++)
{
V=int(projectedPoints.at(i).x);
U=int(projectedPoints.at(i).y);
//std::cout<<U <<"," <<V <<std::endl;
cameraImage.at<char>(int(U),int(V))=char(255);
}
std::string fileName=std::string("image_")+std::to_string(focalLength)+ std::string("_.jpg");
cv::imwrite(fileName,cameraImage);
/////////////////////////////////////////// ray tracing////////////////////////////////////////////
std::vector<cv::Point3d> directionRays;
reverseProjection(distortionCoefficient , cameraMatrix, projectedPoints, directionRays);
/////////////////////////////////////////// 3D visualization////////////////////////////////////////////
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
int pointSize=5;
double coordinateFrameScaleSize=1;
std::string coordinateName;
coordinateName="World";
int viewport = 0;
viewer->addCoordinateSystem(coordinateFrameScaleSize,Eigen::Affine3f::Identity(),coordinateName,viewport);
double rayScale=5;
double r,g,b;
r=0.0;
g=1.0;
b=1.0;
for(std::size_t i=0;i<directionRays.size();i++)
{
std::cout<< directionRays[i].x<<","<<directionRays[i].y<<","<<directionRays[i].z <<std::endl;
viewer->addLine(pcl::PointXYZ(0 ,0 , 0), pcl::PointXYZ(rayScale*directionRays[i].x,rayScale*directionRays[i].y,rayScale*directionRays[i].z), r, g, b, std::to_string(i+100),viewport);
}
int Red,Blue,Green;
Red=255;
Green=0;
Blue=0;
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ > tgt_h ( pointsInCameraCloud,Red,Green,Blue );
viewer->addPointCloud<pcl::PointXYZ> (pointsInCameraCloud ,tgt_h , std::to_string(1), 0);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, pointSize, std::to_string(1));
while (!viewer->wasStopped ())
{
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
}