How to convert panorama image to cube in PHP


mohan21

How can I convert the panorama image below to a cube in php?

I used imagick, js code and... but no result.

hand picture enter image description here

please help me thanks

mohan21

After a lot of effort...

I get the correct result

My final code:

# get x,y,z coords from out image pixels coords
# i,j are pixel coords
# faceIdx is face number
# faceSize is edge length
function outImgToXYZ($i, $j, $faceIdx, $faceSize)
{
    $a = 2.0 * (float) $i / $faceSize;
    $b = 2.0 * (float) $j / $faceSize;

    switch ($faceIdx)
    {
        case 0: return [-1.0, 1.0 - $a, 3.0 - $b]; break; # back
        case 1: return [$a - 3.0, -1.0, 3.0 - $b]; break; # left
        case 2: return [1.0, $a - 5.0, 3.0 - $b]; break; # front
        case 3: return [7.0 - $a, 1.0, 3.0 - $b]; break; # right
        case 4: return [$b - 1.0, $a - 5.0, 1.0]; break; # top
        case 5: return [5.0 - $b, $a - 5.0, -1.0]; break; # bottom
    }

    return [0, 0, 0];
}

function clip($x, $min, $max)
{
    if ($x > $max) return $max;
    if ($x < $min) return $min;
    return $x;
}

# convert using an inverse transformation
function convertFace($imgIn, $imgInWidth, $imgInHeight, $imgOut, $imgOutWidth, $imgOutHeight)
{
    $pi = pi();
    $edge = $imgInWidth / 4;
    $face = 0;
    $face2 = 0;
    $rng = null;

    for($xOut = 0; $xOut <= $imgOutWidth; $xOut++)
    {
        $face = floor($xOut / $edge); // 0 - back, 1 - left 2 - front, 3 - right
        if ($face === 2) $rng = range(0, $edge * 3, 1);
        else $rng = range($edge, $edge * 2, 1);

        for($yOut =0; $yOut <= $imgOutHeight; $yOut++)
        {
            if ($yOut < $edge) $face2 = 4; // top
            else if ($yOut >= 2 * $edge) $face2 = 5; // bottom
            else $face2 = $face;

            list($x, $y, $z) = outImgToXYZ($xOut, $yOut, $face2, $edge);
            $theta = atan2($y, $x);
            $r = hypot($x, $y);
            $phi = atan2($z, $r);

            # source img coords
            $uf = 2.0 * $edge * ($theta + $pi) / $pi;
            $vf = 2.0 * $edge * ($pi / 2 - $phi) / $pi;

            # Use bilinear interpolation between the four surrounding pixels
            $ui = floor($uf); # coord of pixel to bottom left
            $vi = floor($vf);
            $u2 = $ui + 1; # coords of pixel to top right
            $v2 = $vi + 1;
            $mu = $uf - $ui; # fraction of way across pixel
            $nu = $vf - $vi;

            # Pixel values of four corners
            $a = imagecolorsforindex($imgIn, imagecolorat($imgIn, $ui % $imgInWidth, clip($vi, 0, $imgInHeight - 1)));
            $b = imagecolorsforindex($imgIn, imagecolorat($imgIn, $u2 % $imgInWidth, clip($vi, 0, $imgInHeight - 1)));
            $c = imagecolorsforindex($imgIn, imagecolorat($imgIn, $ui % $imgInWidth, clip($v2, 0, $imgInHeight - 1)));
            $d = imagecolorsforindex($imgIn, imagecolorat($imgIn, $u2 % $imgInWidth, clip($v2, 0, $imgInHeight - 1)));

            # interpolate
            list($r, $g, $b) =
                [
                    $a["red"] * (1 - $mu) * (1 - $nu) + $b["red"] * ($mu) * (1 - $nu) + $c["red"] * (1 - $mu) * $nu + $d["red"] * $mu * $nu,
                    $a["green"] * (1 - $mu) * (1 - $nu) + $b["green"] * ($mu) * (1 - $nu) + $c["green"] * (1 - $mu) * $nu + $d["green"] * $mu * $nu,
                    $a["blue"] * (1 - $mu) * (1 - $nu) + $b["blue"] * ($mu) * (1 - $nu) + $c["blue"] * (1 - $mu) * $nu + $d["blue"] * $mu * $nu
                ];
            imagesetpixel($imgOut, $xOut, $yOut, imagecolorallocate($imgOut, (int) round($r), (int) round($g), (int) round($b)));
        }
    }

    return $imgOut;
}

// start convert
$imgInPath = "./main.jpg";
$imgIn = imagecreatefromjpeg($imgInPath);
$imgInSize = getimagesize($imgInPath);
$imgInWidth = $imgInSize[0];
$imgInHeight = $imgInSize[1];
$imgOutWidthHeight = $imgInWidth / 4;
$imgOutFullWidth = $imgInWidth;
$imgOutFullHeight = $imgInWidth * 3 / 4;
$imgFormat = "jpg";

$imgOut = imagecreate($imgOutFullWidth, $imgOutFullHeight);
imagefill($imgOut, 0, 0, imagecolorallocate($imgOut, 0, 0, 0));
imagejpeg($imgOut, "./temp.jpg", 100);
$imgOut = imagecreatefromjpeg("./temp.jpg");
imagefill($imgOut, 0, 0, imagecolorallocate($imgOut, 0, 0, 0));
$imgOut = convertFace($imgIn, $imgInWidth, $imgInHeight, $imgOut, $imgOutFullWidth, $imgOutFullHeight);
imagejpeg($imgOut, "./temp.jpg", 100);
imagedestroy($imgOut);

$faceNames = [['', '', 'top', ''], ['left', 'front', 'right', 'back'], ['', '', 'bottom', '']];
for($i = 0; $i < 3; $i++)
{
    for($j = 0; $j < 4; $j++)
    {
        $faceName = $faceNames[$i][$j];
        if($faceName != "")
        {
            $im = imagecreatefromjpeg("temp.jpg");
            $im2 = imagecrop($im, ["x" => ($j * $imgOutWidthHeight), "y" => ($i * $imgOutWidthHeight), "width" => $imgOutWidthHeight, "height" => $imgOutWidthHeight]);
            if($im2 !== FALSE)
            {
                imagejpeg($im2, "main_$faceName.jpg", 100);
                imagedestroy($im2);
            }
            imagedestroy($im);
        }
    }
}

unlink("./temp.jpg");

Related


How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to convert panorama image to cube in PHP

mohan21 How can I convert the panorama image below to a cube in php? I used imagick, js code and... but no result. hand picture please help me thanks mohan21 After a lot of effort... I get the correct result My final code: # get x,y,z coords from out image pix

How to scroll panorama image horizontally/vertically in Android?

Francisco Dudin Garcia I'm looking for some way to scroll or scan a panorama image in Android. I mean show part of the image to adjust the height (or vertical width) of the image and automatically jog the view to scroll all the images. E.g: This image is so bi

How to convert armadillo matrix to cube?

rich I am trying to recreate the following Python numpy code: num_rows, num_cols = data.shape N = 4 data = data.reshape(N, num_rows/N, num_cols) Using Armadillo matrices and cubes in C++? How to do this most efficiently. I don't think resizing/reshaping opera

How to convert armadillo matrix to cube?

rich I am trying to recreate the following Python numpy code: num_rows, num_cols = data.shape N = 4 data = data.reshape(N, num_rows/N, num_cols) Using Armadillo matrices and cubes in C++? How to do this most efficiently. I don't think resizing/reshaping opera

How to convert armadillo matrix to cube?

rich I am trying to recreate the following Python numpy code: num_rows, num_cols = data.shape N = 4 data = data.reshape(N, num_rows/N, num_cols) Using Armadillo matrices and cubes in C++? How to do this most efficiently. I don't think resizing/reshaping opera

How to convert image to binary image using php?

st_ahmed Is there any way to convert a normal image to a binary image (each pixel has two possible values, 0 or 1)? I found some solutions in Matlab , but I am looking for a solution in PHP/JavaScript . Stimulant Using a lovely photo I just took, I've prepared

How to convert image to binary image using php?

st_ahmed Is there any way to convert a normal image to a binary image (each pixel has two possible values, 0 or 1)? I found some solutions in Matlab , but I am looking for a solution in PHP/JavaScript . Stimulant Using a lovely photo I just took, I've prepared

Convert a cubemap to an isometric panorama

Sark I want to convert from a cubemap [figure1] to an equirectangular panorama [figure2]. figure 1 figure 2 It's possible to convert from "spherical" to "cubic" (following the steps: 2:1 equirectangular panorama to cubemap ), but lost on how to reverse it. Fig

Convert a cubemap to an isometric panorama

Sark I want to convert from a cubemap [figure1] to an equirectangular panorama [figure2]. figure 1 figure 2 It's possible to convert from "spherical" to "cubic" (following the steps: 2:1 equirectangular panorama to cubemap ), but lost on how to reverse it. Fig

Convert a cubemap to an isometric panorama

Sark I want to convert from a cubemap [figure1] to an equirectangular panorama [figure2]. figure 1 figure 2 It's possible to convert from "spherical" to "cubic" (following the steps: 2:1 equirectangular panorama to cubemap ), but lost on how to reverse it. Fig

Convert a cubemap to an isometric panorama

Sark I want to convert from a cubemap [figure1] to an equirectangular panorama [figure2]. figure 1 figure 2 It's possible to convert from "spherical" to "cubic" (following the steps: 2:1 equirectangular panorama to cubemap ), but lost on how to reverse it. Fig

Convert a cubemap to an isometric panorama

Sark I want to convert from a cubemap [figure1] to an equirectangular panorama [figure2]. figure 1 figure 2 It's possible to convert from "spherical" to "cubic" (following the steps: 2:1 equirectangular panorama to cubemap ), but lost on how to reverse it. Fig

How to Add a Background Image to the Windows Phone Panorama App

username I'm trying to add a background image to a Windows Phone Panorama app. When I add the image using XAML code, I can see the image in the preview. However, when I run the app, I can't see the image. The image size I use is 1536x1280 Here is the XAML code

How to Add a Background Image to the Windows Phone Panorama App

username I'm trying to add a background image to a Windows Phone Panorama app. When I add the image using XAML code, I can see the image in the preview. However, when I run the app, I can't see the image. The image size I use is 1536x1280 Here is the XAML code

How to use and open 360 degree *.pano panorama image files?

Jonathan Microsoft's Photosynth (upload via iOS app) website exports to *.pano. Facebook now natively supports 360-degree photos. What will I do with metadata and/or view these 360 photos? username To open *.pano on Linux, build the following code: https://git