# -*- coding: utf-8 -*-
"""
污水颜色识别
"""
import cv2
import numpy as np
img = cv2.imread("wushui.jpg")
#缩放
row,col,chanel = img.shape
img = cv2.resize(img,None,fx= 0.5,fy = 0.5)
row,col,channle = img.shape
cv2.imshow("img",img)
#颜色转HSV
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue =np.array([0,0,0]) #下界
upper_blue = np.array([180,60,120]) #上界
mask = cv2.inRange(hsv,lower_blue,upper_blue)
cv2.imshow("mask",mask)
#腐蚀膨胀
erod = cv2.erode(mask,None,iterations =1)
cv2.imshow('erod',erod)
dilate = cv2.dilate(erod,None,iterations =1)
cv2.imshow("dilate",dilate)
#遍历替换
for i in range(row):
for j in range(col):
if dilate[i,j] == 255:
img[i,j] = (0,0,255) #标记成红色
cv2.imshow("res",img)
cv2.imwrite("white-dog.jpg",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
右图中标记为红色的是识别污水的结果,可以调整黑色的范围。需要解决由于水面有反光的问题。