Python-OpenCV中cv2.cvtColor的使用


? 本文主要记录Python-OpenCV中cv2.cvtColor()的使用;


cv2.cvtColor()

? 转换图像的颜色空间;官方文档

def cvtColor(src, code, dst=None, dstCn=None):
"""
转换图像的颜色空间
Argument:
    src: 原图像;
    code: 指定颜色空间转换类型;
    dst: 目标图像;与原图像大小深度一致;
    dstCn: 指定目标图像通道数;默认None,则会根据src、code自动计算;
"""

使用:

技术分享图片

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# @Time    : 19-4-20 下午3:23
# @Author  : chen
"""
opencv读取的图像是BGR格式;
OpenCV可以指定颜色空间转换形式,包括:
    RGB与其他颜色空间的转换
    BGR与其他颜色空间的转换
"""

import matplotlib.pyplot as plt
import cv2

lena_BGR = cv2.imread("./lena.png")

# show BGR lena
plt.subplot(3, 3, 1)
plt.imshow(lena_BGR)
plt.axis('off')
plt.title('lena_BGR')

# BGR to RGB
lena_RGB = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2RGB)
plt.subplot(3, 3, 2)
plt.imshow(lena_RGB)
plt.axis('off')
plt.title('lena_RGB')

# BGR to GRAY
lena_GRAY = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2GRAY)
plt.subplot(3, 3, 3)
plt.imshow(lena_GRAY)
plt.axis('off')
plt.title('lena_GRAY')

# BGR to CIE XYZ
lena_CIEXYZ = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2XYZ)
plt.subplot(3, 3, 4)
plt.imshow(lena_CIEXYZ)
plt.axis('off')
plt.title('lena_CIEXYZ')

# BGR to YCrCb
lena_YCrCb = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2YCrCb)
plt.subplot(3, 3, 5)
plt.imshow(lena_YCrCb)
plt.axis('off')
plt.title('lena_YCrCb')

# BGR to HSV
lena_HSV = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2HSV)
plt.subplot(3, 3, 6)
plt.imshow(lena_HSV)
plt.axis('off')
plt.title('lena_HSV')

# BGR to HLS
lena_HLS = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2HLS)
plt.subplot(3, 3, 7)
plt.imshow(lena_HLS)
plt.axis('off')
plt.title('lena_HLS')

# BGR to CIE L*a*b
lena_Lab = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2Lab)
plt.subplot(3, 3, 8)
plt.imshow(lena_Lab)
plt.axis('off')
plt.title('lena_Lab')

# BGR to CIE L*u*v
lena_Luv = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2Luv)
plt.subplot(3, 3, 9)
plt.imshow(lena_Luv)
plt.axis('off')
plt.title('lena_Luv')

plt.show()

1. RGB to GRAY

RGB to GRAY:
\[ Y = 0.299 \cdot R + 0.587 \cdot G + 0.114\cdot B \]
GRAY to RGB:
\[ R = Y, \ G=Y, \ B = Y, \ A = max(ChannelRange) \]

2. RGB to CIE XYZ

技术分享图片

3. RGB to YCrCb JPEG

技术分享图片

4. RGB to HSV

技术分享图片

5. RGB to HLS

技术分享图片

6. RGB to CIE L*a*b

技术分享图片

7. RGB to CIE L*u*v

技术分享图片

文章来自:https://www.cnblogs.com/chenzhen0530/p/10741264.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3