如何找到正确的图像和像素以复制到等矩形/ 360全景画布中

我正在尝试找出如何在等边矩形画布(水平360°和垂直180°)上获取像素位置(x,y),并找到与该等边矩形画布上的确切位置匹配的对应图像和像素。找到匹配项后,我们只需将图像中正确的像素(颜色)复制到等矩形画布上即可。

但是我目前的问题是找出什么图像看到与等角画布上的像素位置(x,y)相对应的位置,并找到要复制的正确像素。这个想法是制作一个可以将图像拼接在一起的完整360度等角矩形图像的贴纸。等矩形画布的尺寸为4096 x 2048。

因此,要弄清楚这个想法是在等角画布中循环遍历每个像素,并计算其在球面或圆柱/地理坐标系中的位置

我们像这样遍历像素。

   for (int i = 0; i < EquirectangularCanvas.size().height; i++)
    {
        for (int j = 0; j < EquirectangularCanvas.size().width; j++)
        {

然后,我们很可能必须将其转换为笛卡尔坐标,并比较/减去图像上的信息,直到找到具有与矩形矩形像素位置相对应的可见点的正确图像为止。我们在图像上拥有的信息如下。

示例图片:

尺寸1280 x 720像素。

水平视角为92°,垂直视角为45°。

图像是在45度偏航和45度俯仰下拍摄的。

如何找到正确的图像和像素以复制到等矩形/ 360全景画布中

由于图像是在游戏内部捕获的,因此图像的FOV和位置将具有100%的准确性。所以位置永远是100%

如何找到正确的图像和像素以复制到等矩形/ 360全景画布中

我希望至少在数学方面,我能在正确的方向上获得一些帮助。最终的目标不是使用C ++使用Opencv或类似的方法来进行像素位置移动等。我这样做是因为我想学习,而且我认为这是一个非常有趣且具有挑战性的项目。关于将图像放置在等角矩形画布中,我在互联网上找不到太多相关信息。但是,有很多关于在不同投影之间进行转换的信息,而不是真正关于将图像转换成等矩形投影的信息。

基本上,这就是我在这方面走了多远。我使自己成为一个小程序,可以帮助我测试一些代码。它打印出与等角矩形画布上鼠标位置相对应的一些信息。我的计划是首先找到一种方法来检查图像是否在鼠标所在的位置拍摄。比返回true。下一步是将正确的位置从图像准确复制到等矩形画布。 代码正在使用OpenCV。希望我能在这里得到一些帮助。

#define RadiansToDegrees(angleRadians) ((angleRadians) * 180.0 / M_PI)
#define RAD(x) M_PI*(x)/180.0
static void OnmouseMove(int event,int x,int y,int,void*)
{
    Mat EquirectangularCanvas(2048,4096,CV_8UC3,Scalar(0,0));//Equirectangular Canvas 180 degrees,360 degrees
    
    Vec3b color(0,255,0);
    line(EquirectangularCanvas,Point(0,EquirectangularCanvas.size().height / 2),Point(EquirectangularCanvas.size().width,color,2);//Draw Line In Center For Referanse
    line(EquirectangularCanvas,Point(EquirectangularCanvas.size().width / 2,0),EquirectangularCanvas.size().height),2);//Draw Line In Center For Referanse
    
    ostringstream MouseString;
    MouseString << "Mouse Position(X:" << x << ",Y:" << y << ");";
    putText(EquirectangularCanvas,MouseString.str(),25),FONT_HERSHEY_SIMPLEX,1,2,LINE_AA);//Draw Mouse Pixel Position

    float Phi = M_PI * y / EquirectangularCanvas.size().height;//This will position midpoints in the image along the y-axis in the sphere
    float Theta = 2 * M_PI * x / EquirectangularCanvas.size().width;//This will position midpoints in the image along the x-axis in the sphere

    //Don't know if it is better centering the midpoint. It can be done like this. But than i can't figure out getting the right pixel location when converting from cartesian to spherical.
    //float Phi = M_PI * (y - (EquirectangularCanvas.size().height / 2)) / EquirectangularCanvas.size().height;//This will position midpoints in the center of the image.
    //float Theta = 2 * M_PI * (x - (EquirectangularCanvas.size().width / 2)) / EquirectangularCanvas.size().width;//This will position midpoints in the center of the image.

    ostringstream SphericalCoordinates;
    ostringstream SphericalCoordinatesInDegrees;
    SphericalCoordinates << "Spherical Coordinates(X/Theta/Longitude:" << Theta << ",Y/Phi/Latitude:" << Phi << ");";
    SphericalCoordinatesInDegrees << "Spherical Coordinates In Degrees(X:" << RadiansToDegrees(Theta) << ",Y:" << RadiansToDegrees(Phi) << ");";
    putText(EquirectangularCanvas,SphericalCoordinates.str(),55),LINE_AA);
    putText(EquirectangularCanvas,SphericalCoordinatesInDegrees.str(),55 + 30),LINE_AA);

    Vec3d vec_cartesian;//Convert Spherical To Cartesian
    vec_cartesian[0] = sin(Phi) * cos(Theta);
    vec_cartesian[1] = sin(Phi) * sin(Theta);
    vec_cartesian[2] = cos(Phi);

    ostringstream Cartesian;
    Cartesian << "Rectangular/Cartesian(X:" << vec_cartesian[0] << ",Y:" << vec_cartesian[1] << ",Z:" << vec_cartesian[2] << ");";
    putText(EquirectangularCanvas,Cartesian.str(),85 + 30),LINE_AA);//Draw Cartesian Coordinates

    //Here is the part i am stuck at.
    //We have the theta and phi corresponding to a pixel in an equirectangular projection.
    //We than need to find the point in one of the images(We have in this case one image as describes above,for testing purposes) corresponding to that pixel in the equirectangular canvas.
    //equirectangular canvas xy -> Images xy -> place color from image xy in equirectangular canvas xy
    
    //I use mouse position in this case for easier testing and understanding. But when i get the math right it is going to get coded into a loop that loops over all the pixel in the equirectangular canvas.
    //Like this..
    /*for (int i = 0; i < (EquirectangularCanvas.size().height; i++)
    {
        for (int j = 0; j < EquirectangularCanvas.size().width; j++)
        {
    */

    //So what i think i need to do first is checking if image is outside fov. 
    //Then calculate how far away it is from center of that image and calculate to get the exact pixel to copy the color from..
    
    
    /*Maybe Some useful math -->*/
// XYZ-eular rotation https://github.com/whdlgp/Equirectangular_rotate
//Mat eular2rot(Vec3d theta)
//{
//  // Calculate rotation about x axis
//  Mat R_x = (Mat_<double>(3,3) <<
//      1,//      0,cos(theta[0]),-sin(theta[0]),sin(theta[0]),cos(theta[0])
//      );
//
//  // Calculate rotation about y axis
//  Mat R_y = (Mat_<double>(3,3) <<
//      cos(theta[1]),sin(theta[1]),//      -sin(theta[1]),cos(theta[1])
//      );
//
//  // Calculate rotation about z axis
//  Mat R_z = (Mat_<double>(3,3) <<
//      cos(theta[2]),-sin(theta[2]),//      sin(theta[2]),cos(theta[2]),1);
//
//  // Combined rotation matrix
//  Mat R = R_x * R_y * R_z;
//
//  return R;
//}

    Vec2d vec_rot;//CONVERSION FROM CARTESIAN TO SPHERICAL
    vec_rot[0] = acos(vec_cartesian[2]);//phi //latitude
    vec_rot[1] = atan2(vec_cartesian[1],vec_cartesian[0]);//theta //longitude
    

    ostringstream RETURNEDSPHERICALCOORDS;
    RETURNEDSPHERICALCOORDS << "RETURNED SPHERICAL COORDS(X:" << vec_rot[1] << ",Y:" << vec_rot[0] << ");";
    putText(EquirectangularCanvas,RETURNEDSPHERICALCOORDS.str(),115 + 30),LINE_AA);


    Vec2i vec_pixel;
    vec_pixel[0] = EquirectangularCanvas.size().height * vec_rot[0] / M_PI;
    vec_pixel[1] = EquirectangularCanvas.size().width * vec_rot[1] / (2 * M_PI);

    ostringstream PixelCoordToFind;
    PixelCoordToFind << "Pixel Coord For RETURNED SPHERICAL COORDS(X:" << vec_pixel[1] << ",Y:" << vec_pixel[0] << ");";
    putText(EquirectangularCanvas,PixelCoordToFind.str(),145 + 30),LINE_AA);
    /*<-- Maybe Some useful math*/

    imshow("Equirectangular Canvas",EquirectangularCanvas);
}

int main()
{
    namedwindow("Equirectangular Canvas",WINDOW_AUTOSIZE);
    setMouseCallback("Equirectangular Canvas",OnmouseMove);
    
    waitKey(0);
    destroyAllWindows();
    
    return 1;

}
iCMS 回答:如何找到正确的图像和像素以复制到等矩形/ 360全景画布中

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1764395.html

大家都在问