使用 imageR 将图像从笛卡尔坐标映射到极坐标
Mapping image from cartesian to polar coordinates with imageR
我正在尝试使用 imageR 将全景图像映射到极坐标,但得到了奇怪的结果。
举个例子,我想在 CC BY-NC-SA 下拍摄一张像这样的方形全景图(由 Flickr user gadl (Alexandre Duret-Lutz 提供)并编辑成方形):
并将其重新映射为类似这样的内容(我在 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
需要成为三角函数内的变量。
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)
我正在尝试使用 imageR 将全景图像映射到极坐标,但得到了奇怪的结果。
举个例子,我想在 CC BY-NC-SA 下拍摄一张像这样的方形全景图(由 Flickr user gadl (Alexandre Duret-Lutz 提供)并编辑成方形):
并将其重新映射为类似这样的内容(我在 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
需要成为三角函数内的变量。
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)