opencv相机畸变矫正
- opencv相机畸变矫正
-
- 1、畸变矫正保存加载 C++
- 2、畸变矫正保存加载 python
-
- 3、效果
opencv相机畸变矫正
1、畸变矫正保存加载 C++
#include
#include
#include
using namespace cv;
using namespace std;
Mat image, img_gray;
int BOARDSIZE[2]{ 8,11 };
int main()
{
vector<vector<Point3f>> object_points;
vector<Point3f> obj_world_pts;
vector<vector<Point2f>> images_points;
vector<Point2f> img_corner_points;
vector<String> images_path;
string image_path = "./pic/chess118/*.png";
glob(image_path, images_path);
for (int i = 0; i < BOARDSIZE[1]; i++)
{
for (int j = 0; j < BOARDSIZE[0]; j++)
{
obj_world_pts.push_back(Point3f(j, i, 0));
}
}
for (int i = 0; i < images_path.size(); i++)
{
image = imread(images_path[i]);
cvtColor(image, img_gray, COLOR_BGR2GRAY);
bool found_success = findChessboardCorners(img_gray, Size(BOARDSIZE[0], BOARDSIZE[1]),
img_corner_points,
CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
if (found_success)
{
TermCriteria criteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.001);
cornerSubPix(img_gray, img_corner_points, Size(11, 11),
Size(-1, -1), criteria);
drawChessboardCorners(image, Size(BOARDSIZE[0], BOARDSIZE[1]), img_corner_points,
found_success);
object_points.push_back(obj_world_pts);
images_points.push_back(img_corner_points);
}
char text[] = "image";
char* output = text;
imshow(output, image);
waitKey(200);
}
Mat cameraMatrix, distCoeffs, R, T;
calibrateCamera(object_points, images_points, img_gray.size(),
cameraMatrix, distCoeffs, R, T);
cout << "cameraMatrix:" << endl;
cout << cameraMatrix << endl;
cout << "*****************************" << endl;
cout << "distCoeffs:" << endl;
cout << distCoeffs << endl;
cout << "*****************************" << endl;
cout << "Rotation vector:" << endl;
cout << R << endl;
cout << "*****************************" << endl;
cout << "Translation vector:" << endl;
cout << T << endl;
Mat src, dst;
src = imread("./pic/chess118/100000.png");
undistort(src, dst, cameraMatrix, distCoeffs);
char texts[] = "image_dst";
char* dst_output = texts;
imshow(dst_output, dst);
waitKey(100);
imwrite("./pic/save/1.png", dst);
destroyAllWindows();
system("pause");
return 0;
}
2、畸变矫正保存加载 python
2.1保存矩阵
import cv2 as cv
import numpy as np
import glob
import os
import scipy.io as scio
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
row = 8
column = 11
objp = np.zeros((row * column, 3), np.float32)
objp[:, :2] = np.mgrid[0:row, 0:column].T.reshape(-1, 2)
objp=objp*0.02
objpoints = []
imgpoints = []
images = glob.glob('./pic/chess118/*.png')
i=0
for fname in images:
img = cv.imread(fname)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, corners = cv.findChessboardCorners(gray, (row, column), None)
if ret == True:
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
cv.drawChessboardCorners(img, (row, column), corners, ret)
i += 1;
cv.imshow('img', img)
cv.waitKey(500)
cv.destroyAllWindows()
ret, Matrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
datadir = "./pic/chess118/"
path = os.path.join(datadir)
img_list = os.listdir(path)
for i in img_list:
img = cv.imread(os.path.join(path, i))
h, w = img.shape[:2]
newMatrix, roi = cv.getOptimalNewCameraMatrix(Matrix, dist, (w, h), 1, (w, h))
dst = cv.undistort(img, Matrix, dist, None, newMatrix)
x, y, w, h = roi
dst = dst[y:y + h, x:x + w]
cv.imwrite('./pic/save/' + i, dst)
tot_error = 0
for i in range(len(objpoints)):
imgpoints2, _ = cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], Matrix, dist)
error = cv.norm(imgpoints[i], imgpoints2, cv.NORM_L2) / len(imgpoints2)
tot_error += error
print("------------------保存内参和畸变参数-------------------")
dataFile1 = './neican.mat'
dataFile2='./jican.mat'
scio.savemat(dataFile1, {'train':Matrix})
scio.savemat(dataFile2, {'train':dist})
print("------------保存完成----------------------------")
print("------------------各个校准参数-------------------")
print('ret:\n', ret)
print('mtx:\n', Matrix)
print('dist:\n', dist)
print('rvecs:\n', rvecs)
print('tvecs:\n', tvecs)
print("total error: ", tot_error / len(objpoints))
2.1直接使用保存的矩阵
import cv2 as cv
import numpy as np
import glob
import os
import scipy.io as scio
print("------------------从文件加载内参和畸变参数-------------------")
dataFile1 = './neican.mat'
dataFile2='./jican.mat'
data = scio.loadmat(dataFile1)
data2 = scio.loadmat(dataFile2)
Matrix = data['train']
dist = data2['train']
print("内参:\n", Matrix)
print("畸参:\n",dist)
print("------------加载完成----------------------------")
print("------------用内参和畸参对图片进行矫正----------------------------")
datadir = "./pic/chess118/"
path = os.path.join(datadir)
img_list = os.listdir(path)
for i in img_list:
img = cv.imread(os.path.join(path, i))
h, w = img.shape[:2]
newMatrix, roi = cv.getOptimalNewCameraMatrix(Matrix, dist, (w, h), 1, (w, h))
dst = cv.undistort(img, Matrix, dist, None, newMatrix)
x, y, w, h = roi
dst = dst[y:y + h, x:x + w]
cv.imwrite('./pic/save/' + i, dst)
print("------------矫正完成,矫正后的图片保存在save文件夹----------------------------")
3、效果