1. R(红色,0-255) G(绿色,0-255) B(蓝色,0-255)
2. H(色调,0-360) S(饱和度,0-255) V(明度,0-255) OpenCV H(0-180)
3. H(色调) S(饱和度) I(灰度级别,亮度)
4. Y(明亮度) Cr(色度) Cb(浓度)
5. Y U V
def color_space_demo(image):
"""OpenCV色彩空间转换API"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("hsv", hsv)
# yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
# cv.imshow("yuv", yuv)
# ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)
# cv.imshow("ycrcb", ycrcb)
def extract_object_demo():
"""OpenCV inRange API提取指定颜色"""
capture = cv.VideoCapture('E:/MyFile/Picture/date/videodemo.mp4')
while True:
ret, frame = capture.read()
if ret is False:
break
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # 转换到HSV空间
# 查表设定红色三通道阈值
lower_hsv = np.array([156, 43, 46])
upper_hsv = np.array([180, 255, 255])
# inRange API进行颜色提取
extract_red = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
cv.imshow("video demo", frame)
cv.imshow("extract_red demo", extract_red)
key = cv.waitKey(40)
if key == 27:
break
# split分离指定对象通道
b, g, r = cv.split(img)
cv.imshow("split blue", b)
cv.imshow("split green", g)
cv.imshow("split red", r)
# img[:, :, 2] = 0 # 修改红色通道值
# merge融合指定通道
merger = [b, g, r]
img_merge = cv.merge(merger)
cv.imshow("merge image", img_merge)
# -*- coding: utf-8 -*-
# By:iloveluoluo
# 2019.3.22
import cv2 as cv
import numpy as np
# R(红色,0-255) G(绿色,0-255) B(蓝色,0-255)
# H(色调,0-360) S(饱和度,0-255) V(明度,0-255) OpenCV H(0-180)
# H(色调) S(饱和度) I(灰度级别,亮度)
# Y(明亮度) Cr(色度) Cb(浓度)
# Y U V
def get_image_info(image):
"""属性读取"""
# print(image.shape) # (高,宽,通道数)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2] # Blue, Green, Red
print("height:%s, width:%s, channels:%s, pixels:%s" % (height, width, channels, image.size))
def color_space_demo(image):
"""OpenCV色彩空间转换API"""
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("hsv", hsv)
# yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
# cv.imshow("yuv", yuv)
# ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)
# cv.imshow("ycrcb", ycrcb)
def extract_object_demo():
"""OpenCV inRange API提取指定颜色"""
capture = cv.VideoCapture('E:/MyFile/Picture/date/videodemo.mp4')
while True:
ret, frame = capture.read()
if ret is False:
break
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # 转换到HSV空间
# 查表设定红色三通道阈值
lower_hsv = np.array([156, 43, 46])
upper_hsv = np.array([180, 255, 255])
# inRange API进行颜色提取
extract_red = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
cv.imshow("video demo", frame)
cv.imshow("extract_red demo", extract_red)
key = cv.waitKey(40)
if key == 27:
break
img = cv.imread('E:/MyFile/Picture/date/lenademo.png') # 读取图片
get_image_info(img)
# split分离指定对象通道
b, g, r = cv.split(img)
cv.imshow("split blue", b)
cv.imshow("split green", g)
cv.imshow("split red", r)
# img[:, :, 2] = 0 # 修改红色通道值
# merge融合指定通道
merger = [b, g, r]
img_merge = cv.merge(merger)
cv.imshow("merge image", img_merge)
# color_space_demo(img) # 色彩空间转换
# extract_object_demo() # 提取视频颜色
# cv.imshow("image", img)
cv.waitKey(0)
cv.destroyAllWindows()