算法思想参考:http://blog.csdn.net/weixinhum/article/details/50611750
柱面投影是图片拼接的前期的一部分工作,以下代码只是简单的实现了投影,还可以优化,
柱面半径设置位图片宽度的一半,即 R = width/2
代码运算流程是 对于dst图片上的每一个像素点,通过公式计算出src上对应的位置(hnum,wnum),把src上这个位置的像素值赋值给dst。
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/*
实现一个简单的图像投影实例,
算法根据csdn博客http://blog.csdn.net/weixinhum/article/details/50611750
该代码对目录下的test图片进行柱面投影
*/
int main()
{
Mat src = imread("test.jpg");
Mat dst(src.rows,src.cols,src.type());
int width = src.cols, height = src.rows;
double x, y;
double R = width / 2;
int drcpoint;
for (int hnum = 0; hnum < height;hnum++)
{
for (int wnum = 0; wnum < width;wnum++)
{
double k = R / sqrt(R*R + (wnum - width / 2)*(wnum - width / 2));
x = (wnum - width / 2) / k + width / 2;
y = (hnum - height / 2) / k + height / 2;
if (0 < x && x < width && 0 < y &&y < height)
{
dst.at<Vec3b>(hnum, wnum) = src.at<Vec3b>(int(y), int(x));
}
}
}
imshow("origin Image",src);
imshow("柱面投影图",dst);
waitKey(0);
return 0;
}
运行结果: