1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
void projectFromCameraCoordinateToCameraPlane( Eigen::Matrix3Xd &pointsInCameraCoordinate, Eigen::Matrix3d &cameraIntrinsicMatrix,Eigen::Matrix2Xd &pointsInCameraPlane) { /* Intrinsic camera Matrix is something like this: focalLength ===> unit is mm mx= ==>unit is Pixel/mm U0= ===> unit is Pixel my=-(numberOfPixelInHeight)/heightOfSensor ; U0=(numberOfPixelInHeight)/2 ; mx=(numberOfPixelInWidth)/heightOfSensor; V0=(numberOfPixelInWidth)/2; Gamma=0 X,Y,Z,W are homogeneous position of point in camera coordinate ┌ ┐ ┌ ┐┌ ┐ v` |f*mx Gamma V0 0||X| u` =|0 f*my U0 0||Y| w` |0 0 1 0||Z| └ ┘ └ ┘└W┘ ▲y | ----►V | | ____________________|_____________________ | |__|__|__|__|__|__|__|__|__|__|__|__|__|__| U ▼ |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__|____________► x |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| |__|__|__|__|__|__|__|__|__|__|__|__|__|__| To project points from camera coordinate to camera plane: V`=X*f*mx + V0*Z U`=Y*f*my + U0*Z W`=Z U=U`/W` ,V=V`/W` V=f*mx*X/Z + V0 U=f*my*Y/Z + U0 (U,V) is the index of corresponding point in the image */ Eigen::Matrix3Xd pointsInCameraPlaneHomogeneous(pointsInCameraCoordinate.rows(),pointsInCameraCoordinate.cols()); pointsInCameraPlaneHomogeneous=cameraIntrinsicMatrix*pointsInCameraCoordinate; std::cout<<"pointsInCameraPlaneHomogeneous"<<std::endl; std::cout<<pointsInCameraPlaneHomogeneous<<std::endl; for(int i=0;i<pointsInCameraPlaneHomogeneous.cols();i++ ) { pointsInCameraPlaneHomogeneous(0,i)=pointsInCameraPlaneHomogeneous(0,i)/pointsInCameraPlaneHomogeneous(2,i); pointsInCameraPlaneHomogeneous(1,i)=pointsInCameraPlaneHomogeneous(1,i)/pointsInCameraPlaneHomogeneous(2,i); } pointsInCameraPlane=pointsInCameraPlaneHomogeneous.block(0,0,pointsInCameraPlaneHomogeneous.rows()-1,pointsInCameraPlaneHomogeneous.cols()); } void transformPoints(Eigen::Matrix3Xd &points, Eigen::Matrix3d &rotationMatrix, Eigen::Vector3d &translationVector,Eigen::Matrix3Xd &transformedPoints) { transformedPoints=(rotationMatrix*points).colwise() + translationVector ; } void projectFromCameraCoordinateToCameraPlane_Example() { /* OpenCV coordinate system ▲ / / Z/ / / ------------------------------► X | | | | Y | ▼ */ int numberOfPixelInHeight,numberOfPixelInWidth; double heightOfSensor, widthOfSensor; double focalLength=1.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; double L = 0.2; Eigen::Matrix3Xd controlPointsInWorldCoordinate(3,6); Eigen::Matrix3Xd controlPointsInCameraCoordinate(3,6); Eigen::Matrix3d cameraIntrinsicMatrix; cameraIntrinsicMatrix<<focalLength*mx, 0, V0, 0,focalLength*my,U0, 0,0,1; controlPointsInWorldCoordinate.col(0)= Eigen::Vector3d(-L, -L, 0); controlPointsInWorldCoordinate.col(1)= Eigen::Vector3d(2 * L, -L, 0.2); controlPointsInWorldCoordinate.col(2)= Eigen::Vector3d(L, L, 0.2); controlPointsInWorldCoordinate.col(3)= Eigen::Vector3d(-L, L, 0); controlPointsInWorldCoordinate.col(4)= Eigen::Vector3d(-2 * L, L, 0); controlPointsInWorldCoordinate.col(5)= Eigen::Vector3d(0, 0, 0.5); Eigen::Matrix3d rotationMatrix; rotationMatrix<< 1,0,0 ,0,1,0 ,0,0,1; Eigen::Vector3d translationVector(3,1); translationVector<<-0.1, 0.1, 1.2; transformPoints(controlPointsInWorldCoordinate, rotationMatrix, translationVector, controlPointsInCameraCoordinate ); Eigen::Matrix2Xd pointsInCameraPlane(2,controlPointsInCameraCoordinate.cols()); projectFromCameraCoordinateToCameraPlane(controlPointsInCameraCoordinate,cameraIntrinsicMatrix,pointsInCameraPlane); std::vector<cv::Point3d> cvPointsInCameraCoordinate; std::vector<cv::Point2d> cvpointsInCameraPlane; cvPointsInCameraCoordinate.push_back(cv::Point3d(-L, -L, 0)); cvPointsInCameraCoordinate.push_back(cv::Point3d(2 * L, -L, 0.2)); cvPointsInCameraCoordinate.push_back(cv::Point3d(L, L, 0.2)); cvPointsInCameraCoordinate.push_back(cv::Point3d(-L, L, 0)); cvPointsInCameraCoordinate.push_back(cv::Point3d(-2 * L, L, 0)); cvPointsInCameraCoordinate.push_back(cv::Point3d(0, 0, 0.5)); cv::Mat cameraMatrix= (cv::Mat_<double>(3,3) << focalLength*mx, 0, V0, 0,focalLength*my,U0, 0,0,1); cv::Mat rvec= cv::Mat::eye(3,3, CV_64F); cv::Mat tvec=(cv::Mat_<double>(3,1)<<-0.1, 0.1, 1.2); cv::projectPoints(cvPointsInCameraCoordinate,rvec,tvec,cameraMatrix,cv::noArray(),cvpointsInCameraPlane); std::cout<<"cameraIntrinsicMatrix" <<std::endl; std::cout<<cameraIntrinsicMatrix <<std::endl; std::cout<<cameraMatrix <<std::endl; Eigen::MatrixXd image(numberOfPixelInHeight,numberOfPixelInWidth); image=MatrixXd::Zero(numberOfPixelInHeight,numberOfPixelInWidth); int U,V; for(int i=0;i<pointsInCameraPlane.cols();i++) { U=int(pointsInCameraPlane(0,i)); V=int(pointsInCameraPlane(1,i)); std::cout<<U<<","<<V <<std::endl; image(U,V)=255; std::cout<<cvpointsInCameraPlane.at(i)<<std::endl; } Mat dst; eigen2cv(image, dst); std::string fileName=std::string("eigen_camera_projection_result_focal_")+std::to_string(focalLength)+ std::string("_.jpg"); cv::imwrite(fileName,dst); } |