将R、G、B三个通道作为笛卡尔坐标系中的X、Y、Z轴,就得到了一种对于颜色的空间描述
对于彩色图转灰度图,有一个很著名的心理学公式:
Gray = R * 0.299 + G * 0.587 + B * 0.114
这个模型就是按色彩、深浅、明暗来描述的。
H是色彩;
S是深浅, S = 0时,只有灰度;
V是明暗,表示色彩的明亮程度,但与光强无直接联系。
应用:可以用于偏光矫正、去除阴影、图像分割等.
def cvtColor(src, code, dst=None, dstCn=None): # real signature unknown; restored from __doc__
"""
cvtColor(src, code[, dst[, dstCn]]) -> dst
. @brief Converts an image from one color space to another.
.
. The function converts an input image from one color space to another. In case of a transformation
. to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note
. that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the
. bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue
. component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and
. sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.
.
. The conventional ranges for R, G, and B channel values are:
. - 0 to 255 for CV_8U images
. - 0 to 65535 for CV_16U images
. - 0 to 1 for CV_32F images
import cv2
import numpy as np
if __name__ == ‘__main__‘:
img = cv2.imread(‘./00_dog.jpg‘, cv2.IMREAD_UNCHANGED)
cv2.imshow("img", img)
print(‘RGB2GRAY‘)
cv2.imshow("RGB2GRAY", cv2.cvtColor(img, cv2.COLOR_RGB2GRAY))
print(‘RGB2HSV‘)
dst1 = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
cv2.imshow("RGB2HSV", dst1)
print(‘HSV2RGB‘)
dst2 = cv2.cvtColor(dst1, cv2.COLOR_HSV2RGB)
cv2.imshow("RGB2HSV", dst2)
cv2.waitKey(10000)
cv2.destroyAllWindows()
原文:https://www.cnblogs.com/zhazhaacmer/p/15309722.html