我有一个循环来运行图像并每隔20个像素进行一次填充:
h, w = image.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
mask[:] |= 0
flags = 4
flags |= cv2.FLOODFILL_FIXED_RANGE
for x in range(20,image.shape[1]-20):
for y in range(20,image.shape[0]-20):
if x%20 == 0 and y%20 == 0:
print x, y
flooded = image.copy()
print 'starting flood fill'
minVal = min(image.item(x,y,0),image.item(x,y,1),image.item(x,y,2))
maxVal = max(image.item(x,y,0),image.item(x,y,1),image.item(x,y,2))
size = cv2.floodFill(flooded,mask,(x,y),(0,)*3, (40,)*3, (40,)*3, flags)[0]
虽然在某些时候cv2.floodFill()永远不会完成。如果我降低lo和hi界限它可以进一步处理但有时仍会卡住。还有其他人有这个问题吗?
答案 0 :(得分:1)
每次mask
修改时,您应该在循环内重新初始化floodFill
。您还可以通过删除%
运算符来提高性能。并修正了一个错误image.item(x,y,
,它应该是image.item(y,x,
。
for x in range(20,image.shape[1]-20, 20):
for y in range(20,image.shape[0]-20, 20):
print x, y
mask[:] = 0
flooded = image.copy()
print 'starting flood fill'
size = cv2.floodFill(flooded,mask,(x,y),(0,)*3, (40,)*3, (40,)*3, flags)[0]
答案 1 :(得分:0)
我找到了你挂的真正原因。
事实证明,lo和hi界限不得使目标点的采样颜色超过254.
target_point = (25, 50)
size = cv2.floodFill(flooded,mask, target_point,(0,)*3, (40,)*3, (40,)*3, flags)[0]
如果(25,50)的值为253,则lo和hi界限的最大值为1.
我使用此代码修复它,只检查以确保边界不会导致值超过254.
val = img_f.item(target_point[1], target_point[0])
bounds = min(254-val, INSERT_LO_HI_BOUND_HERE)