【发布时间】:2019-11-01 09:11:57
【问题描述】:
我目前正在尝试在对其应用阈值后在样本映射中找到谷物的轮廓。在图片中,您可以看到,如果这些颗粒彼此非常接近,它们将被检测为单个轮廓。因此,我试图在最薄的部分分割轮廓。然而,我的主要问题是我不知道如何使用 numpy 数组来实际分割轮廓,以便结果是两个数组。 (因为名声问题,我不能放图片,我是新用户)
我尝试创建一个函数,我可以通过 cv2.findContours() 找到的轮廓使用某些标准将它们拆分。我比较了彼此的距离,并用两个因素对其进行了过滤。基本上我得到了两点的两个索引是应该发生分裂(a和b)。现在我希望将它们之间的点移动到新轮廓(数组?)并从旧轮廓中删除这些点。然后将两者都添加到结果中等于 cv2.findContour() 的新列表或数组中。我想我的问题主要是不了解 numpy-type (array) 的 conecpt。
def split(contours, ki = 0.25, ks = 0.25):
contours_new = []
for contour in contours:
dists = []
count = len(contour)
for i in range(0,count-1,1):
for j in range(i+1,count-1,1):
idist = j - i # "index-distance", to exclude neighboring points
if idist > ki*count:
p1 = contour[i][0]
p2 = contour[j][0]
d = dist(p1,p2) #calculates the distance between two points using simple pythagoras
if d <= fit_size(contour)*ks:
dists.append([d, i, j])
#print('{}-{}: {}'.format(i,j,d))
#print(dists)
if len(dists) > 0:
dists = sorted(dists, key=lambda a: a[0])
a = dists[0][1] #first point
b = dists[0][2] # second point, split bewteen these!
old = contour
new = old[a:b] # ????????????
old = np.delete(old,range(a,b,1)) # ????????????
# i = 0
# new = []
# while a < (b - i):
# new.append(old.pop(a))
# i = i + 1
contours_new.append(old)
contours_new.append(new)
else:
contours_new.append(contour)
print('OLD: {} - NEW: {}'.format(len(contours),len(contours_new)))
return contours_new
将结果传递给我的绘图函数(它完美地绘制了 cv2.findContours() 找到的轮廓),确实会导致错误:
错误:(-215:Assertion failed) npoints > 0 in function 'cv::drawContours'
【问题讨论】:
-
表示输入边缘图像不包含图像。进行快速图像检查以验证边缘是否具有非零数据
标签: python arrays numpy opencv contour