从 png 文件中获取边界

Obtain the boundaries from a png file

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

例如:

我正在寻找使用

转换一些图像
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)

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

您可以使用 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")

希望对您有所帮助。