Go to the documentation of this file.00001
00012 #include "tuw_utils/camera.h"
00013
00014 namespace tuw {
00015
00016
00017 void Camera::distort(const std::vector<cv::Point2f> &src, std::vector<cv::Point2f> &des) {
00018 const double &srcFx = projectionMatrix(0,0);
00019 const double &srcFy = projectionMatrix(1,1);
00020 const double &srcCx = projectionMatrix(0,2);
00021 const double &srcCy = projectionMatrix(1,2);
00022 const double &k1 = distCoeffs(0,0);
00023 const double &k2 = distCoeffs(0,1);
00024 const double &p1 = distCoeffs(0,2);
00025 const double &p2 = distCoeffs(0,3);
00026 const double &k3 = distCoeffs(0,4);
00027 const double &desFx = cameraMatrix(0,0);
00028 const double &desFy = cameraMatrix(1,1);
00029 const double &desCx = cameraMatrix(0,2);
00030 const double &desCy = cameraMatrix(1,2);
00031 des.resize(src.size());
00032 double x, y, x1, y1, r2, r4, r6, a1, a2, a3, cdist;
00033 for(unsigned int i = 0; i < src.size(); i++) {
00034
00035 x = (src[i].x - srcCx) / srcFx;
00036 y = (src[i].y - srcCy) / srcFy;
00037 r2 = x*x + y*y;
00038 r4 = r2*r2;
00039 r6 = r4*r2;
00040 a1 = 2*x*y;
00041 a2 = r2 + 2*x*x;
00042 a3 = r2 + 2*y*y;
00043 cdist = 1+k1*r2+k2*r4+k3*r6;
00044 x1 = (x*cdist + p1*a1 + p2*a2);
00045 y1 = (y*cdist + p1*a3 + p2*a1);
00046
00047 des[i].x = x1 * desFx + desCx;
00048 des[i].y = y1 * desFy + desCy;
00049 }
00050 }
00051
00052 void Camera::distort(const cv::RotatedRect &src, cv::RotatedRect &des) {
00053
00054 std::vector<cv::Point2f> vtx(5);
00055 src.points(&vtx[0]);
00056 vtx[4] = src.center;
00057 std::vector<cv::Point2f> vtxDistort;
00058 distort(vtx, vtxDistort);
00059 des.size = cv::Size2f((cv::norm(vtx[2] - vtx[1])+cv::norm(vtx[0] - vtx[3]))/2., (cv::norm(vtx[1] - vtx[0])+cv::norm(vtx[3] - vtx[2]))/2.);
00060 des.angle = atan2(vtx[2].y - vtx[1].y, vtx[2].x - vtx[1].x) * 180.0/M_PI;
00061 des.center = vtxDistort[4];
00062
00063 }
00064
00065
00066 }