import cv2 as cv
import numpy as np
def fill_color_demo(image):
# 彩色图像的填充
copyImg = image.copy()
h, w = image.shape[:2]
mask = np.zeros([h+2, w+2], np.uint8) # mask的大小和数据类型必须是这种
# (30, 30)是图像上某个像素值(的位置),该像素值-100作为最小值,+50作为最大值,将图片上像素值在此范围内的填上(0,255,255)颜色
cv.floodFill(copyImg, mask, (30, 30), (0, 255, 255), (100, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
cv.imshow('fill_color_demo', copyImg)
def fill_binary():
# 二值图像的填充
image = np.zeros([400, 400, 3], np.uint8)
image[100:300, 100:300, :] = 255
cv.imshow('fill_binary', image)
mask = np.ones([402, 402, 1], np.uint8)
mask[101:301, 101:301] = 0
cv.floodFill(image, mask, (200, 200), (0, 0, 255), cv.FLOODFILL_MASK_ONLY)
cv.imshow('fill binary', image)
# mask初始化为1,填充区域初始化为0
# 其他的mask,为1的部分起作用,填充时不为1(为0)的地方起作用
src = cv.imread('C:/Users/Y/Pictures/Saved Pictures/demo.png')
cv.namedWindow('input image', cv.WINDOW_AUTOSIZE)
cv.imshow('input image', src)
fill_binary()
# ROI区域,通过位置指定区域
# face = src[50:250, 100:300]
# # cv.imshow('face', face)
# gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY)
# backface = cv.cvtColor(gray, cv.COLOR_GRAY2BGR)
# src[50:250, 100:300] = backface
# cv.imshow('face', src)
cv.waitKey(0)
cv.destroyAllWindows()