一、色彩空间
1、什么是色彩空间?
色彩空间是定义的颜色范围。
2、常见的色彩空间有哪些?
①RGB
②HSV
在OpenCV中,Hue的值为0~180°,之所以不是360°是因为,8位存不下,故进行归一化操作,使得Hue取值在0 ~180°,可以通过一个字节来存储。
③HIS
Hue:色调
Intensity:强度
Saturation :饱和度
④YCrCb
主要在早期的人的皮肤检测方面应用
⑤YUV
Android开发中常用,Linux的色彩空间
3、色彩空间之间的相互转化
import cv2def color_space(image):gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)cv2.imshow("gray",gray)hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)cv2.imshow("hsv",hsv)yuv = cv2.cvtColor(image,cv2.COLOR_BGR2YUV)cv2.imshow("yuv",yuv)ycrcb = cv2.cvtColor(image,cv2.COLOR_BGR2YCrCb)cv2.imshow("ycrcb",ycrcb)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
color_space(src)
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
运行效果图如下:
二、对视频中的目标(青色水杯)进行跟踪
cv2.inRange(hsv,lowerb=lower,upperb=upper)
:对HSV颜色过滤,lowerb和upperb需要查上面的表进行颜色转换
方法一:目标物体白色,其余黑色
import cv2
import numpy as npdef extrace_object():capture = cv2.VideoCapture("G:/Juptyer_workspace/study/data/yy.mp4")while(True):ret,frame = capture.read()if ret==False:breakhsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)lower = np.array([78,43,46])#由于水杯是青色的故需要根据表格选出hsv对应的min值upper = np.array([99,255,255])#由于水杯是青色的故需要根据表格选出hsv对应的max值mask = cv2.inRange(hsv,lowerb=lower,upperb=upper)cv2.imshow("video",frame)cv2.imshow("mask",mask)c = cv2.waitKey(40)if c==27:breakt1 = cv2.getTickCount()
extrace_object()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下:
方法二:目标物体颜色保留,其余黑色
import cv2
import numpy as npdef extrace_object():capture = cv2.VideoCapture("G:/Juptyer_workspace/study/data/yy.mp4")while(True):ret,frame = capture.read()if ret==False:breakhsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)lower = np.array([78,43,46])#由于水杯是青色的故需要根据表格选出hsv对应的min值upper = np.array([99,255,255])#由于水杯是青色的故需要根据表格选出hsv对应的max值mask = cv2.inRange(hsv,lowerb=lower,upperb=upper)dst = cv2.bitwise_and(frame,frame,mask=mask)cv2.imshow("video",frame)#cv2.imshow("mask",mask)cv2.imshow("dst",dst)c = cv2.waitKey(40)if c==27:breakt1 = cv2.getTickCount()
extrace_object()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下:
三、对彩色图像三颜色通道进行分割显示并合并显示
cv2.split(src)
:对src三颜色通道的彩色图像进行分割
cv2.merge([b,g,r])
:将b,g,r三个单颜色通道图像进行合并
import cv2
import numpy as npsrc = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()b,g,r = cv2.split(src)#拆分彩色通道图像
cv2.imshow("blue",b)
cv2.imshow("green",g)
cv2.imshow("red",r)src = cv2.merge([b,g,r])#将拆分的三个单颜色通道进行合并还原
src[:,:,0]=0#将第0个颜色通道,即B颜色通道赋值为0
cv2.imshow("changed",src)t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下: