OpenCV | OpenCV 图片换底色

环境

pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16

示例代码

import cv2
import  numpy as np

img=cv2.imread('molecule.png')
#缩放
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)

#转换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)
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()

原图

OpenCV | OpenCV 图片换底色_第1张图片

输出图

OpenCV | OpenCV 图片换底色_第2张图片

 

你可能感兴趣的:(#,OpenCV,Image,and,Vision)