import cv2
import numpy as np
green=np.uint8([[[0,255,0]]])
hsv_green=cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
print(hsv_green)
hsv_green = hsv_green[0][0][0]
print(hsv_green)
img = cv2.imread("img/green.jpg")
#转换到HSV
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
#设定绿色的阀值
lower_green = np.array([hsv_green-100,100,100])
upper_green = np.array([hsv_green+100,255,255])
print(lower_green)
print(upper_green)
#根据阀值构建掩模
mask = cv2.inRange(hsv,lower_green,upper_green)
#对原图和掩模进行位运算
res = cv2.bitwise_and(img,img,mask=mask)
#显示图像
cv2.imshow('frame',img)
cv2.imshow('mask',mask)
cv2.imwrite("green_mask.jpg", mask)
cv2.imshow('res',res)
cv2.imwrite("green_result.jpg",res)
cv2.waitKey(0)
原图:
掩模图:
结果图:
参考资料链接:
Datawhale 计算机视觉基础-图像处理(上)-Task03 彩色空间互转
Opencv-Python中文教程之颜色空间转换