捕捉HSV颜色,提取图片指定颜色
1.捕捉颜色HSV,获取h_min,h_max,s_min,s_max,v_min,v_max,6个参数
import cv2
import numpy as np
def empty(a):
pass
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",96,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",110,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",94,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",241,255,empty)
cv2.createTrackbar("Val Min","TrackBars",23,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)
while True:
img1 = cv2.imread(r"123.png")
img = cv2.resize(img1, (0, 0), None, 0.4, 0.4)
# 色调(H),饱和度(S),明度(V)。
imgHSV = cv2.cvtColor(img1,cv2.COLOR_BGR2HSV)
# 第一个参数是滑动条名字,
# 第二个时所在窗口,
# 返回值是滑动条的数值。
h_min = cv2.getTrackbarPos("Hue Min","TrackBars")
h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
print(h_min,h_max,s_min,s_max,v_min,v_max)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHSV,lower,upper)
mask_contrary = mask.copy()
mask_contrary[mask_contrary == 0] = 1
mask_contrary[mask_contrary == 255] = 0 # 把黑色背景转白色
mask_bool = mask_contrary.astype(bool)
mask_img = cv2.add(img1, np.zeros(np.shape(img1), dtype=np.uint8), mask=mask)
# 这个是把掩模图和原图进行叠加,获得原图上掩模图位置的区域
mask_img = cv2.cvtColor(mask_img, cv2.COLOR_BGR2BGRA)
mask_img[mask_bool] = [255, 255, 255, 255]
cv2.imshow("img", img1)
cv2.imshow("imgHSV", imgHSV)
cv2.imshow("imgResult", mask_img)
cv2.waitKey(1)
2.提取指定HSV颜色
import cv2
import numpy as np
src = cv2.imread(r"123.png")#这里填你的原图像路径
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input", src)
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)#BGR转HSV
# # 红色
# low_hsv = np.array([0, 45, 45])#这里要根据HSV表对应
# high_hsv = np.array([10, 255, 255])#这里填入三个max值
# # 蓝色
# low_hsv = np.array([116, 20, 10])#这里要根据HSV表对应
# high_hsv = np.array([122, 255, 255])#这里填入三个max值
# 黑色
low_hsv = np.array([0, 0, 0])#这里要根据HSV表对应
high_hsv = np.array([180, 255, 46])#这里填入三个max值
# # 黄色
# low_hsv = np.array([20, 25, 25])#这里要根据HSV表对应
# high_hsv = np.array([50, 255, 255])#这里填入三个max值
# # 红色有2个区间,所以可以分别获取后进行合并
# low_hsv = np.array([156, 45, 45]) # 这里要根据HSV表对应
# high_hsv = np.array([180, 255, 255]) # 这里填入三个max值
# mask1 = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv) # 提取掩膜
# mask = cv2.add(mask, mask1)
mask = cv2.inRange(hsv,lowerb=low_hsv,upperb=high_hsv)#提取掩膜
#黑色背景转透明部分
mask_contrary = mask.copy()
mask_contrary[mask_contrary==0]=1
mask_contrary[mask_contrary==255]=0#把黑色背景转白色
mask_bool = mask_contrary.astype(bool)
mask_img = cv2.add(src, np.zeros(np.shape(src), dtype=np.uint8), mask=mask)
#这个是把掩模图和原图进行叠加,获得原图上掩模图位置的区域
mask_img=cv2.cvtColor(mask_img,cv2.COLOR_BGR2BGRA)
mask_img[mask_bool]=[255,255,255,255]
cv2.imshow("image",mask_img)
cv2.imwrite('labe.png',mask_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
标签:颜色,cv2,mask,HSV,max,np,捕捉,hsv,255
From: https://www.cnblogs.com/angelyan/p/16650132.html