Python_Opencv8_ROI與泛洪處理
import cv2 as cv
import numpy as np
def fill_color_demo(image):#彩色影象填充。泛洪演算法 1.基於掃描的2.基於遞迴的 一般而言基於掃描的速度快
copying=image.copy() #複製一張圖片
h,w=image.shape[:2] #取shape的前兩個值,分別付給h,w.shape[0]=h,shape[1]=w
mask=np.zeros([h+2,w+2],np.uint8) #必須這樣!保證周邊元素都被處理
#從(30,30)開始,填充成(0,255,255),(100,100,100)需要填充的最低畫素值為(30,30)-(100,100),高值(30,30)+(50,50)
cv.floodFill(copying,mask,(30,30),(0,255,255),(100,100,100),(50,50,50),cv.FLOODFILL_FIXED_RANGE)
cv.imshow("fill_color_demo",copying)
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) #需要單通道,8位
mask[101:301,101:301]=0 #需要初始化的地方位1,填充的地方初始化位0
cv.floodFill(image,mask,(200,200),(0,0,255),cv.FLOODFILL_MASK_ONLY)#只有不為1的地方才會被填充
print('---Hello Python-----')
src=cv.imread('E:/aaaxuexi/python_z/zhanglaoshi/lianxi/liuyifei.jpg')
cv.imshow('xiaojiejie',src)
'''
face=src[120:380,230:450] #先是高度關係,後是寬度關係 120-300 230 450
cv.imshow("show",face)
gray=cv.cvtColor(face,cv.COLOR_BGR2GRAY)
backface=cv.cvtColor(gray,cv.COLOR_GRAY2BGR)
src[120:380,230:450]=backface
cv.imshow('face',src)
backface=cv.cvtColor(gray,cv.COLOR_BAYER_GB2RGB)
'''
#fill_color_demo(src)
fill_binary()
cv.waitKey(0)
cv.destroyAllWindows()