如何将图像转换为ggplot2散点图的点数据 [英] How to convert an image into point data for ggplot2 scatterplot

查看:150
本文介绍了如何将图像转换为ggplot2散点图的点数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Github上找到了,开发人员对此进行了分析安东尼·普罗亚斯(Antonio Prohias)的《间谍与间谍》漫画的248期分期中的精选数据出现在《疯狂杂志》中.

I recently found a package on Github where the dev analyzed fan-curated data on the 248 installments of the Spy vs Spy comic by Antonio Prohias appearing in Mad Magazine.

他进行了一些基本的探索性数据分析,计算了黑人间谍胜利的连续净得分,然后运行了一个非参数检验(Wald Wolfowitz检验)以查看其中一名间谍的连续胜利聚类,以确定Prohias是通过反转先前的结果来保持分数平衡,还是可能是他选择了最爱.

He goes through some basic exploratory data analysis, computes a running net score of Black Spy victories and then runs a non-parametric test (Wald Wolfowitz test) to look at clusters of consecutive victories by one of the Spies in order to ascertain if Prohias kept the score balance by reversing the previous outcome, or if he perhaps picked favorites.

虽然我发现它很有趣,但是我对间谍图最感兴趣.

While I found it a fun exercise I was most interested in the Spy Plot.

间谍图的点数据实际上来自 MATLAB复活节彩蛋 spy()包,然后开发人员将此点数据作为小标题放入R: :tribble .

The point data for the Spy Plot actually came from a MATLAB easter egg spy() package and the dev put this point data into R as a tibble::tribble.

我的问题是如何从图像创建点数据?是否可以使用imager()在R中进行边缘检测获取轮廓

My question is how can one create point data from an image? Is it possible to do edge detection in R with imager() to get an outline

然后以某种方式将此图像数据转换为tbl_df?我不熟悉位图数组,但也许答案就在这样的东西上?

And then somehow convert this image data into into a tbl_df? I'm unfamiliar with bitmap arrays but perhaps the answer lies in something like this?

推荐答案

我们可以使用imager::cannyEdges获取边缘,然后将坐标放入数据框中进行绘制.

We can use imager::cannyEdges to get the edges, then put the coordinates in a data frame to plot.

library('ggplot2')
library('imager')

plot(boats)

img <- cannyEdges(boats)
plot(img)

看起来img是一个具有4维的逻辑数组.

It looks like img is a logical array with 4 dimensions.

dim(img)
# [1] 256 384   1   3

我只想要两个尺寸,所以我将丢弃其中的两个尺寸.

I just want two dimensions, so I'll discard two of the dimensions.

img <- img[, , 1, 1]
img[1:8, 1:8]
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]
# [1,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [3,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

which可以将此矩阵转换为坐标列表.

which can convert this matrix into a list of coordinates.

coords <- which(img, arr.ind = T)
head(coords)
#      row col
# [1,] 255   1
# [2,] 255   2
# [3,] 255   3
# [4,] 255   4
# [5,] 255   5
# [6,] 255   6

现在可以绘制了.

df <- data.frame(x = coords[,1], y = coords[,2])

ggplot(df, aes(x, -y)) +
  geom_point()

这篇关于如何将图像转换为ggplot2散点图的点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆