Mapping image on spherical surface

573 views Asked by At

In simple words i need to map a image to be use in a spherical surface. I being trying to do this for several hours. searching in google I din't find any proper solution (explained for dumb people).

I thinks the code from this link: https://www.codeproject.com/articles/19712/mapping-images-on-spherical-surfaces-using-c is what i need. but (i think everything is alright) can make it work in Julia.

This is my code so far:

image = brightNoise(height,width,seed,rand=true)

arr = Array{Float64}(height,width)

function MapCoordinate(i1, i2,w1,w2,p)
    return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1
end

function Rotate(angle, axisA, axisB)
    return axisA * cos(angle) - axisB * sin(angle),axisA * sin(angle) + axisB * cos(angle)
end

phi0 = 0.0
phi1 = pi
theta0 = 0.0
theta1 = 2.0*pi
radius = 50


arr = Array{Float64}(height,width)

for i= 1:size(image)[1]
    for j= 1:size(image)[2]
        #map the angles from image coordinates
        theta = MapCoordinate(0.0,width - 1,theta1, theta0, i)
        phi = MapCoordinate(0.0,height - 1,phi0,phi1, j)
        #find the cartesian coordinates
        x = radius * sin(phi) * cos(theta);
        y = radius * sin(phi) * sin(theta);
        z = radius * cos(phi);
        #apply rotation around X and Y axis to reposition the sphere
        y,z=Rotate(1.5, y, z);
        x,z=Rotate(pi/2, x, z);
        #plot only positive points
        if (z > 0)
            color = image[i,j]
            ix = floor(Int64,x)  
            iy = floor(Int64,y)
            arr[ix,iy] = color
            println(ix,iy)
        end
     end
 end

The image is just a black and white noise generated in Julia, i need to wrap a sphere with it.

1

There are 1 answers

4
daycaster On BEST ANSWER

mandrill

I have little idea what your code is doing, but fixing some of the indexing issues gives something that might help you get started. It looks like it's doing something spherical, anyway...

using Images, FileIO
mandrill = load(mandrill.png")    
height, width = size(mandrill)
arr = colorim(Array{Float64}(height, width, 3))

function MapCoordinate(i1, i2, w1, w2, p)
    return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1
end

function Rotate(angle, axisA, axisB)
    return axisA * cos(angle) - axisB * sin(angle),axisA * sin(angle) + axisB * cos(angle)
end

phi0 = 0.0
phi1 = pi
theta0 = 0.0
theta1 = 2.0 * pi
radius = 200

for i = 1:size(mandrill, 1)
    for j = 1:size(mandrill, 2)
        # map the angles from image coordinates
        theta = MapCoordinate(1.0, width - 1, theta1, theta0, i)
        phi   = MapCoordinate(1.0, height - 1,  phi0,   phi1, j)
        # find the cartesian coordinates
        x = radius * sin(phi) * cos(theta)
        y = radius * sin(phi) * sin(theta)
        z = radius * cos(phi)
        # apply rotation around X and Y axis to reposition the sphere
        y, z = Rotate(1.5, y, z)
        x, z = Rotate(pi/2, x, z)

        # plot only positive points
        if z > 0
            color = mandrill[i, j]
            ix = convert(Int, floor(x + width/2))
            iy = convert(Int, floor(y + height/2))
            arr[ix, iy, :] = [color.r, color.g, color.b]
        end
     end
 end

save("/tmp/mandrill-output.png", arr)
run(`open /tmp/mandrill-output.png`)