将鱼眼图像转换为等角

时间:2019-06-27 17:45:41

标签: c++ opencv fisheye

我想将鱼眼图像转换为正角(球形)投影。我阅读了此文档(http://paulbourke.net/dome/dualfish2sphere/)。根据本文档,我编写了一些代码,但本文档中不太可能得出结果。

在显示代码之前,我对将鱼眼图像转换为等角投影过程有疑问。

我了解的是,首先,我必须将鱼眼图像一张一张地转换为等角投影。经过这个过程,在这个例子中,我有两个重新映射的图像,它们现在是等角的。之后,我必须合并这些图像。

如果是真的,我应该如何合并这两个等矩形的图像。合并过程是拼接吗?

我尝试使用C ++代码进行映射。我尝试过的实现如下所示:

#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
#define PI 3.1415926536
Point2f getInputPoint(int x, int y, int srcwidth, int srcheight)
{
    Point2f pfish;
    float theta, phi, r;
    Point3f psph;
    float FOV = 210*PI/180;
    //float FOV = (float)PI / 180 * 150;
    //float FOV2 = (float)PI / 180 * 120;;
    float width = srcwidth;
    float height = srcheight;

    theta = 2.0 * PI * (x / width - 0.5); // -pi to pi
    phi = PI * (y / height - 0.5);  // -pi/2 to pi/2

    psph.x = cos(phi) * sin(theta);
    psph.y = cos(phi) * cos(theta);
    psph.z = sin(phi);

    theta = atan2(psph.z, psph.x);
    phi = atan2(sqrt(psph.x * psph.x + psph.z * psph.z), psph.y);
    r = width * phi / FOV;
    //float r2 = height * phi / FOV2;
    pfish.x = 0.5 * width + r * cos(theta);
    pfish.y = 0.5 * height + r * sin(theta);
    return pfish;
}
int main(int argc, char** argv)
{   
    cout << "staraadsted" << endl;
    Mat orignalImage = imread("left.JPG");

    if (orignalImage.empty())
    {
        cout << "Empty issssmage\n";
        return 0;
    }
    Mat outImage(orignalImage.rows, orignalImage.cols, CV_8UC3);
    //getInputPoint(0,5,10,10);
    //namedWindow("result", CV_WINDOW_NORMAL);
    for (int i = 0; i < outImage.cols; i++)
    {
        for (int j = 0; j < outImage.rows; j++)
        {

            Point2f inP = getInputPoint(i, j, orignalImage.cols, orignalImage.rows);
            //cout<<"in "<<i<<","<<j<<endl;
            //cout<<"out "<<inP.x<<","<<inP.y<<endl;
            Point inP2((int)inP.x, (int)inP.y);
            if (inP2.x >= orignalImage.cols || inP2.y >= orignalImage.rows)
                continue;
            if (inP2.x < 0 || inP2.y < 0)
                continue;
            Vec3b color = orignalImage.at<Vec3b>(inP2);
            outImage.at<Vec3b>(Point(i, j)) = color;
        }
    }

    imwrite("result_left.jpg", outImage);
}

我希望像this文档中那样具有2:1比例的图像。

预期图像: enter image description here

我得到的是: enter image description here

没有2:1的图像比例。我可以使用cv :: Resize()更改比率,但这是正确的吗?

对于正确的图像,我得到:

enter image description here

但是在文档中,期望的图像是正确的:

enter image description here

我的错误是什么?

0 个答案:

没有答案