python_opencv中的泛洪填充方法
- cv.FLOODFILL_FIXED_RANGE ———— 对图像进行泛洪填充
- cv.FLOODFILL_MASK_ONLY ———— 对mask进行填充
泛洪填充的一些简单介绍
常见的泛洪填充算法有四邻域像素填充,八邻域填充,基于扫描线的像素填充方法;同时又可以分为递归和非递归方法。
基于Python_opencv的代码实现
import cv2 as cv
import numpy as np
#基于彩色图像的泛洪填充
def flood_color_demo(image):
copy_Image = image.copy()
h , w =image.shape[:2]
mask = np.zeros((h+2, w+2), dtype = np.uint8)
cv.floodFill(copy_Image, mask, (100,100),(255,255,0),(100,100,100),(20,20,20),cv.FLOODFILL_FIXED_RANGE)
cv.imshow("flood_color_demo",copy_Image)
def flood_binary_demo()
image = np.zeros((400,400,3), dtype = np.uint8)
image[100:300,100:300,3] = 255
cv.imshow("image ", image)
mask = np.ones((402,402,1), dtype=np.uint8)
mask[101:301,101:301] = 0
cv.floodFill(image,mask,(102,102),(255,255,0),cv.FLOODFILL_MASK_ONLY)
cv.imshow("flood_binary_demo",image)
if __name__ == “__main__”:
src = cv.imread("")
cv.imshow("input_image",src)
flood_color_demo(src)
flood_binary_demo()
cv.waitKey(0)
cv.destroyAllWindows()
实现效果
cv.floodFill()参数介绍
cv.floodFill(image, mask, seedPoint, newVal, loDiff=None, upDiff=None, flags=None)
- image : Input/output 1- or 3-channel, 8-bit, or floating-point image.
It is modified by the function unless the #FLOODFILL_MASK_ONLY flag is set in the second variant of the function. - mask
- seedPoint:Starting point.
- newVal :New value of the repainted domain pixels.
- loDiff : lower brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component.
- upDiff : upper brightness/color difference between the currently observed pixel and one of its neighbors belonging to the component, or a seed pixel being added to the component.
- flags : 官方介绍四邻域像素填充,八邻域填充方法。