基于OpenCV的全景图剪切程序

去年实习的时候,接到一个任务将公司软件生成的一张全景图扭曲切割成六个正常的面,然后动态生成一个3D网页。忙活了2天后完成代码。但最终因为某些原因未被采用。
首先配置OpenCV。
找寻了众多资料,找了一个算是我觉得最靠谱的:http://tieba.baidu.com/p/3931605400

安装好OpenCV以后我们来看原始图片
目标图片

全景图的剪切原理我也不一一叙述了,贴出详细的原理解释:
http://paulbourke.net/geometry/transformationprojection/
接下来我们就po出源码:

 #include <opencv2/imgproc/imgproc.hpp>    
#include <opencv2/core/core.hpp>          
#include <opencv2/highgui/highgui.hpp>   
#include<math.h>
#include <iostream>

using namespace cv;
using namespace std;
float M_PI = 3.14159265358979323846f;
float faceTransform[6][2] =
{
    {
  0, 0},
    {M_PI /2,0},
    {M_PI,0},
    {-M_PI /2,0},
    {
  0,-M_PI/2},
    {
  0,M_PI/2}      
};


inline void createCubeMapFace(const Mat &in, Mat &face, int faceId = 0, const int width = -1,const int height = -1)
{

    float inWidth = in.cols;                                                 
    float inHeight = in.rows;     // 获取图片的行列数量

//  cout << in.cols;
//  cout << in.rows;
//  system("pause");
    // Allocate map
    Mat mapx(height, width, CV_32F);
    Mat mapy(height, width, CV_32F);                                         //分配图的x,y轴


    // Calculate adjacent (ak) and opposite (an) of the
    // triangle that is spanned from the sphere center 
    //to our cube face.
    const float an = sin(M_PI / 4);
    const float ak = cos(M_PI / 4);                                          //计算相邻ak和相反an的三角形张成球体中心

//  cout << ak;
//  cout << an;
//  system("pause");
    const float ftu = faceTransform[faceId][0];
    const float ftv = faceTransform[faceId][1];

    // For each point in the target image, 
    // calculate the corresponding source coordinates.                      对于每个图像计算相应的源坐标
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {           

            // Map face pixel coordinates to [-1, 1] on plane               将坐标映射在平面上
            float nx = (float)y / (float)height - 0.5f;
            float ny = (float)x / (float)width - 0.5f;

            nx *= 2;
            ny *= 2;

            // Map [-1, 1] plane coord to [-an, an]                          
            // thats the coordinates in respect to a unit sphere 
            // that contains our box. 
            nx *= an;
            ny *= an;

            float u, v;

            // Project from plane to sphere surface.
            if (ftv == 0) {
                // Center faces
                u = atan2(nx, ak);
                v = atan2(ny * cos(u), ak);
                u += ftu;
            }
            else if (ftv > 0) {
                // Bottom face 
                float d = sqrt(nx * nx + ny * ny);
                v = M_PI / 2 - atan2(d, ak);
                u = atan2(ny, nx);
            }
            else {
                // Top face
                //cout << "aaa";
                float d = sqrt(nx * nx + ny * ny);
                v = -M_PI / 2 + atan2(d, ak);
                u = atan2
  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
OpenCV是一个功能强大的像处理库,可以用于创建全景。它利用特征提取、特征匹配、齐次估计、像配准和像融合等技术,将一系列像合成为全景。相比于Pillow库,OpenCV在处理全景拼接方面更为灵活和强大,能够更好地消除全景中的接缝和阴影。\[1\] 在使用OpenCV创建全景时,可以使用Stitcher类进行像拼接。首先,需要读入要拼接的像,然后实例化Stitcher类。接下来,调用stitch方法,将要拼接的像作为参数传入。该方法会返回拼接后的全景以及可视化的拼接过程。最后,使用imshow方法显示拼接前的各个像、特征点匹配结果和最终的全景。\[2\] 在拼接过程中,关键点和特征向量的提取是非常重要的。可以使用SIFT算法来检测像的关键点和计算特征向量。首先,将像转换为灰度像,然后实例化SIFT方法。接下来,调用detectAndCompute方法,传入像作为参数,该方法会返回像的关键点和特征向量。最后,将关键点转换为float32位的列表,并返回关键点和特征向量。\[3\] 总结起来,使用OpenCV可以利用特征提取、特征匹配、齐次估计、像配准和像融合等技术来创建全景。在拼接过程中,可以使用Stitcher类进行像拼接,并使用SIFT算法来提取关键点和特征向量。 #### 引用[.reference_title] - *1* [OpenCV合成全景](https://blog.csdn.net/hzblucky1314/article/details/130570600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [opencv实际案例(三)全景像的拼接](https://blog.csdn.net/weixin_44660348/article/details/113764084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值