【传感器标定】图片校正(c++、python代码)

文章目录

  • 前言
  • 代码
    • python 代码
      • 普通相机
      • 鱼眼相机
    • c++ 代码
      • 普通相机
      • 鱼眼相机

前言

  • 一遍给出了相机标定的代码【传感器标定】相机内参标定(c++、python代码)
  • 本文章给出图片校正的代码,python 代码、c++ 代码。
  • 普通相机与鱼眼相机校正代码在细微之处有所不同,普通相机与鱼眼相机校正代码本文都会给出。

代码

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 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

vector read_images_in_folder(cv::String pattern);

int main()
{	
	// 原图所在的目录
	cv::String dirName = "F:\\*jpg";
	vector images = read_images_in_folder(dirName);
	return 0;
}

vector read_images_in_folder(cv::String pattern)
{
	const int ImgWidth = 1920;
	const int ImgHeight = 1080;
	const cv::Mat K = (cv::Mat_(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_(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 fn;
	glob(pattern, fn, false);
	vector 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 
#include 

using namespace std;
using namespace cv;

vector read_images_in_folder(cv::String pattern);

int main()
{
    cv::String dirName = "F:\\*.jpg";
    vector images = read_images_in_folder(dirName);
    return 0;
}

vector read_images_in_folder(cv::String pattern) 
{
    const cv::Mat K = (cv::Mat_(3, 3) << 510.1529232524548, 0, 978.097474431498,0, 510.4286100550981, 529.9045678787428,0, 0, 1);
    const cv::Mat D = (cv::Mat_(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 fn;
    glob(pattern, fn, false);
    vector 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;
}

你可能感兴趣的:(c++,python,opencv,自动驾驶)