【问题标题】:Obtain the boundaries from a png file从 png 文件中获取边界
【发布时间】:2019-05-06 05:02:27
【问题描述】:

我想知道如何从这种图像中获取边界。

例如:

我正在寻找使用

转换一些图像
image <- as(x, 'SpatialGridDataFrame')

但它只会给我特别重的图像。

在这种情况下,使用 x_coord 和 y_coord 我可以创建一个简单的对象。

 x_coord <- c(16.48438,  17.49512,  24.74609, 22.59277, 16.48438)
 y_coord <- c(59.736328125, 55.1220703125, 55.0341796875, 
 61.142578125, 59.736328125)
 xym <- cbind(x_coord, y_coord)
 xym

 library(sp)
 p = Polygon(xym)
 ps = Polygons(list(p),1)
 sps1 = SpatialPolygons(list(ps))
 plot(sps1)

我希望为作为示例添加的圆形 png 获得一组 x_coord 和 y_coord。

【问题讨论】:

  • 您是如何读取图像文件的?
  • library(imager) im &lt;- load.image("circle.jpg") plot(im) @TeeKea

标签: r sp


【解决方案1】:

您可以使用pixsets 方法(在imager 包中)来识别给定图像中圆的边缘,如下所示:

px <- im > 0.6 #Select pixels of the circle (i.e., those with high luminance)
plot(px)

现在,当您绘制px 时,您会得到以下结果:

要获取像素的坐标,请使用以下命令:

coord <- where(px)
head(coord)

这给了你这样的东西:

#  x y cc
#1 1 1  1
#2 2 1  1
#3 3 1  1
#4 4 1  1
#5 5 1  1
#6 6 1  1

要获得边界,请使用以下内容:

boundaries <- boundary(px)
boundaries.xy <- where(boundaries)
head(boundaries.xy)

它为您提供以下内容:

#    x  y cc
#1 103 64  1
#2 102 65  1
#3 104 65  1
#4 103 66  1
#5 185 71  1
#6 184 72  1

您甚至可以按如下方式保存圆形像素:

px_image <- as.cimg(px)
save.image(px_image, "px_image.jpg")

希望对你有帮助。

【讨论】:

  • 谢谢!它解决了寻找边界的问题。
猜你喜欢
  • 2012-05-08
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多