本文實例為大家分享了python3實現(xiàn)證件照背景替換的具體代碼,供大家參考,具體內(nèi)容如下
從策劃到設計制作,每一步都追求做到細膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站建設、成都網(wǎng)站建設、網(wǎng)站策劃、網(wǎng)頁設計、空間域名、網(wǎng)絡空間、網(wǎng)絡營銷、VI設計、 網(wǎng)站改版、漏洞修補等服務。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進步。import cv2 import numpy as np img=cv2.imread('zjz.jpg') #縮放 rows,cols,channels = img.shape img=cv2.resize(img,None,fx=0.5,fy=0.5) rows,cols,channels = img.shape cv2.imshow('img',img) #轉(zhuǎn)換hsv hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) lower_blue=np.array([78,43,46]) upper_blue=np.array([110,255,255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) cv2.imshow('Mask', mask) #腐蝕膨脹 erode=cv2.erode(mask,None,iterations=1) cv2.imshow('erode',erode) dilate=cv2.dilate(erode,None,iterations=1) cv2.imshow('dilate',dilate) #遍歷替換 for i in range(rows): for j in range(cols): if dilate[i,j]==255: img[i,j]=(0,0,255)#此處替換顏色,為BGR通道 cv2.imshow('res',img) cv2.waitKey(0) cv2.destroyAllWindows()