Julia 球面上的映射图像

Julia 球面上的映射图像,julia,texture-mapping,Julia,Texture Mapping,简单地说,我需要映射一个用于球面的图像。我已经试了好几个小时了。在谷歌搜索,我找不到任何合适的解决方案(为哑巴解释) 我认为这个链接的代码是: 这就是我需要的。但是(我认为一切都很好)可以让朱莉娅成功 这是我目前的代码: image = brightNoise(height,width,seed,rand=true) arr = Array{Float64}(height,width) function MapCoordinate(i1, i2,w1,w2,p) return ((p

简单地说,我需要映射一个用于球面的图像。我已经试了好几个小时了。在谷歌搜索,我找不到任何合适的解决方案(为哑巴解释)

我认为这个链接的代码是: 这就是我需要的。但是(我认为一切都很好)可以让朱莉娅成功

这是我目前的代码:

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
图像
只是Julia中产生的黑白噪声,我需要用它包裹一个球体。

我不知道您的代码在做什么,但修复一些索引问题可能会帮助您开始。不管怎样,它看起来像是在做一件球形的事情

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

这似乎更适合于代码审查,而不是堆栈溢出。工作正常。(我是说,至少跑步)谢谢。但是如果我尝试使用另一个图像,我会得到一个“BoundsError”错误,试图访问2000x1000图像中的索引1001。有什么想法吗?再次感谢@exsnake必须确保正确计算索引,否则在访问数组时会超出范围。我不知道你是怎么计算指数的。你可以在实验时使用
钳夹
,我只是意识到图像必须是方形的。更好地了解工作,但我会在中间得到一些奇怪的工件…我会继续接触代码。谢谢你的帮助@是的,有很多事情要做——我可能把高度和宽度弄混了(试试
w,h=widthheight(img)
)。有很多关于图像的文档。如果您有问题,请提出问题,因为它不是最为最终用户友好的软件包…:)