利用MatLab+OpenCV进行相机畸变矫正

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()
 
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章