目录
1 转换颜色空间 2 物体跟踪 附录
OpenCV中有多种色彩空间,包括 RGB、HSI、HSL、HSV、HSB、YCrCb、CIE XYZ、CIE Lab8种,在图像处理中常需要转换色彩空间以便进行其他操作。
cv2.cvtColor(src,code,dst,dstCn)
2.x版本和3.x版本的code表示方法不同:
code可通过以下代码获取:
import cv2
import numpy as np
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
print(flags)
结果如下,只截取了部分,共150多种
也可以用数字代替,由于数量太多所以放在博文最后的附录中。
举一个例子:
import matplotlib.pyplot as plt
import cv2
img_BGR = cv2.imread('test.jpg') # BGR
plt.subplot(331), plt.imshow(img_BGR), plt.axis('off'), plt.title('BGR')
img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
plt.subplot(332), plt.imshow(img_RGB), plt.axis('off'), plt.title('RGB')
img_GRAY = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2GRAY)
plt.subplot(333), plt.imshow(img_GRAY), plt.axis('off'), plt.title('GRAY')
img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)
plt.subplot(334), plt.imshow(img_HSV), plt.axis('off'), plt.title('HSV')
img_YcrCb = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YCrCb)
plt.subplot(335), plt.imshow(img_YcrCb), plt.axis('off'), plt.title('YcrCb')
img_HLS = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HLS)
plt.subplot(336), plt.imshow(img_HLS), plt.axis('off'), plt.title('HLS')
img_XYZ = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2XYZ)
plt.subplot(337), plt.imshow(img_XYZ), plt.axis('off'), plt.title('XYZ')
img_LAB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2LAB)
plt.subplot(338), plt.imshow(img_LAB), plt.axis('off'), plt.title('LAB')
img_YUV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2YUV)
plt.subplot(339), plt.imshow(img_YUV), plt.axis('off'), plt.title('YUV')
plt.show()
cv2.imshow('gray', img_GRAY)
k = cv2.waitKey(0) # 始终检测键盘
if k == 27: # 按ESC退出
cv2.destroyAllWindows()
结果如下:
转换为灰度结果如下:
HSV空间比在BGR空间中更容易表示一个特定的颜色。现在要提取蓝色物体 。
注意:HSV空间中,H表示色彩/色度,取值范围 [0,179],S表示饱和度,取值范围 [0,255],V表示亮度,取值范围 [0,255]。但是不同的软件使用值不同
步骤如下
cv2.inRange(src, lowerb, upperb, dst)
例如:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
ret, frame = cap.read() # 读取视频帧
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 转换为HSV空间
# 注意是HSV参数,HSV空间中,H表示色调,取值范围 [0,179],S表示饱和度,取值范围 [0,255],V表示亮度,取值范围 [0,255]
lower_blue = np.array([80, 50, 0]) # 设定蓝色的阈值
upper_blue = np.array([130, 255, 255])
mask = cv2.inRange(hsv, lower_blue, upper_blue) # 设定取值范围
res = cv2.bitwise_and(frame, frame, mask=mask) # 对原图像处理
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('res', res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
结果如下,依次是最终结果,掩膜,原图:
那么如何获得所需颜色的HSV值呢?
首先常用颜色可以通过下表获得:
其次也可以通过以下程序获得:
例如获得绿色:
import cv2
import numpy as np
green = np.uint8([[[0, 255, 0]]])
hsv_green = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print(hsv_green)
结果如下
最后用 [H-10, 100,100] 和 [H+10, 255, 255] 做阈值上下限。也可以使用其他程序处理,推荐一个手机APP,叫颜色设计工具。
同时提取红绿蓝三种颜色;
import cv2
import numpy as np
img = cv2.imread("color1.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 转换为HSV空间
lower_blue = np.array([110, 100, 100]) # 蓝色阈值下限
upper_blue = np.array([130, 255, 255]) # 蓝色阈值上限
lower_green = np.array([60, 100, 100]) # 绿色阈值下限
upper_green = np.array([70, 255, 255]) # 绿色阈值上限
lower_red = np.array([0, 100, 100]) # 红色阈值下限
upper_red = np.array([10, 255, 255]) # 红色阈值上限
red_mask = cv2.inRange(hsv, lower_red, upper_red) # 红色掩膜
blue_mask = cv2.inRange(hsv, lower_blue, upper_blue) # 蓝色掩膜
green_mask = cv2.inRange(hsv, lower_green, upper_green) # 绿色掩膜
red = cv2.bitwise_and(img, img, mask=red_mask) # 对原图像处理
green = cv2.bitwise_and(img, img, mask=green_mask) # 对原图像处理
blue = cv2.bitwise_and(img, img, mask=blue_mask) # 对原图像处理
dst = green+red+blue # 三个图像求和
cv2.imshow('original', img)
cv2.imshow('result', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果如下:
code对应的数字
COLOR_BGR2BGRA =0,
COLOR_RGB2RGBA =COLOR_BGR2BGRA,
COLOR_BGRA2BGR =1,
COLOR_RGBA2RGB =COLOR_BGRA2BGR,
COLOR_BGR2RGBA =2,
COLOR_RGB2BGRA =COLOR_BGR2RGBA,
COLOR_RGBA2BGR =3,
COLOR_BGRA2RGB =COLOR_RGBA2BGR,
COLOR_BGR2RGB =4,
COLOR_RGB2BGR =COLOR_BGR2RGB,
COLOR_BGRA2RGBA =5,
COLOR_RGBA2BGRA =COLOR_BGRA2RGBA,
COLOR_BGR2GRAY =6,
COLOR_RGB2GRAY =7,
COLOR_GRAY2BGR =8,
COLOR_GRAY2RGB =COLOR_GRAY2BGR,
COLOR_GRAY2BGRA =9,
COLOR_GRAY2RGBA =COLOR_GRAY2BGRA,
COLOR_BGRA2GRAY =10,
COLOR_RGBA2GRAY =11,
COLOR_BGR2BGR565 =12,
COLOR_RGB2BGR565 =13,
COLOR_BGR5652BGR =14,
COLOR_BGR5652RGB =15,
COLOR_BGRA2BGR565 =16,
COLOR_RGBA2BGR565 =17,
COLOR_BGR5652BGRA =18,
COLOR_BGR5652RGBA =19,
COLOR_GRAY2BGR565 =20,
COLOR_BGR5652GRAY =21,
COLOR_BGR2BGR555 =22,
COLOR_RGB2BGR555 =23,
COLOR_BGR5552BGR =24,
COLOR_BGR5552RGB =25,
COLOR_BGRA2BGR555 =26,
COLOR_RGBA2BGR555 =27,
COLOR_BGR5552BGRA =28,
COLOR_BGR5552RGBA =29,
COLOR_GRAY2BGR555 =30,
COLOR_BGR5552GRAY =31,
COLOR_BGR2XYZ =32,
COLOR_RGB2XYZ =33,
COLOR_XYZ2BGR =34,
COLOR_XYZ2RGB =35,
COLOR_BGR2YCrCb =36,
COLOR_RGB2YCrCb =37,
COLOR_YCrCb2BGR =38,
COLOR_YCrCb2RGB =39,
COLOR_BGR2HSV =40,
COLOR_RGB2HSV =41,
COLOR_BGR2Lab =44,
COLOR_RGB2Lab =45,
COLOR_BayerBG2BGR =46,
COLOR_BayerGB2BGR =47,
COLOR_BayerRG2BGR =48,
COLOR_BayerGR2BGR =49,
COLOR_BayerBG2RGB =COLOR_BayerRG2BGR,
COLOR_BayerGB2RGB =COLOR_BayerGR2BGR,
COLOR_BayerRG2RGB =COLOR_BayerBG2BGR,
COLOR_BayerGR2RGB =COLOR_BayerGB2BGR,
COLOR_BGR2Luv =50,
COLOR_RGB2Luv =51,
COLOR_BGR2HLS =52,
COLOR_RGB2HLS =53,
COLOR_HSV2BGR =54,
COLOR_HSV2RGB =55,
COLOR_Lab2BGR =56,
COLOR_Lab2RGB =57,
COLOR_Luv2BGR =58,
COLOR_Luv2RGB =59,
COLOR_HLS2BGR =60,
COLOR_HLS2RGB =61,
COLOR_BayerBG2BGR_VNG =62,
COLOR_BayerGB2BGR_VNG =63,
COLOR_BayerRG2BGR_VNG =64,
COLOR_BayerGR2BGR_VNG =65,
COLOR_BayerBG2RGB_VNG =COLOR_BayerRG2BGR_VNG,
COLOR_BayerGB2RGB_VNG =COLOR_BayerGR2BGR_VNG,
COLOR_BayerRG2RGB_VNG =COLOR_BayerBG2BGR_VNG,
COLOR_BayerGR2RGB_VNG =COLOR_BayerGB2BGR_VNG,
COLOR_BGR2HSV_FULL = 66,
COLOR_RGB2HSV_FULL = 67,
COLOR_BGR2HLS_FULL = 68,
COLOR_RGB2HLS_FULL = 69,
COLOR_HSV2BGR_FULL = 70,
COLOR_HSV2RGB_FULL = 71,
COLOR_HLS2BGR_FULL = 72,
COLOR_HLS2RGB_FULL = 73,
COLOR_LBGR2Lab = 74,
COLOR_LRGB2Lab = 75,
COLOR_LBGR2Luv = 76,
COLOR_LRGB2Luv = 77,
COLOR_Lab2LBGR = 78,
COLOR_Lab2LRGB = 79,
COLOR_Luv2LBGR = 80,
COLOR_Luv2LRGB = 81,
COLOR_BGR2YUV = 82,
COLOR_RGB2YUV = 83,
COLOR_YUV2BGR = 84,
COLOR_YUV2RGB = 85,
COLOR_BayerBG2GRAY = 86,
COLOR_BayerGB2GRAY = 87,
COLOR_BayerRG2GRAY = 88,
COLOR_BayerGR2GRAY = 89,
//YUV 4:2:0 formats family
COLOR_YUV2RGB_NV12 = 90,
COLOR_YUV2BGR_NV12 = 91,
COLOR_YUV2RGB_NV21 = 92,
COLOR_YUV2BGR_NV21 = 93,
COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
COLOR_YUV2RGBA_NV12 = 94,
COLOR_YUV2BGRA_NV12 = 95,
COLOR_YUV2RGBA_NV21 = 96,
COLOR_YUV2BGRA_NV21 = 97,
COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
COLOR_YUV2RGB_YV12 = 98,
COLOR_YUV2BGR_YV12 = 99,
COLOR_YUV2RGB_IYUV = 100,
COLOR_YUV2BGR_IYUV = 101,
COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
COLOR_YUV2RGBA_YV12 = 102,
COLOR_YUV2BGRA_YV12 = 103,
COLOR_YUV2RGBA_IYUV = 104,
COLOR_YUV2BGRA_IYUV = 105,
COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
COLOR_YUV2GRAY_420 = 106,
COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
//YUV 4:2:2 formats family
COLOR_YUV2RGB_UYVY = 107,
COLOR_YUV2BGR_UYVY = 108,
//COLOR_YUV2RGB_VYUY = 109,
//COLOR_YUV2BGR_VYUY = 110,
COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
COLOR_YUV2RGBA_UYVY = 111,
COLOR_YUV2BGRA_UYVY = 112,
//COLOR_YUV2RGBA_VYUY = 113,
//COLOR_YUV2BGRA_VYUY = 114,
COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
COLOR_YUV2RGB_YUY2 = 115,
COLOR_YUV2BGR_YUY2 = 116,
COLOR_YUV2RGB_YVYU = 117,
COLOR_YUV2BGR_YVYU = 118,
COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
COLOR_YUV2RGBA_YUY2 = 119,
COLOR_YUV2BGRA_YUY2 = 120,
COLOR_YUV2RGBA_YVYU = 121,
COLOR_YUV2BGRA_YVYU = 122,
COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
COLOR_YUV2GRAY_UYVY = 123,
COLOR_YUV2GRAY_YUY2 = 124,
//COLOR_YUV2GRAY_VYUY = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
// alpha premultiplication
COLOR_RGBA2mRGBA = 125,
COLOR_mRGBA2RGBA = 126,
COLOR_RGB2YUV_I420 = 127,
COLOR_BGR2YUV_I420 = 128,
COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
COLOR_RGBA2YUV_I420 = 129,
COLOR_BGRA2YUV_I420 = 130,
COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
COLOR_RGB2YUV_YV12 = 131,
COLOR_BGR2YUV_YV12 = 132,
COLOR_RGBA2YUV_YV12 = 133,
COLOR_BGRA2YUV_YV12 = 134,
COLOR_COLORCVT_MAX = 135