Home | History | Annotate | Download | only in xcore
      1 /*
      2  * image_projector.h - Calculate 2D image projective matrix
      3  *
      4  *  Copyright (c) 2017 Intel Corporation
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  *
     18  * Author: Zong Wei <wei.zong (at) intel.com>
     19  */
     20 
     21 #ifndef XCAM_IMAGE_PROJECTIVE_2D_H
     22 #define XCAM_IMAGE_PROJECTIVE_2D_H
     23 
     24 #include <xcam_std.h>
     25 #include <meta_data.h>
     26 #include <vec_mat.h>
     27 #include <vector>
     28 
     29 namespace XCam {
     30 
     31 struct CalibrationParams {
     32     double focal_x;  //Focal length, x axis, in pixels
     33     double focal_y;  //Focal length, y axis, in pixels
     34     double offset_x;  //Principal point x coordinate on the image, in pixels
     35     double offset_y;  //Principal point y coordinate on the image, in pixels
     36     double skew; //in case if the image coordinate axes u and v are not orthogonal to each other
     37     double readout_time;
     38     double gyro_delay;
     39     Vec4d gyro_drift;
     40 
     41     CalibrationParams ()
     42         : focal_x (0)
     43         , focal_y (0)
     44         , offset_x (0)
     45         , offset_y (0)
     46         , skew (0)
     47         , readout_time (0)
     48         , gyro_delay (0)
     49     {
     50         gyro_drift.zeros();
     51     }
     52 };
     53 
     54 enum CoordinateAxisType {
     55     AXIS_X = 0,
     56     AXIS_MINUS_X,
     57     AXIS_Y,
     58     AXIS_MINUS_Y,
     59     AXIS_Z,
     60     AXIS_MINUS_Z,
     61     AXIS_NONE,
     62 };
     63 
     64 struct CoordinateSystemConv {
     65     CoordinateAxisType axis_to_x;
     66     CoordinateAxisType axis_to_y;
     67     CoordinateAxisType axis_mirror;
     68 
     69     CoordinateSystemConv ()
     70     {
     71         axis_to_x = AXIS_X;
     72         axis_to_y = AXIS_Y;
     73         axis_mirror = AXIS_NONE;
     74     }
     75 
     76     CoordinateSystemConv (
     77         CoordinateAxisType to_x,
     78         CoordinateAxisType to_y,
     79         CoordinateAxisType mirror)
     80     {
     81         axis_to_x = to_x;
     82         axis_to_y = to_y;
     83         axis_mirror = mirror;
     84     }
     85 };
     86 
     87 class ImageProjector
     88 {
     89 public:
     90     explicit ImageProjector () {};
     91     explicit ImageProjector (CalibrationParams &params);
     92     explicit ImageProjector (
     93         double focal_x,
     94         double focal_y,
     95         double offset_x,
     96         double offset_y,
     97         double skew);
     98 
     99     virtual ~ImageProjector () {};
    100 
    101     XCamReturn set_sensor_calibration (CalibrationParams &params);
    102     XCamReturn set_camera_intrinsics (
    103         double focal_x,
    104         double focal_y,
    105         double offset_x,
    106         double offset_y,
    107         double skew);
    108 
    109     Mat3d get_camera_intrinsics () {
    110         return _intrinsics;
    111     }
    112 
    113     Mat3d calc_camera_extrinsics (
    114         const int64_t frame_ts,
    115         const std::vector<int64_t> &pose_ts,
    116         const std::vector<Vec4d> &orientation,
    117         const std::vector<Vec3d> &translation);
    118 
    119     Mat3d calc_camera_extrinsics (
    120         const int64_t frame_ts,
    121         DevicePoseList &pose_list);
    122 
    123     Mat3d calc_projective (
    124         Mat3d &extrinsic0,
    125         Mat3d &extrinsic1);
    126 
    127     Mat3d align_coordinate_system (
    128         CoordinateSystemConv &world_to_device,
    129         Mat3d &extrinsics,
    130         CoordinateSystemConv &device_to_image);
    131 
    132 protected:
    133     Quaternd interp_orientation (
    134         int64_t ts,
    135         const std::vector<Vec4d> &orientation,
    136         const std::vector<int64_t> &orient_ts,
    137         int& index);
    138 
    139     Mat3d rotate_coordinate_system (
    140         CoordinateAxisType axis_to_x,
    141         CoordinateAxisType axis_to_y);
    142 
    143     Mat3d mirror_coordinate_system (CoordinateAxisType axis_mirror);
    144 
    145     Mat3d transform_coordinate_system (CoordinateSystemConv &transform);
    146 
    147 private:
    148     XCAM_DEAD_COPY (ImageProjector);
    149 
    150 private:
    151     Mat3d             _intrinsics;
    152     CalibrationParams _calib_params;
    153 };
    154 
    155 }
    156 
    157 #endif //XCAM_IMAGE_PROJECTIVE_2D_H