我有一个循环遍历图像并每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()从未完成。如果我降低上下限,它可以继续处理,但有时仍会卡住。有没有其他人有这个问题?
这张图片的像素为(40,400):
最佳答案
每次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]
关于python - cv2.floodFill()与某些输入一起挂起,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33464895/