Skip to content

Latest commit

 

History

History
274 lines (217 loc) · 6.53 KB

613-1189946-HSV色彩模式.sy.md

File metadata and controls

274 lines (217 loc) · 6.53 KB
show version enable_checker
step
1.0
true

opencv

回忆

  • 上次我们研究了滚动条
  • 可以控制画面不同的
    • 文本内容
    • 文本位置
    • 具体颜色
  • 还可以有什么样具体的应用吗?

色彩模式转化

import cv2                                                                    
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv_image",hsv_image)
print(hsv_image)
cv2.waitKey()
cv2.destroyAllWindows()
  • 运行结果

图片描述

  • 看起来HSV的现实结果很混乱
  • 究竟什么是HSV?

分通道显示hsv

import cv2
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image)
cv2.imshow("hue",h)
cv2.imshow("saturation",s)
cv2.imshow("velocity",v)
cv2.waitKey()                                                       
cv2.destroyAllWindows()
  • 显示结果

图片描述

简写 名称 含义
H Hue 色相
S Saturation 饱和度
V Value 亮度

转化色相hue

import cv2
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image)
h[:,:] = 180
hsv = cv2.merge([h,s,v])
new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow("new_image",new_image)
cv2.waitKey()
cv2.destroyAllWindows()
  • 设置色相为180

图片描述

设置色相

import cv2                   
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)    
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image) 
print(h)                     
h[:,:] = (h[:,:] + 90) % 180
hsv = cv2.merge([h,s,v])     
new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow("new_image",new_image)
cv2.waitKey()                
cv2.destroyAllWindows()
  • 设置色相为180度后反相

图片描述

  • 饱和度saturation可以控制吗?

转化饱和度saturation

import cv2
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image)
s[:,:] = 100
hsv = cv2.merge([h,s,v])
new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow("s = 100,new_image)
h,s,v = cv2.split(hsv_image)
s[:,:] = 0
hsv = cv2.merge([h,s,v])
new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow("s = 0",new_image)
cv2.waitKey()
cv2.destroyAllWindows()
  • 结果

图片描述

  • 明度可以转化吗?

转化明度velocity

import cv2 
image = cv2.imread("/home/shiyanlou/gourd.png")
cv2.imshow("image",image)
hsv_image = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image)
v[:,:] = 255
hsv = cv2.merge([h,s,v])                                            
new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
cv2.imshow("new_image",new_image)
cv2.waitKey()
cv2.destroyAllWindows()

根据滑杆控制HSV

import cv2
import numpy as np

#trackbar callback fucntion to update HSV value
def callback(x):
    S = cv2.getTrackbarPos('S','controls')
    print("S========",type(S),S)
    #read source image
    img = cv2.imread("/home/shiyanlou/gourd.png")
    #convert sourece image to HSC color mode
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h,s,v = cv2.split(hsv)
    s[:,:] = S
    cv2.imshow('img',img)
    hsv = cv2.merge([h,s,v])
    new_image = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    cv2.imshow('controls',new_image)

#create a seperate window named 'controls' for trackbar
cv2.namedWindow('controls',2)
cv2.resizeWindow("controls", 550,200);

#create trackbars for high,low H,S,V
cv2.createTrackbar('S','controls',0,255,callback)

while(True):

    #waitfor the user to press escape and break the while loop
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
#destroys all window
cv2.destroyAllWindows()
  • 运行结果

图片描述

根据HSV控制选区范围

import cv2
import numpy as np

#trackbar callback fucntion to update HSV value
def callback(x):
    global H_low,H_high,S_low,S_high,V_low,V_high
    H_low = cv2.getTrackbarPos('low H','controls')
    H_high = cv2.getTrackbarPos('high H','controls')
    S_low = cv2.getTrackbarPos('low S','controls')
    S_high = cv2.getTrackbarPos('high S','controls')
    V_low = cv2.getTrackbarPos('low V','controls')
    V_high = cv2.getTrackbarPos('high V','controls')


#create a seperate window named 'controls' for trackbar
cv2.namedWindow('controls',2)
cv2.resizeWindow("controls", 550,200);

#global variable
H_low = 0
H_high = 179
S_low= 0
S_high = 255
V_low= 0
V_high = 255

#create trackbars for high,low H,S,V
cv2.createTrackbar('low H','controls',0,179,callback)
cv2.createTrackbar('high H','controls',179,179,callback)

cv2.createTrackbar('low S','controls',0,255,callback)
cv2.createTrackbar('high S','controls',255,255,callback)

cv2.createTrackbar('low V','controls',0,255,callback)
cv2.createTrackbar('high V','controls',255,255,callback)


while(1):
    #read source image
    img = cv2.imread("/home/shiyanlou/gourd.png")
    #convert sourece image to HSC color mode
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    #
    hsv_low = np.array([H_low, S_low, V_low], np.uint8)
    hsv_high = np.array([H_high, S_high, V_high], np.uint8)

    #making mask for hsv range
    mask = cv2.inRange(hsv, hsv_low, hsv_high)
    print (mask)
    #masking HSV value selected color becomes black
    res = cv2.bitwise_and(img, img, mask=mask)

    #show image
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)

    #waitfor the user to press escape and break the while loop
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
#destroys all window
cv2.destroyAllWindows()
  • 最终效果

图片描述

总结

  • 这次研究的是HSV
  • HSV是一种色彩模式
简写 名称 含义
H Hue 色相
S Saturation 饱和度
V Value 亮度
  • 可以通过滑块对于HSV的图像进行控制
  • 还有什么可以玩的东西吗?🤔
  • 我们下次再说 👋