千家信息网

C语言中OpenCV怎样实现柱面投影

发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,C语言中OpenCV怎样实现柱面投影,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。前言在做全景拼接的时候,为了保持图片中的空间约束与视觉
千家信息网最后更新 2025年11月13日C语言中OpenCV怎样实现柱面投影

C语言中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(row, col)[0] = image1.at(i, j)[0];                                imgOut.at(row, col)[1] = image1.at(i, j)[1];                                imgOut.at(row, col)[2] = image1.at(i, j)[2];                        }                }        }         imshow("imgOut", imgOut);         waitKey(0);        return 0;}

实现效果

针对灰度图像

cv::Mat image1 = cv::imread("E:\\zcb_work\\2113\\pic2\\k.jpg", 0);        if (!image1.data)                return 0;        imshow("image1", image1);        cv::Mat image2 = cv::imread("E:\\zcb_work\\2113\\pic2\\j.jpg", 0);        if (!image2.data)                return 0;        imshow("image2", image2);   Mat imgOut1 = Mat(image1.rows, image1.cols, CV_8UC1);        imgOut1.setTo(0);        Mat imgOut2 = Mat(image2.rows, image2.cols, CV_8UC1);        imgOut2.setTo(0);                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)                        {                                imgOut1.at(row, col) = image1.at(i, j);                                imgOut2.at(row, col) = image2.at(i, j);                                //imgOut.at(row, col)[1] = image1.at(i, j)[1];                                //imgOut.at(row, col)[2] = image1.at(i, j)[2];                        }                }        }         imshow("imgOut1", imgOut1);        imshow("imgOut2", imgOut2);

实现效果

原图

柱面投影

用surf算法,特征检测,

合成这样,呵呵呵,

看完上述内容,你们掌握C语言中OpenCV怎样实现柱面投影的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0