将数据叠加到背景图像上

发布于 2024-10-17 05:44:18 字数 250 浏览 3 评论 0原文

我最近发现使用 Tableau Public 来使用背景图像和其上的地图数据是多么容易。以下是他们的网站中的流程。正如您所看到的,它相当简单,您只需告诉软件您要使用什么图像以及如何定义坐标即可。

R 中的过程是否如此简单?最好的方法是什么?

I recently figured out how easy it was to use a background image and map data on top of it using Tableau Public. Here is the process from their website. As you can see, it is fairly straightforward, and you simply tell the software what image you want to use and how to define the coordinates.

Is the process as straightforward in R? What would be the best approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

城歌 2024-10-24 05:44:18

JPEG

对于 jpeg 图像,您可以使用 rimage 包中的 read.jpeg()

例如:

anImage <- read.jpeg("anImage.jpeg")
plot(anImage)
points(my.x,my.y,col="red")
...

通过在下一个绘图命令之前设置 par(new=T),您可以在背景图片上构建完整的绘图。 (请参阅 ?par 及下文)

PNG

您可以使用 png 包中的 readPNG 上传 PNG 图像。使用readPNG,您需要rasterImage命令来绘图(另请参阅帮助文件)。在 Windows 上,必须摆脱 Alpha 通道,因为 Windows 到目前为止无法处理每像素 Alpha。 Simon Urbanek 非常友善地指出了这个解决方案:

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
r = as.raster(img[,,1:3])
r[img[,,4] == 0] = "white"

plot(1:2,type="n")
rasterImage(r,1,1,2,2)

GIF

对于 gif 文件,您可以使用 caTools 中的 read.gif。问题是这是旋转矩阵,因此您必须调整它:

Gif <- read.gif("http://www.openbsd.org/art/puffy/ppuf600X544.gif")

n <- dim(Gif$image)
image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)

要在该图像上绘图,您必须正确设置 par,例如:

image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
op <- par(new=T)
plot(1:100,new=T)
par(op)

JPEG

For jpeg images, you can use read.jpeg() from the rimage package.

eg :

anImage <- read.jpeg("anImage.jpeg")
plot(anImage)
points(my.x,my.y,col="red")
...

By setting par(new=T) before the next plot command, you can construct complete plots over a background picture. (see ?par and further down)

PNG

PNG images you can upload using readPNG from the png package. With readPNG, you need the rasterImage command to plot (see also the help files). On Windows, one has to get rid of the alpha channel, as Windows cannot cope with per-pixel alphas up to now. Simon Urbanek was so kind as to point out this solution :

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
r = as.raster(img[,,1:3])
r[img[,,4] == 0] = "white"

plot(1:2,type="n")
rasterImage(r,1,1,2,2)

GIF

For gif files, you can use read.gif from caTools. Problem is that this is rotating the matrix, so you have to adjust it :

Gif <- read.gif("http://www.openbsd.org/art/puffy/ppuf600X544.gif")

n <- dim(Gif$image)
image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)

To plot over this image, you have to set the par correctly, eg :

image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
op <- par(new=T)
plot(1:100,new=T)
par(op)
转角预定愛 2024-10-24 05:44:18

我不确定你想要做的部分是所谓的“地理参考”——拍摄没有坐标信息的图像并精确定义它如何映射到现实世界的行为。

为此,我将使用 Quantum GIS,这是一个免费的开源 GIS 软件包。将图像作为栅格图层加载,然后启动地理配准插件。单击图像上的一些已知点,然后输入这些点的经纬度真实世界坐标。一旦你获得了足够的这些,地理配准器就会计算出如何拉伸和移动你的图像到地球上的真实位置,并编写一个“世界文件”。

然后,R 应该能够使用 rgdal 包中的 readGDAL 读取它,也可能是 raster 包。

I'm not sure that part of what you want to do is what's called "geo-referencing" - the act of taking an image with no coordinate information and precisely defining how it maps onto the real world.

For this I'd use Quantum GIS, a Free and Open Source GIS package. Load in the image as a raster layer, then fire up the georeferencing plugin. Click on some known points on your image and enter the lat-long real-world coordinates of those points. Once you've got enough of those, the georeferencer will work out how to stretch and shift your image to it's real place on the planet, and write a 'world file'.

Then with that R should be able to read it using readGDAL from the rgdal package, and possibly the raster package too.

原谅过去的我 2024-10-24 05:44:18

对于 JPEG 图像,您可以使用 jpeg 库ggplot2 库

通常我发现让轴以像素为单位分级,垂直轴向下为正,并且图片保持其原始纵横比是有用的。所以我可以直接将计算机视觉算法产生的输出输入R,例如该算法可以检测弹孔并从射击目标图片中提取弹孔坐标,然后R可以使用目标图像作为背景绘制二维直方图。

我的代码基于 baptistehttps://stackoverflow.com/a/16418186/15485

library(ggplot2)
library(jpeg)

img <- readJPEG("bersaglio.jpg") # http://www.tiropratico.com/bersagli/forme/avancarica.jpg

h<-dim(img)[1] # image height
w<-dim(img)[2] # image width

df<-data.frame(x=rnorm(100000,w/1.99,w/100),y=rnorm(100000,h/2.01,h/97))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
                                                      # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
     )

在此处输入图像描述

df<-data.frame(x=rnorm(100000,100,w/70),y=rnorm(100000,400,h/100))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
       # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
)

在此处输入图像描述

For a JPEG image you can use the jpeg library and ggplot2 library.

Usually I found useful to have the axis graduated in pixel and the vertical axis going positive in the downward direction and the picture keeping its original aspect ratio. So I can feed R directly with the output produced by computer vision algorithm, for example the algorithm can detect the bullet hole and extract the hole coordinates from a shooting target picture and then R can plot a 2D histogram using the target image as background.

My code is based on code by baptiste found at https://stackoverflow.com/a/16418186/15485

library(ggplot2)
library(jpeg)

img <- readJPEG("bersaglio.jpg") # http://www.tiropratico.com/bersagli/forme/avancarica.jpg

h<-dim(img)[1] # image height
w<-dim(img)[2] # image width

df<-data.frame(x=rnorm(100000,w/1.99,w/100),y=rnorm(100000,h/2.01,h/97))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
                                                      # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
     )

enter image description here

df<-data.frame(x=rnorm(100000,100,w/70),y=rnorm(100000,400,h/100))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
       # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文