#鱼眼模型参考链接
本文假设去畸变后的图像与原图大小一样大。由于去畸变后的图像符合针孔投影模型,因此不同的去畸变焦距得到不同的视场大小,且物体的分辨率也不同。可以见上图,当焦距缩小为一半时,相同大小的图像(横向投影距离一样长),对应的视场角不同。所以为了扩大视野,需要缩小焦距,作为相机坐标系到去畸变图像的投影内参焦距。
理论方面不再多说,直接上代码:
C++ 版本
#include
#include
#include
using namespace std;
//图像去畸变部分///
int main(){
cv::Size img_sizea;
std::string image_file = "test.jpeg";
cv::Mat src = cv::imread(image_file);
cv::Mat distortiona(img_sizea,CV_8UC3);
// 内参
cv::Mat camera_matrixa = (cv::Mat_<double>(3, 3) << 5.4108215568312232e+02, 0.0, 1.0318237337253406e+03, 0, 5.4083086444334469e+02, 1.0225293088570558e+03, 0, 0, 1);
cv::Mat distortion_coefficientsa=(cv::Mat_<double >(1,4)<<1.0926628389307196e-01,-6.5713320780575097e-04,8.4866561354316559e-03,-4.2045330300667406e-03);
cv::Mat new_intrinsic_mat(3, 3, CV_64FC1, cv::Scalar(0));
camera_matrixa.copyTo(new_intrinsic_mat);
//调整输出校正图的视场
new_intrinsic_mat.at<double>(0, 0) *= 0.4; //注意数据类型,非常重要
new_intrinsic_mat.at<double>(1, 1) *= 0.4;
//调整输出校正图的中心
new_intrinsic_mat.at<double>(0, 2) *= 1.0;
new_intrinsic_mat.at<double>(1, 2) *= 1.0;
// new_intrinsic_mat.at(0, 2) += 0.0;
// new_intrinsic_mat.at(1, 2) += 0.0;
cv::fisheye::undistortImage(src, distortiona, camera_matrixa,
distortion_coefficientsa, new_intrinsic_mat);
cv::resize(distortiona, distortiona, cv::Size(1024,1024));
cv::imshow("undistort", distortiona);
cv::waitKey(0);
imwrite("undistort.jpg", distortiona);
return 0;
}
Python 版本
import cv2 as cv
import numpy as np
def fisheye_undistortion(img, K, D, fs):
#一定要使用copy,作为不同的变量
newK = K.copy()
#调整输出校正图的视场
newK[0][0] = fs * newK[0][0]
newK[1][1] = fs * newK[1][1]
#调整输出校正图的中心
newK[0][2] = 1.0 * newK[0][2]
newK[1][2] = 1.0 * newK[1][2]
undis_img = cv.fisheye.undistortImage(img, K, D, None, newK)
return undis_img, newK
def camera2undistortionimg(camera_point, newK):
x = camera_point[0] / camera_point[2]
y = camera_point[1] / camera_point[2]
u = x * newK[0][0] + newK[0][2]
v = y * newK[1][1] + newK[1][2]
return [u, v]
#内参
K = np.array([[5.4108215568312232e+02, 0.0, 1.0318237337253406e+03],
[0, 5.4083086444334469e+02, 1.0225293088570558e+03],
[0, 0, 1]], dtype=np.float32)
#畸变系数
D = np.array([[1.0926628389307196e-01],
[-6.5713320780575097e-04],
[8.4866561354316559e-03],
[-4.2045330300667406e-03]], dtype=np.float32)
#读取图像
srcimg = cv.imread("./test.jpeg")
#去畸变
fs = 0.4 #焦距比例控制,控制视场大小
undis_img, newK = fisheye_undistortion(srcimg, K, D, fs)
print(newK)
#相机到去畸变图像映射
camera_p = (-20, 20, 6)
[u, v] = camera2undistortionimg(camera_p, newK)
print([u, v])
#显示验证
cv.circle(undis_img,(int(u), int(v)), 10, (0,0,255), -1)
undis_img = cv.resize(undis_img,(1024, 1024))
cv.imshow("undis_img",undis_img)
cv.waitKey(0)