【问题标题】:Overlay data onto background image将数据叠加到背景图像上
【发布时间】:2011-06-26 23:07:52
【问题描述】:

我最近发现使用 Tableau Public 使用背景图像并在其上映射数据是多么容易。这是他们website的过程。如您所见,它相当简单,您只需告诉软件您要使用什么图像以及如何定义坐标。

这个过程在 R 中是否很简单?最好的方法是什么?

【问题讨论】:

    标签: r background-image


    【解决方案1】:

    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)
    

    要在此图像上绘图,您必须正确设置标准杆,例如:

    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)
    

    【讨论】:

    • 我尝试在 2.10 和 2.12.1 上安装 rimage 包,但两次都出现相同的错误。我正在使用 Windows XP:警告:无法访问存储库 stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.10 的索引警告消息:在 getDependencies(pkgs, dependencies, available, lib) 中:包‘rimage’不可用
    • @Btibert3 :我可以毫无问题地从 www.freestatistics.org/cran 安装两者。无论如何尝试获取另一个存储库,cran.r-project.org/mirrors.html 中提到的 cran 镜像之一就可以了。
    • 我的工作笔记本电脑由于某种原因出现问题。我刚刚尝试了我的个人笔记本电脑,它安装没有问题。最后,我应该说它是一个 GIF 文件。
    【解决方案2】:

    对于 JPEG 图像,您可以使用 jpeg libraryggplot2 library

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

    我的代码基于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")
    )
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 感谢您的帮助。该图像实际上没有任何地理属性;这是一个曲棍球场,我想在上面绘制 x/y 坐标。我的图像是一个 GIF 文件。
      猜你喜欢
      • 2017-03-10
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多