- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下图片:
我想获得一个列表,其中包含 (x, y)
- 每个 blob 的外部和内部轮廓的坐标(我们称它们为 blob A 和 B)。
import cv2
from skimage import measure
blob = cv2.imread('blob.png', 0)
contours, hier = cv2.findContours(blob, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
labels = measure.label(blob)
props = measure.regionprops(labels)
for ii in range(0,len(props))
xy = props[ii].coords
plt.figure(figsize=(18, 16))
plt.imshow(blob, cmap='gray')
plt.plot(xy[:, 0], xy[:,1])
plt.show()
所需的输出图像,其中蓝色和红色是从 (x, y)
坐标列表 A 和 B 中绘制的:
最佳答案
您可以直接从 cv2.findContours
获得 (x, y)
坐标.要识别单个 blob,请查看层次结构 hier
。第四个索引告诉您,可能的内部(或子)轮廓与哪个外部(或父)轮廓相关。大多数外部轮廓的索引为 -1
,所有其他轮廓都具有非负值。因此,对于绘图/绘图,一种天真的方法是,在迭代轮廓时,每次看到 -1
时增加一个 blob 计数器,并使用相同的颜色绘制所有轮廓,直到下一个 -1
显示。
import cv2
from skimage import io # Only needed for web grabbing images, use cv2.imread for local images
# Read image; find contours with hierarchy
blob = io.imread('/image/Ga5Pe.png')
contours, hier = cv2.findContours(blob, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Define sufficient enough colors for blobs
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
# Draw all contours, and their children, with different colors
out = cv2.cvtColor(blob, cv2.COLOR_GRAY2BGR)
k = -1
for i, cnt in enumerate(contours):
if (hier[0, i, 3] == -1):
k += 1
cv2.drawContours(out, [cnt], -1, colors[k], 2)
cv2.imshow('out', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
当然,获取属于同一个 blob 的所有轮廓可以使用 NumPy 进行优化,但循环在这里感觉最直观。我省略了所有其他内容(skimage、Matplotlib),因为它们在这里似乎不相关。正如我所说,(x, y)
坐标已经存储在 contours
中。
希望对您有所帮助!
编辑: 我还没有验证,如果 OpenCV 总是连续获取属于一个最外层轮廓的所有轮廓,或者如果 - 例如 - 给定层次结构级别的所有轮廓随后存储。因此,对于更复杂的层次结构,应该事先对此进行测试,或者应该从一开始就使用提到的使用 NumPy 查找索引。
关于python - 如何获得带孔的二进制掩码的边界坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58885816/
我想检测(并关闭)封闭网格的开口和孔洞/镂空部分。我知道对于一个开放的网格,这可以通过找到边界边(只属于一个三角形的边)来完成,如 here 所述。 . 但是对于不存在此类边界边的封闭网格呢?下面是一
我试过https://docs.konghq.com/getting-started-guide/2.1.x/expose-services/我也有本地服务器:kong/2.2.1我可以同时注册服务和
我在我的应用程序中使用 logback(SiftingAppender 和 RollingFileAppender)。每当请求进来时,它们将根据 url 查询中的 id 记录到不同的文件中。 这工作得
我在 ARKit 上遇到了一个问题,我需要帮助。我正在做一个小演示,我在场景中放置了一个简单的 SCNTorus 几何体,我试图将一个小球 (SCNSphere) 扔进环面孔中。问题是球在中间弹跳而不
编辑 经过更多研究但仍然没有解决方案,我添加了实质性编辑以及指向 .shp 文件的链接。 The shape file is included here 我有一个包含 9 个多边形的 SpatialP
有没有一种简单直接的方法可以使用 opencv 3.1 python 从图像中提取内部轮廓(孔)? 我知道我可以使用“区域”作为条件。但是,如果我更改图像分辨率,“区域”就不一样了。 例如,这张图片:
我正在审查 Angular 中的模板项目 引导组件有一个如下所示的 HTML 模板。很有道理。应用程序标题、应用程序侧边栏等标签将包含由带有匹配选择器的组件提供的内容。这一切都检查出来了。
案例场景: $ cat Status.txt 1,connected 2,connected 3,connected 4,connected 5,connected 6,connected 7,dis
我是一名优秀的程序员,十分优秀!