frame_helper
FrameHelper.h
Go to the documentation of this file.
1 #ifndef FRAMEHELPER_H
2 #define FRAMEHELPER_H
3 
4 // Workaround for GCC 4.6
5 #include <stddef.h>
6 #include <base/samples/Frame.hpp>
7 #include <opencv2/core/core.hpp>
8 #include "FrameHelperTypes.h"
9 #include "CalibrationCv.h"
10 //#include <base/samples/compressed_frame.h>
11 
12 //TODO
13 //at the moment some functions call copyImageIndependantAttributes
14 //automatically
15 //this should be replaced by a flag
16 
17 namespace frame_helper
18 {
20  {
21  private:
22  //the buffer are used to convert one frame to an other one
23  //if src and dst attributes are not changing no memory allocation
24  //is done
25  //DO NOT USE THIS VARIABLES INSIGHT any function other than convert
26  base::samples::frame::Frame frame_buffer;
27  base::samples::frame::Frame frame_buffer2;
28 
29  //used insight convertColor
30  base::samples::frame::Frame frame_buffer3;
31 
32  //mapping matrix for undistort
33  //if the size of the image is not changing
34  //the mapping is calculated only once
35  CameraCalibrationCv calibration;
36 
37  public:
38  FrameHelper();
39 
40  //converts one frame to another frame
41  //this can be used to convert colors resize and undistort frames
42  //the attributes are implied by the dst attributes
43  //call setCalibrationParameters to set the calibration parameters
44  //this is none static because an internal buffer is used which is not be resized if the
45  //src and dst have always the same attributes
46  void convert(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst,
47  int offset_x = 0, int offset_y = 0, ResizeAlgorithm algo = INTER_LINEAR, bool bundistort=false);
48 
49  //sets the calibration paramter for the camera
50  //parameter. Be aware that this does not set the R and P parameter
51  //for stereo undistort. Use the method that takes a cameraCalibrationCv
52  //instead.
53  //para intrinsic and distortion parameters
55 
56  // sets the calibration parameters for the camera
57  // which uses R and P matrices for stereo calibration as well
59 
60  //this is a convenience function to undistort a frame
61  //call setCalibrationParameter first
62  //see cv::remap
63  void undistort(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst);
64 
65 
66  //copies a cv::Mat into a Frame
67  //frame is initialized if it has not got the right size
68  static void copyMatToFrame(const cv::Mat &src, base::samples::frame::Frame &frame);
69 
70  //resizes a bayer image without converting it to another color format by skipping pixels
71  //The size of src must be a multiple of two times dst size
72  //otherwise an exception will be thrown
73  static void resize_bayer(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst,
74  int offset_x = 0, int offset_y = 0);
75 
76  //resize a frame
77  //the size is implied by the dst frame
78  static void resize(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst,
79  int offset_x = 0, int offset_y = 0,ResizeAlgorithm algo = INTER_LINEAR);
80 
81  //rotates a frame by 180 degrees
82  //dst is inititialized as a copy of src with rotated image
83  static void rotateBy180Degrees(const base::samples::frame::Frame &src,
84  base::samples::frame::Frame &dst);
85 
86 
87  //calculates the distance in meters to an object seen on an image
88  //parameters:
89  //f focal length of the camera in pixels which has taken the image
90  // set f = fx if the width of the object is used as size
91  // set f = fy if the height of the object is used as size
92  //virtual_size size of the object in pixels
93  //real_size size of the object in meters
94  static float calcDistanceToObject(float f,float virtual_size,float real_size);
95 
96 
97  //calculates the distance in meters to an object seen on an image
98  //parameters:
99  //frame frame which must have the additional attributes fx (focal length) and fy
100  //virtual_width width of the object in pixels
101  //real_width width of the object in meters
102  //virtual_height height of the object in pixels
103  //real_height height of the object in meters
104  //you can set width or height to zero if you do not want to use it
105  //internal always the biggest dimension is used to calculate the distance
106  static float calcDistanceToObject(const base::samples::frame::Frame &frame,
107  float virtual_width,float real_width,
108  float virtual_height,float real_height);
109 
110  //calculates the relative positions in meters between to image points
111  //parameters:
112  //fx focal length of the camera
113  //fy focal length of the camera
114  //x1 x coordinate of the first point in pixels
115  //y1 y coordinate of the first point in pixels
116  //x2 x coordinate of the second point in pixels
117  //y2 y coordinate of the second point in pixels
118  //d distance between the camera and the two points
119  static cv::Point2f calcRelPosToPoint(float fx,float fy, int x1,int y1,int x2,int y2, float d);
120  static cv::Point2f calcRelPosToPoint(const base::samples::frame::Frame &frame,
121  int x1,int y1,int x2,int y2, float d);
122  static cv::Point2f calcRelPosToCenter(const base::samples::frame::Frame &frame,int x1,int y1,float d);
123 
124  //converts src to dst
125  //the mode of dst implies the conversion
126  void convertColor(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst);
127 
128  //converts a bayer pattern image to rgb image which has 8 bit data depth for each channel
129  static void convertBayerToRGB24(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst);
130  static void convertBayerToRGB24(const uint8_t *src, uint8_t *dst, int width, int height ,base::samples::frame::frame_mode_t mode);
131 
132  //converts a rgb image to gray scale image
133  static void convertRGBToGray(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst,bool copy_attributes =true);
134 
135  //copies only the green channel of a bayer pattern image to dst
136  //dst has to be MODE_GRAYSCALE
137  static void convertBayerToGreenChannel(const base::samples::frame::Frame &src,base::samples::frame::Frame &dst);
138  static void convertBayerToGreenChannel(const uint8_t *src, uint8_t *dst, int width, int height, base::samples::frame::frame_mode_t mode);
139 
140  //calculates the diff between src1 and src2
141  //dst[i] = |src1[i]-src2[i]|
142  //only 8 Bit data depth is supported
143  static void calcDiff(const base::samples::frame::Frame &src1,const base::samples::frame::Frame &src2,base::samples::frame::Frame &dst);
144 
148  static void convertPJPGToRGB24(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst);
149  static bool convertPJPGToRGB24(const uint8_t *source, uint8_t *target_buffer,const size_t source_size, const int width, const int height);
150 
151  // JPEG to RGB24 conversion using JpegConversion.
152  static void convertJPEGToRGB24(uint8_t const* src, uint8_t* dst, size_t const src_size, int const width, int const height);
153 
154  static cv::Mat convertToCvMat(const base::samples::frame::Frame &frame);
155 
156  static int getOpenCvType(const base::samples::frame::Frame &frame);
157 
158  void saveFrame(const std::string &filename,base::samples::frame::Frame const &frame);
159  void loadFrame(const std::string &filename,base::samples::frame::Frame &frame);
160  };
161 };
162 #endif // FRAMEHELPER_H
void convertColor(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:355
void undistort(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:218
static void calcDiff(const base::samples::frame::Frame &src1, const base::samples::frame::Frame &src2, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:953
Definition: Calibration.h:18
Definition: FrameHelperTypes.h:8
static void convertPJPGToRGB24(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:1058
ResizeAlgorithm
Definition: FrameHelperTypes.h:6
static cv::Point2f calcRelPosToPoint(float fx, float fy, int x1, int y1, int x2, int y2, float d)
Definition: FrameHelper.cpp:811
static void resize_bayer(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst, int offset_x=0, int offset_y=0)
Definition: FrameHelper.cpp:301
FrameHelper()
Definition: FrameHelper.cpp:23
static void convertBayerToGreenChannel(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:980
void setCalibrationParameter(const CameraCalibration &para)
Definition: FrameHelper.cpp:207
void convert(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst, int offset_x=0, int offset_y=0, ResizeAlgorithm algo=INTER_LINEAR, bool bundistort=false)
Definition: FrameHelper.cpp:132
static cv::Mat convertToCvMat(const base::samples::frame::Frame &frame)
Definition: FrameHelper.cpp:66
Definition: FrameHelper.h:19
static void convertBayerToRGB24(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:865
static void convertJPEGToRGB24(uint8_t const *src, uint8_t *dst, size_t const src_size, int const width, int const height)
Definition: FrameHelper.cpp:1102
static void copyMatToFrame(const cv::Mat &src, base::samples::frame::Frame &frame)
Definition: FrameHelper.cpp:1073
void loadFrame(const std::string &filename, base::samples::frame::Frame &frame)
Definition: FrameHelper.cpp:1131
static void resize(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst, int offset_x=0, int offset_y=0, ResizeAlgorithm algo=INTER_LINEAR)
Definition: FrameHelper.cpp:257
Definition: Calibration.h:10
static cv::Point2f calcRelPosToCenter(const base::samples::frame::Frame &frame, int x1, int y1, float d)
Definition: FrameHelper.cpp:839
static int getOpenCvType(const base::samples::frame::Frame &frame)
Definition: FrameHelper.cpp:28
void saveFrame(const std::string &filename, base::samples::frame::Frame const &frame)
Definition: FrameHelper.cpp:1122
static float calcDistanceToObject(float f, float virtual_size, float real_size)
Definition: FrameHelper.cpp:821
static void rotateBy180Degrees(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst)
Definition: FrameHelper.cpp:1110
Definition: CalibrationCv.h:18
static void convertRGBToGray(const base::samples::frame::Frame &src, base::samples::frame::Frame &dst, bool copy_attributes=true)
Definition: FrameHelper.cpp:757