使用opencv将蓝底照片换成白底照片
import cv2
import numpy as np
img = cv2.imread("blue_img.png")
#缩放
row,col,chanel = img.shape
img = cv2.resize(img,None,fx= 1,fy = 1)
row,col,channle = img.shape
#颜色转HSV
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue =np.array([90,70,70]) #蓝色下界
upper_blue = np.array([110,255,255]) #蓝色上界
mask = cv2.inRange(hsv,lower_blue,upper_blue)
#腐蚀膨胀
erod = cv2.erode(mask,None,iterations =1)
dilate = cv2.dilate(erod,None,iterations =1)
#遍历替换
for i in range(row):
for j in range(col):
if dilate[i,j] == 255:
img[i,j] = (255,255,255) #白色
# 保存图像
cv2.imwrite("white_img.png",img)