问题描述
我正在尝试使用imageR将全景图像映射到极坐标中,但结果很奇怪。
作为一个例子,我想拍摄一个像这样的正方形全景图(由Flickr user gadl(Alexandre Duret-Lutz提供)在CC BY-NC-SA下并编辑为正方形) >
并将其重新映射为这样的东西(我在GIMP中已经完成):
通过以下代码,我在imageR中已经非常接近
:library(imager)
pano <- mirror(load.image("pano_image.jpg"),"y") # mirroring the image to map to center
map.shift <- function(x,y) list(x = y*cos(x),y = y*sin(x)) # Here,y is the radius and x is theta
polar_pano <- imwarp(pano,map = map.shift)
plot(polar_pano)
但是我得到了奇怪的结果:
我不确定为什么它只映射到一个象限? (注意:我意识到插值在这个例子中会很奇怪-这不是问题)。 为了确认此应该是否有效,这是一个玩具示例:
library(ggplot)
test <- data.frame("val" = rep(100:1,each = 99),theta = rep(1:100,99),r = rep(1:100,each = 99))
ggplot(test,aes(x = theta,y = r,col = val)) + geom_point()
# Now converting from polar to cartesian
test$x <- test$r*cos(test$theta)
test$y <- test$r*sin(teast$theta)
ggplot(test_p2c,aes(x = x,y = y,col = val)) + geom_point()
解决方法
您的结果只有一个象限,因为结果转换同时具有负值和正值。
在您的函数中,结果的左上角是(-400,-400)
,右下角是(400,400)
。将200减半并加到(0,0)
和(400,400)
。
将触发参数从-pi
缩放到pi
。
要使原始图像的底部成为结果圆的中心,x
必须是trig函数中的变量。
library(imager)
pano <- load.image("pano_image.jpg")
pano <- mirror(load.image("pano_image.jpg"),"y")
map_shift <- function(x,y) {
list(
x = y * cos((x - 200) / 400 * 2 * pi) / 2 + 200,y = y * sin((x - 200) / 400 * 2 * pi) / 2 + 200
)
}
polar_pano <- imwarp(pano,map = map_shift)
plot(polar_pano)