fast-yolo4/fast/ImageCalibrator.cpp
2024-09-29 13:58:12 +08:00

49 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdafx.h"
#include "ImageCalibrator.h"
#include <opencv2\opencv.hpp>
using namespace std;
CImageCalibrator::CImageCalibrator()
{
}
CImageCalibrator::~CImageCalibrator()
{
}
cv::Mat CImageCalibrator::imageCalibration(cv::Mat inImg)
{
//相机内参矩阵与畸变系数,通过黑白棋盘格标定法测定得到
cv::Mat cameraMatrix = (cv::Mat_ <double>(3, 3) << 720.689879, 0, 957.57171, 0, 721.705519, 935.484724, 0, 0, 1),
distCoeffs = (cv::Mat_ <double>(1, 4) << -0.0765657862, 0.0190079504, -0.032948619, 0.0159874152); //重投影误差rms0.601462
//平移操作,修正鱼眼图像位置偏差,对照测定参数用相机采集的图片
cv::Mat warp_matrix = (cv::Mat_<float>(2, 3) <<
cos(0), -sin(0), -63, //x轴平移像素
sin(0), cos(0), -68); //y轴平移像素
cv::warpAffine(inImg, inImg, warp_matrix, inImg.size(), cv::INTER_LINEAR);
//取有效鱼眼镜头区域
cv::Mat inImg2(inImg, cv::Rect(324, 0, 1944, 1944));
//鱼眼画面展平
cv::Mat outImg, new_intrinsic_mat;
cameraMatrix.copyTo(new_intrinsic_mat);
//调节视场大小,乘的系数越小视场越大,可调整
new_intrinsic_mat.at<double>(0, 0) *= 0.7;
new_intrinsic_mat.at<double>(1, 1) *= 0.7;
//调节校正图中心,建议置于校正图中心
new_intrinsic_mat.at<double>(0, 2) = 0.5 * inImg2.cols;
new_intrinsic_mat.at<double>(1, 2) = 0.5 * inImg2.rows;
cv::fisheye::undistortImage(inImg2, outImg, cameraMatrix, distCoeffs, new_intrinsic_mat);
//展平后扣出有效分析区域
//Mat roi(outImg, cv::Rect(711, 465, 557, 941));
//显示展平后的图像
//cv::imshow("标定效果", outImg); cv::waitKey(0);
return outImg;
}