前言
代码
python 代码
普通相机
import cv2
import numpy as np
mtx = np.array(
[1018.488091073461, 0.0, 976.4698604089125, 0.0, 1018.1743205737621, 524.3940644115754, 0.0, 0.0, 1.0]).reshape(3,
3)
dist = np.array(
[-0.39265196173805544, 0.20922680814651598, -4.70111490361179e-05, 4.289211380146578e-05, -0.06715044674693742]
).reshape(5, 1)
h, w = 1080, 1920
alpha = 0
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), alpha, (w, h))
img = cv2.imread("F:\\1.png")
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
x, y, w, h = roi
dst = dst[y:y + h, x:x + w]
dst = cv2.resize(dst, (1920, 1080))
cv2.imshow("show", dst)
cv2.waitKey(0)
鱼眼相机
import cv2
import numpy as np
def fish_image_dist(img):
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, (1920, 1080), cv2.CV_16SC2)
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
return undistorted_img
K = np.array([[508.0680189, 0., 947.9423389],
[0., 508.02681978, 506.37321985],
[0., 0., 1.]]).reshape(3, 3)
D = np.array([[0.13142053],
[-0.01600879],
[-0.01790873],
[0.00550534]]).reshape(4, 1)
img = cv2.imread("F:\\1.png")
dist = fish_image_dist(img)
cv2.imshow("show", dist)
cv2.waitKey(0)
c++ 代码
普通相机
#include <iostream>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <io.h>
#include <string>
using namespace std;
using namespace cv;
vector<Mat> read_images_in_folder(cv::String pattern);
int main()
{
// 原图所在的目录
cv::String dirName = "F:\\*jpg";
vector<Mat> images = read_images_in_folder(dirName);
return 0;
}
vector<Mat> read_images_in_folder(cv::String pattern)
{
const int ImgWidth = 1920;
const int ImgHeight = 1080;
const cv::Mat K = (cv::Mat_<double>(3, 3) << 1.00880026e+03, 0.00000000e+00, 9.45387230e+02,
0.00000000e+00, 1.00784680e+03, 5.65509311e+02,
0.00000000e+00, 0.00000000e+00, 1.00000000e+00);
const cv::Mat D = (cv::Mat_<double>(5, 1) << -0.3900199, 0.21476073, - 0.00118118, 0.00081861, - 0.06851613);
cv::Mat map1, map2;
const double alpha = 0; // 决定保留多少黑边
cv::Size imageSize(ImgWidth, ImgHeight);
cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, imageSize, alpha, imageSize, 0);
// 非鱼眼相机
initUndistortRectifyMap(K, D, cv::Mat(), NewCameraMatrix, imageSize, CV_16SC2, map1, map2);
vector<cv::String> fn;
glob(pattern, fn, false);
vector<Mat> images;
size_t count = fn.size(); // 文件夹有多少图片
for (size_t i = 0; i < count; i++)
{
images.push_back(imread(fn[i]));
cout << fn[i] << endl;
cv::Mat RawImage = cv::imread(fn[i]);
cv::Mat UndistortImage;
remap(RawImage, UndistortImage, map1, map2, cv::INTER_LINEAR);
cv::imwrite(fn[i], UndistortImage);
}
return images;
}
鱼眼相机
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
vector<Mat> read_images_in_folder(cv::String pattern);
int main()
{
cv::String dirName = "F:\\*.jpg";
vector<Mat> images = read_images_in_folder(dirName);
return 0;
}
vector<Mat> read_images_in_folder(cv::String pattern)
{
const cv::Mat K = (cv::Mat_<double>(3, 3) << 510.1529232524548, 0, 978.097474431498,0, 510.4286100550981, 529.9045678787428,0, 0, 1);
const cv::Mat D = (cv::Mat_<double>(4, 1) << 0.13159907730082632, -0.025159280794422065, -0.011608807129743496, 0.003894052503105539);
const int ImgWidth = 1920;
const int ImgHeight = 1080;
cv::Mat map1, map2;
cv::Size imageSize(ImgWidth, ImgHeight);
const double alpha = 0;
cv::Mat NewCameraMatrix;
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K, D, imageSize, cv::Matx33d::eye(), NewCameraMatrix, alpha);
cv::fisheye::initUndistortRectifyMap(K, D, cv::Matx33d::eye(), NewCameraMatrix, imageSize, CV_16SC2, map1, map2);
vector<cv::String> fn;
glob(pattern, fn, false);
vector<Mat> images;
size_t count = fn.size(); //number of png files in images folder
for (int i = 0; i < count; i++)
{
images.push_back(imread(fn[i]));
cout << fn[i] << endl;
cv::Mat RawImage = cv::imread(fn[i]);
cv::Mat UndistortImage;
cv::fisheye::undistortImage(RawImage, UndistortImage, K, D, K, imageSize);
cv::imwrite(fn[i], UndistortImage);
}
return images;
}