查找带有孔的多个对象的轮廓 [英] Find contour of multiple objects with their holes
问题描述
这是来自以下问题的后续问题:如何获得带孔的二元掩模的边界坐标?
This is a follow up question from: How to obtain boundary coordinates of binary mask with holes?
给出相同的图像:
我想为每个对象的外部轮廓及其内部轮廓的(x, y)
坐标获取单独的列表.理想情况下,我想使用此列表在单独的空白画布上绘制对象(外部轮廓和内部轮廓).
I want to get a separate list for each object of (x, y)
-coordinates of the outer contour and its inner contour. Ideally, I want to use this list to plot the object (outer and inner contour) on a separate blank canvas.
import matplotlib.pyplot as plt # For plotting
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('https://i.stack.imgur.com/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)
# Check if it's the outer contour
k = -1
# Preallocate list
obj_list = []
for i, cnt in enumerate(contours):
if (hier[0, i, 3] == -1):
k += 1
# cv2.drawContours(out, [cnt], -1, colors[k], 2)
# Add contour list to object list if it is an inner contour
obj_list.extend([cnt])
# Concatenate array in list
obj_list = np.vstack(obj_list)
obj_list = np.squeeze(obj_list)
x = obj_list[:,0].tolist()
y = obj_list[:,1].tolist()
cv2.imshow('out', out)
cv2.waitKey(0)
cv2.destroyAllWindows()
可接受的答案仅适用于具有内部轮廓的对象,不适用于没有轮廓的对象.我试图通过添加以下代码来修复它:
The accepted answer only works with objects with inner contours but not for objects without. I tried to fix it by adding the following code:
# Add inner contours of blob to list
cnt_idx = np.squeeze(np.where(hier[0, :, 3] == b_idx))
c_cnt_idx = np.array(cnt_idx)
if c_cnt_idx.size > 0:
cnt_idx = b_idx
但是我收到以下错误消息:
but I received the following error message:
ValueError:大小为零的操作数的迭代未启用
ValueError: Iteration of zero-sized operands is not enabled
推荐答案
然后,我还将回答这个问题.同样,我跳过了整个绘图部分.而且,正如我先前的回答中所建议的那样,我修改了斑点的发现,以便事先使用NumPy找到正确的斑点索引"(相对于层次结构).
Then I will also answer this question. Again, I skipped the whole plotting part. And, as suggested in my earlier answer, I modified the finding of the blobs, such that the proper "blob indices" (with respect to the hierarchy) are found beforehand using NumPy.
以下是修改后的代码:
import cv2
import numpy as np
from skimage import io # Only needed for web grabbing images, use cv2.imread for local images
# Read image; add an additional hole; find contours with hierarchy
blob = io.imread('https://i.stack.imgur.com/Ga5Pe.png')
cv2.circle(blob, (380, 120), 25, 0, cv2.FILLED)
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)]
# Get blob indices with respect to hierarchy
blob_idx = np.squeeze(np.where(hier[0, :, 3] == -1))
# Initialize blob images
blob_imgs = []
# Iterate all blobs
k = 0
for b_idx in np.nditer(blob_idx):
# Add outer contour of blob to list
blob_cnts = [contours[b_idx]]
# Add inner contours of blob to list, if present
cnt_idx = np.squeeze(np.where(hier[0, :, 3] == b_idx))
if (cnt_idx.size > 0):
blob_cnts.extend([contours[c_idx] for c_idx in np.nditer(cnt_idx)])
# Generate blank BGR image with same size as input; draw contours
img = np.zeros((blob.shape[0], blob.shape[1], 3), np.uint8)
cv2.drawContours(img, blob_cnts, -1, colors[k], 2)
blob_imgs.append(img)
k += 1
# Just for visualization: Iterate all blob images
k = 0
for img in blob_imgs:
cv2.imshow(str(k), img)
k += 1
cv2.waitKey(0)
cv2.destroyAllWindows()
生成了两个输出(我在一个斑点中添加了另一个孔以检查多个内部轮廓):
The two outputs generated (I added another hole in one of the blobs to check for multiple inner contours):
因此,在主循环内,现在将属于一个斑点的所有轮廓存储在blob_cnts
中,再次作为(x, y)
坐标的列表.因此,您可以生成图或做任何您想做的事情,而不是生成此处所示的不同图像.
So, inside the main loop, you now have all contours belonging to one blob stored in blob_cnts
, again as lists of (x, y)
-coordinates. So, instead of generating different images as shown here, you can generate plots or do whatever you like.
希望有帮助!
这篇关于查找带有孔的多个对象的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!