: range of interest
import cv2 as cv
import numpy as np
def pro(image): #相当于抠图改改再放回去。。
flag = image[175:335,130:190]
gray = cv.cvtColor(flag,cv.COLOR_BGR2GRAY)
new_flag = cv.cvtColor(gray,cv.COLOR_GRAY2BGR)
image[175:335, 130:190] = new_flag
cv.imshow("new",image)
def fill_color(image):
copy_image = image.copy()
h ,w =image.shape[:2]
mask = np.zeros([h+2,w+2],np.uint8) #+2为要求
cv.floodFill(copy_image, mask, (150,320), (0,255,255), (100,100,100), (50,50,50), cv.FLOODFILL_FIXED_RANGE)
cv.imshow("filled",copy_image)
mask:掩模 ,应为 单通道,8位,[h+2,w+2]
cv.floodFill(copy_image, mask, (150,320), (0,255,255), (100,100,100),
(50,50,50), cv.FLOODFILL_FIXED_RANGE)
(150,320): seedpoint,填充起始处
(0,255,255):填充后的颜色
(100,100,100),(50,50,50):与seedpoint处像素值相比,被填充点的像素满足条件,浮动范围的下、上限
cv.FLOODFILL_FIXED_RANGE:flag,表示将符合要求的点全填充
即:cv.floodFill(原图, mask, seedpoint, 目标颜色, 浮动下限,
浮动上限,cv.FLOODFILL_FIXED_RANGE)
def mask_fill():
image = np.zeros([400,400,3],np.uint8)
image[100:300,100:300,:] = 255
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("mask_filled",image)
填充mask=0的区域
cv.floodFill(原图,mask,seedpoint, 目标颜色,cv.FLOODFILL_MASK_ONLY)
#seedpoint需在mask=0的范围内
print("---------Hello Python---------")
src = cv.imread("D:/sourses/picture/001.jpg")
cv.namedWindow("image", cv.WINDOW_NORMAL)
cv.imshow("image", src)
mask_fill()
cv.waitKey(0)
cv.destroyAllWindows()