00001
00034 #include "CameraBridge.h"
00035 #include <opencv/cv.h>
00036
00037 #include "ros/ros.h"
00038
00039
00040
00041 CameraBridge::CameraBridge():
00042 m_image_type(CameraBridge::BW),
00043 m_there_is_calibration(false),
00044 m_there_is_distortion(false)
00045 {
00046 }
00047
00048
00049
00050 CameraBridge::CameraBridge(int w, int h,
00051 float cx, float cy, float fx, float fy):
00052 m_image_type(CameraBridge::BW),
00053 m_there_is_calibration(true),
00054 m_there_is_distortion(false),
00055 m_image_width(w),
00056 m_image_height(h)
00057 {
00058 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00059 fx, 0, cx,
00060 0, fy, cy,
00061 0, 0, 1);
00062
00063 m_distortion = cv::Mat::zeros(4,1, CV_32F);
00064 }
00065
00066
00067
00068 CameraBridge::CameraBridge(int w, int h,
00069 float cx, float cy, float fx, float fy,
00070 float k1, float k2, float p1, float p2):
00071 m_image_type(CameraBridge::BW),
00072 m_there_is_calibration(true),
00073 m_there_is_distortion(true),
00074 m_image_width(w),
00075 m_image_height(h)
00076 {
00077 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00078 fx, 0, cx,
00079 0, fy, cy,
00080 0, 0, 1);
00081
00082 m_distortion = (cv::Mat_<float>(4,1) << k1, k2, p1, p2);
00083
00084 createMaps();
00085 }
00086
00087
00088
00089 CameraBridge::CameraBridge(ImageType _imageType):
00090 m_image_type(_imageType), m_there_is_calibration(false),
00091 m_there_is_distortion(false)
00092 {
00093 }
00094
00095
00096
00097 CameraBridge::CameraBridge(ImageType _imageType,
00098 int w, int h,
00099 float cx, float cy, float fx, float fy):
00100 m_image_type(_imageType),
00101 m_there_is_calibration(true),
00102 m_there_is_distortion(false),
00103 m_image_width(w),
00104 m_image_height(h)
00105 {
00106 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00107 fx, 0, cx,
00108 0, fy, cy,
00109 0, 0, 1);
00110
00111 m_distortion = cv::Mat::zeros(4,1, CV_32F);
00112 }
00113
00114
00115
00116 CameraBridge::CameraBridge(ImageType _imageType,
00117 int w, int h,
00118 float cx, float cy, float fx, float fy,
00119 float k1, float k2, float p1, float p2):
00120 m_image_type(_imageType),
00121 m_there_is_calibration(true),
00122 m_there_is_distortion(true),
00123 m_image_width(w),
00124 m_image_height(h)
00125 {
00126 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00127 fx, 0, cx,
00128 0, fy, cy,
00129 0, 0, 1);
00130
00131 m_distortion = (cv::Mat_<float>(4,1) << k1, k2, p1, p2);
00132
00133 createMaps();
00134 }
00135
00136
00137
00138 void CameraBridge::SetParameters(int w, int h,
00139 float cx, float cy, float fx, float fy)
00140 {
00141 m_image_width = w;
00142 m_image_height = h;
00143
00144 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00145 fx, 0, cx,
00146 0, fy, cy,
00147 0, 0, 1);
00148
00149 m_distortion = cv::Mat::zeros(4,1, CV_32F);
00150
00151 m_there_is_calibration = true;
00152 m_there_is_distortion = false;
00153 }
00154
00155
00156
00157 void CameraBridge::SetParameters(int w, int h,
00158 float cx, float cy, float fx, float fy,
00159 float k1, float k2, float p1, float p2)
00160 {
00161 m_image_width = w;
00162 m_image_height = h;
00163
00164 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00165 fx, 0, cx,
00166 0, fy, cy,
00167 0, 0, 1);
00168
00169 m_distortion = (cv::Mat_<float>(4,1) << k1, k2, p1, p2);
00170 createMaps();
00171
00172 m_there_is_calibration = true;
00173 m_there_is_distortion = true;
00174 }
00175
00176
00177
00178
00179 void CameraBridge::SetParameters(ImageType _imageType,
00180 int w, int h,
00181 float cx, float cy, float fx, float fy)
00182 {
00183 m_image_type = _imageType;
00184 m_image_width = w;
00185 m_image_height = h;
00186
00187 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00188 fx, 0, cx,
00189 0, fy, cy,
00190 0, 0, 1);
00191
00192 m_distortion = cv::Mat::zeros(4,1, CV_32F);
00193
00194 m_there_is_calibration = true;
00195 m_there_is_distortion = false;
00196 }
00197
00198
00199
00200
00201 void CameraBridge::SetParameters(ImageType _imageType,
00202 int w, int h,
00203 float cx, float cy, float fx, float fy,
00204 float k1, float k2, float p1, float p2)
00205 {
00206 m_image_type = _imageType;
00207 m_image_width = w;
00208 m_image_height = h;
00209
00210 m_intrinsic_parameters = (cv::Mat_<float>(3,3) <<
00211 fx, 0, cx,
00212 0, fy, cy,
00213 0, 0, 1);
00214
00215 m_distortion = (cv::Mat_<float>(4,1) << k1, k2, p1, p2);
00216 createMaps();
00217
00218 m_there_is_calibration = true;
00219 m_there_is_distortion = true;
00220 }
00221
00222
00223
00224 void CameraBridge::createMaps()
00225 {
00226 cv::initUndistortRectifyMap(m_intrinsic_parameters, m_distortion,
00227 cv::Mat(), m_intrinsic_parameters,
00228 cv::Size(m_image_width, m_image_height), CV_32F,
00229 m_remap_1, m_remap_2);
00230 }
00231
00232
00233
00234 void CameraBridge::ConvertImage(cv::Mat &image) const
00235 {
00236 cv::Mat aux, aux2;
00237
00238
00239 switch(m_image_type){
00240 case BAYER_BG:
00241 cv::cvtColor(image, aux2, CV_BayerBG2RGB);
00242 break;
00243
00244 case BAYER_GB:
00245 cv::cvtColor(image, aux2, CV_BayerGB2RGB);
00246 break;
00247
00248 case BAYER_RG:
00249 cv::cvtColor(image, aux2, CV_BayerRG2RGB);
00250 break;
00251
00252 case BAYER_GR:
00253 cv::cvtColor(image, aux2, CV_BayerGR2RGB);
00254 break;
00255
00256 case RGB:
00257 cv::cvtColor(image, aux, CV_RGB2GRAY);
00258 break;
00259
00260 case BGR:
00261 cv::cvtColor(image, aux, CV_BGR2GRAY);
00262 break;
00263
00264 case BW:
00265 aux = image;
00266 break;
00267 }
00268
00269 if(!aux2.empty())
00270 cv::cvtColor(aux2, aux, CV_RGB2GRAY);
00271
00272
00273 if(m_there_is_distortion){
00274 if(aux.data == image.data) aux = aux.clone();
00275
00276
00277 cv::remap( aux, image, m_remap_1, m_remap_2, cv::INTER_LINEAR,
00278 cv::BORDER_REPLICATE );
00279
00280 }else
00281 image = aux;
00282
00283
00284 cv::GaussianBlur(image, image, cv::Size(3,3), 0);
00285
00286 }
00287
00288
00289
00290 void CameraBridge::DistortPoint(int &x, int &y) const
00291 {
00292 if(m_there_is_distortion && x >= 0 && x < m_image_width &&
00293 y >= 0 && y < m_image_height)
00294 {
00295 int xx = x;
00296 int yy = y;
00297 x = (int)m_remap_1.at<float>(yy, xx);
00298 y = (int)m_remap_2.at<float>(yy, xx);
00299 }
00300 }
00301
00302
00303
00304 void CameraBridge::DistortPoint(float x, float y, float& distx, float& disty) const
00305 {
00306 if(m_there_is_distortion && x >= 0 && x < m_image_width &&
00307 y >= 0 && y < m_image_height)
00308 {
00309 distx = m_remap_1.at<float>(y, x);
00310 disty = m_remap_2.at<float>(y, x);
00311 }else{
00312 distx = x;
00313 disty = y;
00314 }
00315 }
00316
00317