将数据叠加到背景图像上
我最近发现使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JPEG
对于 jpeg 图像,您可以使用
rimage
包中的read.jpeg()
。例如:
通过在下一个绘图命令之前设置 par(new=T),您可以在背景图片上构建完整的绘图。 (请参阅
?par
及下文)PNG
您可以使用
png
包中的readPNG
上传 PNG 图像。使用readPNG
,您需要rasterImage
命令来绘图(另请参阅帮助文件)。在 Windows 上,必须摆脱 Alpha 通道,因为 Windows 到目前为止无法处理每像素 Alpha。 Simon Urbanek 非常友善地指出了这个解决方案:GIF
对于 gif 文件,您可以使用
caTools
中的read.gif
。问题是这是旋转矩阵,因此您必须调整它:要在该图像上绘图,您必须正确设置 par,例如:
JPEG
For jpeg images, you can use
read.jpeg()
from therimage
package.eg :
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 thepng
package. WithreadPNG
, you need therasterImage
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 :GIF
For gif files, you can use
read.gif
fromcaTools
. Problem is that this is rotating the matrix, so you have to adjust it :To plot over this image, you have to set the par correctly, eg :
我不确定你想要做的部分是所谓的“地理参考”——拍摄没有坐标信息的图像并精确定义它如何映射到现实世界的行为。
为此,我将使用 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.
对于 JPEG 图像,您可以使用 jpeg 库 和 ggplot2 库。
通常我发现让轴以像素为单位分级,垂直轴向下为正,并且图片保持其原始纵横比是有用的。所以我可以直接将计算机视觉算法产生的输出输入R,例如该算法可以检测弹孔并从射击目标图片中提取弹孔坐标,然后R可以使用目标图像作为背景绘制二维直方图。
我的代码基于 baptiste 在 https://stackoverflow.com/a/16418186/15485
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