opencv柱面投影

在做全景拼接的时候,为了保持图片中的空间约束与视觉的一致性,需要进行柱面投影,否则离中心图像距离越远的图像拼接后变形越大。

柱面投影公式为

变换效果如下:

int main()
{
	cv::Mat image1 = cv::imread("images/1.jpg", 1);
	if (!image1.data)
		return 0;
	imshow("image1", image1);

	Mat imgOut = Mat(image1.rows, image1.cols, CV_8UC3);
	float w = image1.cols;
	float h = image1.rows;
	float f = (w / 2) / atan(PI / 8);

	for (int i = 0; i < image1.rows; i++)
	{
		for (int j = 0; j < image1.cols; j++)
		{
			float x = j;
			float y = i;
			float x1 = f * atan((x - w / 2) / f) + f * atan(w / (2.0f * f));
			float y1 = f * (y - h / 2.0f) / sqrt((x - w / 2.0f) * (x - w / 2.0f) + f * f) + h / 2.0f;

			int col = (int)(x1 + 0.5f);//加0.5是为了四舍五入
			int row = (int)(y1 + 0.5f);//加0.5是为了四舍五入

			if (col < image1.cols && row < image1.rows)
			{
				imgOut.at<Vec3b>(row, col)[0] = image1.at<Vec3b>(i, j)[0];
				imgOut.at<Vec3b>(row, col)[1] = image1.at<Vec3b>(i, j)[1];
				imgOut.at<Vec3b>(row, col)[2] = image1.at<Vec3b>(i, j)[2];
			}
		}
	}

	imshow("imgOut", imgOut);

	waitKey(0);
	return 0;
}

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值