pre:
關於矯正的數學原理這裏不再贅述,可以參考openCV官方文檔和https://github.com/Nocami/PythonComputerVision-6-CameraCalibration
1.準備數據
step1:去openCV下載pattern.jpg
顯示在屏幕上即可。
step2:用需要標定的相機進行拍照,各個角度拍4-5張。共20張左右。
step3:需要記錄拍照屏幕的方格的大小,推薦使用PS像素轉釐米,如圖在我PC上的長度
2.MATLAB上場
step1: 新建文件
J = (checkerboard(300,4,5)>0.5);
figure, imshow(J);
step2:
在控制檯輸入 cameraCalibrator
上傳你拍的圖
之後會出現
這個時候就需要測量的長度參數值 單位mm
step3: 等待......
step4:矯正(請忽略雜亂有序的實驗室)
step5:導出參數 在工作區看到了參數
3.Python矯正
這裏面的參數值有的是不需要的所以要把需要的提取出來
這裏需要的是
1.
總共有五個,徑向畸變3個(k1,k2,k3)和切向畸變2個(p1,p2)。沒的添上0。
2.
對比着加到代碼裏。
大概就這樣
C++版
// correct_camera.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//
#include "pch.h"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
VideoCapture inputVideo(0);
if (!inputVideo.isOpened())
{
cout << "Could not open the input video: " << endl;
return -1;
}
Mat frame;
Mat frameCalibration;
inputVideo >> frame;
Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
cameraMatrix.at<double>(0, 0) = 4.450537506243416e+02;
cameraMatrix.at<double>(0, 1) = 0.192095145445498;
cameraMatrix.at<double>(0, 2) = 3.271489590204837e+02;
cameraMatrix.at<double>(1, 1) = 4.473690628394497e+02;
cameraMatrix.at<double>(1, 2) = 2.442734958206504e+02;
Mat distCoeffs = Mat::zeros(5, 1, CV_64F);
distCoeffs.at<double>(0, 0) = -0.320311439187776;
distCoeffs.at<double>(1, 0) = 0.117708464407889;
distCoeffs.at<double>(2, 0) = -0.00548954846049678;
distCoeffs.at<double>(3, 0) = 0.00141925006352090;
distCoeffs.at<double>(4, 0) = 0;
Mat view, rview, map1, map2;
Size imageSize;
imageSize = frame.size();
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
imageSize, CV_16SC2, map1, map2);
while (1) //Show the image captured in the window and repeat
{
inputVideo >> frame; // read
if (frame.empty()) break; // check if at end
remap(frame, frameCalibration, map1, map2, INTER_LINEAR);
imshow("Origianl", frame);
imshow("Calibration", frameCalibration);
char key = waitKey(1);
if (key == 27 || key == 'q' || key == 'Q')
break;
}
return 0;
}
python版
代碼是我超哥整的,@超哥 https://blog.csdn.net/qq_41170600/article/details/103037028
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
def undistort(frame):
fx = 1311.94228326091
cx = 937.984968117315
fy = 1310.63631268594
cy = 514.783585422419
k1, k2, p1, p2, k3 = -0.469785052535390, 0.274212670963307, 0.0, 0.0, 0.0
# 相機座標系到像素座標系的轉換矩陣
k = np.array([
[fx, 0, cx],
[0, fy, cy],
[0, 0, 1]
])
# 畸變係數
d = np.array([
k1, k2, p1, p2, k3
])
h, w = frame.shape[:2]
mapx, mapy = cv2.initUndistortRectifyMap(k, d, None, k, (w, h), 5)
return cv2.remap(frame, mapx, mapy, cv2.INTER_LINEAR)
while(cap.isOpened()):
ret, frame = cap.read()
# frame =
cv2.imshow('frame', undistort(frame))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()