You get the (x, y)-coordinates directly from cv2.findContours. To identify the single blobs, have a look at the hierarchy hier. The fourth index tells you, to which outer (or parent) contour a possible inner (or child) contour is related. Most outer contours have an index of -1, all other have non-negative values. So, for plotting/drawing, a naive approach would be, while iterating the contours, to increase a blob counter every time you see a -1, and draw all contours with the same color until the next -1 shows.import cv2from skimage import io # Only needed for web grabbing images, use cv2.imread for local images# Read image; find contours with hierarchyblob = io.imread('https://i.stack.imgur.com/Ga5Pe.png')contours, hier = cv2.findContours(blob, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# Define sufficient enough colors for blobscolors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]# Draw all contours, and their children, with different colorsout = cv2.cvtColor(blob, cv2.COLOR_GRAY2BGR)k = -1for 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() 当然,可以使用NumPy优化获取属于同一斑点的所有轮廓,但是在这里循环感觉最直观.我忽略了所有其他内容(skimage,Matplotlib),因为它们在这里似乎并不相关.如我所说,(x, y)坐标已经存储在contours中.Of course, obtaining all contours belonging to the same blob can be optimized using NumPy, but looping feels most intuitive here. I omitted all the other stuff (skimage, Matplotlib), since they didn't seem relevant here. As I said, the (x, y)-coordinates are already stored in contours.希望有帮助! 我尚未验证,如果OpenCV始终连续获取属于一个最外部轮廓的所有轮廓,或者例如(例如)随后存储了给定层次结构级别的所有轮廓.因此,对于更复杂的层次结构,应事先进行测试,或者应从一开始就使用使用NumPy进行的索引查找. I haven't validated, if OpenCV always obtains all contours belonging to one most outer contour continously, or if - for example - all contours for a given hierarchy level are stored subsequently. So, for more complicated hierarchies, this should be tested beforehand, or the mentioned index finding using NumPy should be used right from the start. 这篇关于如何获得带孔的二元掩模的边界坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 09:51