使用imageR将图像从笛卡尔坐标映射到极坐标

问题描述

我正在尝试使用imageR将全景图像映射到极坐标中,但结果很奇怪。

作为一个例子,我想拍摄一个像这样的正方形全景图(由Flickr user gadlAlexandre Duret-Lutz提供)在CC BY-NC-SA下并编辑为正方形) >

square panorama

并将其重新映射为这样的东西(我在GIMP中已经完成):

polar coordinate remapping of square panorama

通过以下代码,我在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)

但是我得到了奇怪的结果:

imager output of polar coordinate mapped panorama

我不确定为什么它只映射到一个象限? (注意:我意识到插值在这个例子中会很奇怪-这不是问题)。 为了确认此应该是否有效,这是一个玩具示例:

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)

plot

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...