OpenCV实现影像畸变矫正GPU

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fb_help/article/details/88069039

OpenCV实现影像畸变矫正GPU

OpenCV实现影像矫正使用的是
initUndistortRectifyMap()计算畸变的映射
remap()计算映射,其详解见:OpenCV函数remap详解

注:k1,k2,p1,p2,k3的顺序
在这里插入图片描述
只能纠正不考虑skew扭曲参数的情况。

	Mat src_cpu = imread(in_filename);
	cv::cuda::GpuMat src(src_cpu);
	cv::cuda::GpuMat distortion(src.size(),src.type());
	cv::Mat result;
	//Mat src = imread(in_filename);
	//Mat distortion = src.clone();
	Mat camera_matrix = Mat::zeros(3, 3, CV_64FC1);
	Mat distortion_coefficients = Mat::zeros(1,5,CV_64FC1);


	cv::Size imageSize = src.size();
	camera_matrix.at<double>(0, 0) = f;
	camera_matrix.at<double>(0, 2) = x0;
	camera_matrix.at<double>(1, 1) = f;
	camera_matrix.at<double>(1, 2) = y0;
	camera_matrix.at<double>(2, 2) = 1;
	//std::cout << camera_matrix << std::endl;

	distortion_coefficients.at<double>(0, 0) = K1;
	distortion_coefficients.at<double>(0, 1) = K2;
	distortion_coefficients.at<double>(0, 2) = P1;
	distortion_coefficients.at<double>(0, 3) = P2;
	distortion_coefficients.at<double>(0, 4) = K3;

	//矫正
	//undistort(src, distortion, camera_matrix, distortion_coefficients);

	cv::Mat map1, map2;
	initUndistortRectifyMap(
		camera_matrix, distortion_coefficients, Mat(),
		camera_matrix, imageSize,
		CV_32FC1, map1, map2);

	::cv::cuda::GpuMat m_mapx;
	::cv::cuda::GpuMat m_mapy;
	m_mapx = ::cv::cuda::GpuMat(map1);
	m_mapy = ::cv::cuda::GpuMat(map2);

	::cv::cuda::remap(src, distortion, m_mapx, m_mapy, INTER_LINEAR);
	distortion.download(result);


	//imshow("img", src);
	//imshow("undistort", distortion);
	//imwrite("undistort.jpg", distortion);

	//imshow("img", src_cpu);
	//
	//imshow("undistort", result);
	//waitKey(0);

	imwrite(out_filename, result);

猜你喜欢

转载自blog.csdn.net/fb_help/article/details/88069039