本文实例为大家分享了python3实现证件照背景替换的具体代码,供大家参考,具体内容如下
import cv2import numpy as npimg=cv2.imread('zjz.jpg')#缩放rows,cols,channels = img.shapeimg=cv2.resize(img,None,fx=0.5,fy=0.5)rows,cols,channels = img.shapecv2.imshow('img',img)#转换hsvhsv=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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林站长站。
新闻热点
疑难解答