4,449
社区成员
发帖
与我相关
我的任务
分享
Mat myUndistortion(Mat& src, int camera)
{
Mat src_temp, dst_temp;
Mat camera_matrix, distortion_matrix;
//copyMakeBorder参数
int top = (int)(src.rows/8);
int bottom = top;
int left = (int)(src.cols/8 + 20);
int right = left;
int borderType = BORDER_CONSTANT;
Scalar value = Scalar(255,255,255);
//扩充边界,用白色填充
copyMakeBorder(src, src_temp, top, bottom, left, right, borderType, value);
src_temp.convertTo(src_temp, CV_32FC1, 1.0/255);
dst_temp.create(src_temp.size(), src_temp.type());
switch(camera)
{
case 1:
//camera 1
camera_matrix = (Mat_<double>(3,3) << 参数1, 参数2, ...,参数9);
distortion_matrix = (Mat_<double>(1,4) <<参数1,...参数4);
break;
case 2:
//camera 2
camera_matrix = (Mat_<float>(3,3) << 参数1, 参数2, ...,参数9);
distortion_matrix = (Mat_<float>(1,4) <<参数1,...参数4);
break;
case 3:
//camera 3
camera_matrix = (Mat_<float>(3,3) <<参数1, 参数2, ...,参数9);
distortion_matrix = (Mat_<float>(1,4) << 参数1,...参数4 );
break;
case 4:
break;
}
if(camera == 4)
{
return src;
}
CvMat* _input = &src_temp.operator CvMat();
CvMat* _output = &dst_temp.operator CvMat();
CvMat* _camera = &camera_matrix.operator CvMat();
CvMat* _distortion = &distortion_matrix.operator CvMat();
cvUndistort2(_input, _output, _camera, _distortion);
Mat output(_output, 1);
//showTestImage(output, 100);
Mat img;
resize(output, img, Size(352,288), 0, 0, CV_INTER_AREA);
img.convertTo(img, CV_8UC1, 255.0);
return img;
}