来自 Numpy 文档:
>>> a = np.arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])
>>> a[ : :-1] # reversed a
array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, …Run Code Online (Sandbox Code Playgroud) 这是一个显示cv2.floodfill函数用法的示例代码
import cv2
import numpy as np
import os
def imshow(img):
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('test4.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,3,1)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
mask = np.zeros(img.shape[:-1],np.uint8)
cv2.drawContours(mask,contours,-1,(255,255,255),-1)
height, width = img.shape[:-1]
mask1 = np.zeros((height+2, width+2), np.uint8) # line 26
cv2.floodFill(mask,mask1,(0,0),255) # line 27
mask_inv=cv2.bitwise_not(mask)
imshow(mask_inv)
Run Code Online (Sandbox Code Playgroud)
我在我的一个项目中使用此函数但我不理解代码的mask1部分(第26行和第27行),
为什么我们为高度为'-h'和宽度为'w'的给定图像创建形状为h + 2,w + 2的mask1?(第26行)
为什么我们必须将这个mask1传递给cv2.floodfill函数?(第27行)
这是示例代码的输入和输出.
请帮忙