opencv 鱼眼矫正

前提

这里不讨论怎么获取矫正参数,假定已经获取矫正参数

使用undistort

#include 
using namespace cv;
void correct_photo(const char * jpg)
{
	Mat src = imread(jpg);
	Mat distortion = src.clone();
	Mat camera_matrix = Mat(3, 3, CV_32FC1);
	Mat distortion_coefficients;
	//导入相机内参和畸变系数矩阵
	FileStorage file_storage("out_camera_data.xml", FileStorage::READ);
	file_storage["Camera_Matrix"] >> camera_matrix;
	file_storage["Distortion_Coefficients"] >> distortion_coefficients;
	file_storage.release();
	undistort(src, distortion, camera_matrix, distortion_coefficients);
}

undistort函数对于单幅

2、使用remap

2.1 说明

void cvRemap( const CvArr* src, CvArr* dst,const CvArr* mapx, const CvArr* mapy,int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,CvScalar fillval=cvScalarAll(0) );

参数说明:
src——输入图像.
  dst——输出图像.
  mapx——x坐标的映射 (32fC1 image).
  mapy——y坐标的映射 (32fC1 image).
  flags——插值方法和以下开关选项的组合:
  CV_WARP_FILL_OUTLIERS——填充边界外的像素. 如果输出图像的部分象素落在变换后的边界外,那么它们的值设定为 fillval。
  fillval——用来填充边界外面的值.

2.2 show me the code

#include 
#include 

using namespace std;

using namespace cv;
int main()
{
	const cv::Mat K = (cv::Mat_<double>(3, 3) << 6.5746697810243404e+002, 0.0, 3.1950000000000000e+002
		, 0.0, 6.5746697810243404e+002, 2.3950000000000000e+002, 0.0, 0.0, 1.0);
	const cv::Mat D = (cv::Mat_<double>(5, 1) << -4.1802327018241026e-001, 5.0715243805833121e-001, 0.0, 0.0, 
		-5.7843596847939704e-001);

	//const string str = "/home/jiang/4_learn/WeChatCode/ImageUndistort/data/";
	//const int nImage = 5;
	const int ImgWidth = 640;
	const int ImgHeight = 480;

	cv::Mat map1, map2;
	cv::Size imageSize(ImgWidth, ImgHeight);
	const double alpha = 1;
	cv::Mat NewCameraMatrix = getOptimalNewCameraMatrix(K, D, 
		imageSize, alpha, imageSize, 0);
	initUndistortRectifyMap(K, D, cv::Mat(), NewCameraMatrix, 
		imageSize, CV_16SC2, map1, map2);

	VideoCapture capture("l:/109_sub.mkv");
	Mat frame;
	while (capture.isOpened())
	{
		capture >> frame;
		//string InputPath = str + to_string(i) + ".png";
		//cv::Mat RawImage = cv::imread(InputPath);
		//cv::imshow("RawImage", RawImage);

		cv::Mat UndistortImage;
		remap(frame, UndistortImage, map1, map2, cv::INTER_LINEAR);
		cv::imshow("origin", frame);
		cv::imshow("UndistortImage", UndistortImage);

		//string OutputPath = str + to_string(i) + "_un" + ".png";
		//cv::imwrite(OutputPath, UndistortImage);
		cv::waitKey(2);
	}

	return 0;
}

不过矫正函数是非常耗cpu的,图像大了以后尤其明显。

你可能感兴趣的:(c++,音视频和c++,java,物联网,数据结构与算法,opencv,鱼眼矫正)