在球面上映射图像

时间:2016-12-11 03:06:59

标签: julia texture-mapping

简单来说,我需要映射要在球面上使用的图像。我试图这样做几个小时。在谷歌搜索我没有找到任何适当的解决方案(为愚蠢的人解释)。

我认为这个链接的代码: 我需要https://www.codeproject.com/articles/19712/mapping-images-on-spherical-surfaces-using-c。但是(我认为一切都很好)可以让它在朱莉娅工作。

到目前为止,这是我的代码:

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

image只是朱莉娅产生的黑白噪音,我需要用它包裹一个球体。

1 个答案:

答案 0 :(得分:2)

mandrill

我不知道你的代码在做什么,但修复一些索引问题可以帮助你开始。无论如何,看起来它做了一些球形......

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`)