Go to the documentation of this file.00001
00002
00003
00004 #include <ucl_drone/map/projection_2D.h>
00005
00006
00007
00008
00009 void projection2D_utils1(double h, const ucl_drone::Pose3D pose, cv::Mat &T, cv::Mat &n_cam)
00010 {
00011
00012 double yaw = -pose.rotZ;
00013 double pitch = -pose.rotY;
00014 double roll = -pose.rotX;
00015
00016
00017 cv::Mat world2drone = rollPitchYawToRotationMatrix(roll, pitch, yaw);
00018
00019 cv::Mat drone2cam = rollPitchYawToRotationMatrix(PI, 0, -PI / 2);
00020
00021 T = drone2cam * world2drone;
00022
00023
00024 cv::Mat n_world = (cv::Mat_< double >(3, 1) << 0.0, 0.0, 1.0);
00025
00026 n_cam = T * n_world;
00027 }
00028
00029
00030 void projection2D_utils2(double pixelx, double pixely, double h, const cv::Mat &T,
00031 const cv::Mat &n_cam, cv::Mat &p_world)
00032 {
00033
00034 cv::Mat d = (cv::Mat_< double >(3, 1) << (pixelx - Read::img_center_x()) / Read::focal_length_x(),
00035 (pixely - Read::img_center_y()) / Read::focal_length_y(), 1);
00036
00037
00038 cv::Mat temp = n_cam.t() * d;
00039 double t = -h / (temp.at< double >(0, 0));
00040
00041 cv::Mat p_cam = d * t;
00042 p_world = T.t() * p_cam;
00043 }
00044
00045 #ifndef PROJECTION_2D_2015
00046
00049 void projection_2D(std::vector< cv::Point2f > &points_in, ucl_drone::Pose3D &pose,
00050 std::vector< cv::Point3f > &points_out, bool h_flag )
00051 {
00052 cv::Mat T;
00053 cv::Mat n_cam;
00054 double h = pose.z;
00055 if (h_flag)
00056 {
00057 h = 0.73;
00058 }
00059 projection2D_utils1(h, pose, T, n_cam);
00060 points_out.resize(points_in.size());
00061 for (int i = 0; i < points_in.size(); i++)
00062 {
00063 cv::Mat p_world;
00064 projection2D_utils2(points_in[i].x, points_in[i].y, h, T, n_cam, p_world);
00065 points_out[i] = cv::Point3f((float)(p_world.at< double >(0, 0) + pose.x),
00066 (float)(p_world.at< double >(1, 0) + pose.y),
00067 (float)(p_world.at< double >(2, 0) + h));
00068
00069
00070
00071 }
00072 }
00073
00074 #endif
00075
00076 #ifdef PROJECTION_2D_2015
00077
00078 #define MAP_WIDTH 640
00079 #define MAP_HEIGHT 360
00080
00081 #define FOV_X 0.2575
00082 #define FOV_Y 0.465
00083
00084
00087 void projection_2D(std::vector< cv::Point2f > &points_in, ucl_drone::Pose3D &pose,
00088 std::vector< cv::Point3f > &points_out, bool h_flag )
00089
00090
00091
00092 {
00093
00094
00095 double altitude = 1;
00096
00097 double theta = -(double)pose.rotY / 1000 / 180 * PI;
00098 double phi = (double)pose.rotX / 1000 / 180 * PI;
00099 double theta_z = (double)pose.rotZ / 1000 / 180 * PI;
00100 double theta_x = cos(theta_z) * theta - sin(theta_z) * phi;
00101 double theta_y = -sin(theta_z) * theta - cos(theta_z) * phi;
00102
00103
00104
00105
00106 double plane_dist = altitude / cos(theta_x) / cos(theta_y);
00107
00108 points_out.resize(points_in.size());
00109
00110 for (int i = 0; i < points_in.size(); i++)
00111 {
00112 double pixelx = points_in[i].x;
00113 double pixely = points_in[i].y;
00114
00115
00116
00117
00118
00119
00120
00121 double x = plane_dist *
00122 sin(((double)pixelx - (double)MAP_WIDTH / 2) / ((double)MAP_WIDTH / 2) * FOV_Y);
00123 double y = plane_dist *
00124 sin(-((double)pixely - (double)MAP_HEIGHT / 2) / ((double)MAP_HEIGHT / 2) * FOV_X);
00125 double z = 1 / cos(theta_x) / cos(theta_y);
00126
00127
00128
00129
00130
00131
00132
00133
00134 double dx = cos(theta_y) * (sin(theta_z) * y + cos(theta_z) * x) - sin(theta_y) * z;
00135 double dy =
00136 sin(theta_x) * (cos(theta_y) * z + sin(theta_y) * (sin(theta_z) * y + cos(theta_z) * x)) +
00137 cos(theta_x) * (cos(theta_z) * y - sin(theta_z) * x);
00138 double dz =
00139 cos(theta_x) * (cos(theta_y) * z + sin(theta_y) * (sin(theta_z) * y + cos(theta_z) * x)) -
00140 sin(theta_x) * (cos(theta_z) * y - sin(theta_z) * x);
00141
00142
00143
00144 cv::Point3f point;
00145 point.x = pose.x + dx / dz;
00146 point.y = pose.y + dy / dz;
00147 point.z = 0;
00148 points_out[i] = point;
00149 }
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 void convert_imgpoint_to_mappoint(int pixelx, int pixely, double *output_mapx, double *output_mapy,
00162 navdata_struct navdata)
00163 {
00164 double altitude = 1;
00165
00166 double theta =
00167 (double)navdata.Theta / 1000 / 180 * PI;
00168 double phi = -(double)navdata.Phi / 1000 / 180 * PI;
00169 double theta_z = (double)navdata.Psi / 1000 / 180 * PI;
00170 double theta_x = cos(theta_z) * theta - sin(theta_z) * phi;
00171 double theta_y = -sin(theta_z) * theta - cos(theta_z) * phi;
00172
00173
00174
00175 double plane_dist = altitude / cos(theta_x) / cos(theta_y);
00176
00177
00178
00179
00180 double x =
00181 plane_dist * sin(((double)pixelx - (double)MAP_WIDTH / 2) / ((double)MAP_WIDTH / 2) * FOV_Y);
00182 double y = plane_dist *
00183 sin(-((double)pixely - (double)MAP_HEIGHT / 2) / ((double)MAP_HEIGHT / 2) * FOV_X);
00184 double z = 1 / cos(theta_x) / cos(theta_y);
00185
00186
00187
00188
00189
00190
00191
00192 double dx = cos(theta_y) * (sin(theta_z) * y + cos(theta_z) * x) - sin(theta_y) * z;
00193 double dy =
00194 sin(theta_x) * (cos(theta_y) * z + sin(theta_y) * (sin(theta_z) * y + cos(theta_z) * x)) +
00195 cos(theta_x) * (cos(theta_z) * y - sin(theta_z) * x);
00196 double dz =
00197 cos(theta_x) * (cos(theta_y) * z + sin(theta_y) * (sin(theta_z) * y + cos(theta_z) * x)) -
00198 sin(theta_x) * (cos(theta_z) * y - sin(theta_z) * x);
00199
00200 *output_mapx = navdata.posx + dx / dz;
00201 *output_mapy = navdata.posy + dy / dz;
00202 }
00203
00204 #endif