python-opencv cv2.cvtColor()颜色空间转换

简介: python-opencv cv2.cvtColor()颜色空间转换

python-opencv cv2.cvtColor()颜色空间转换


RGB代表红绿蓝。大多数情况下,RGB颜色存储在结构或无符号整数中,蓝色占据最不重要的“区域”(32位和24位格式的字节),绿色第二少,红色第三少。

BGR是相同的,除了区域顺序颠倒。红色占据最不重要的区域,绿色占第二位(静止),蓝色占第三位。


示例:


#FF0000在读取为RGB十六进制颜色(#rrggbb)时为纯红色,因为第三个区域(数字从右向左读取!)为FF(最大值,全彩色),其他两个区域为00(最小值)价值,没有颜色).如果#FF0000被读作BGR十六进制颜色,则它是纯蓝色。

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.
    该功能将输入图像从一种颜色空间转换为另一种颜色空间。 
    在从RGB颜色空间转换的情况下,应明确指定通道的顺序(RGB或BGR)。 
    请注意,OpenCV中的默认颜色格式通常称为RGB,但实际上是BGR(字节是相反的)。 
    因此,标准(24位)彩色图像中的第一个字节将是8位蓝色分量,第二个字节将是绿色分量,第三个字节将是红色分量。 
    第四,第五和第六个字节将是第二个像素(蓝色,然后是绿色,然后是红色),依此类推。
    .   
    .   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
    R,G和B通道值的常规范围是:
     。 -CV_8U图像为0至255
     。 -CV_16U图像为0至65535
     。 -CV_32F图像为0到1
    .   
    .   In case of linear transformations, the range does not matter. But in case of a non-linear
    .   transformation, an input RGB image should be normalized to the proper value range to get the correct
    .   results, for example, for RGB \f$\rightarrow\f$ L\*u\*v\* transformation. For example, if you have a
    .   32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will
    .   have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor ,
    .   you need first to scale the image down:
    .   @code
    .   img *= 1./255;
    .   cvtColor(img, img, COLOR_BGR2Luv);
    .   @endcode
    .   If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many
    .   applications, this will not be noticeable but it is recommended to use 32-bit images in applications
    .   that need the full range of colors or that convert an image before an operation and then convert
    .   back.
    .   
    在线性变换的情况下,范围无关紧要。 
    但是在进行非线性变换的情况下,应将输入的RGB图像规范化为适当的值范围以获得正确的结果,
    例如,对于RGB \ f $ \ rightarrow \ f $ L \ * u \ * v \ * 转型。 
    例如,如果您有一个32位浮点图像直接从8位图像转换而没有任何缩放,则它将具有0..255的值范围,而不是该函数假定的0..1。 
    因此,在调用#cvtColor之前,您需要先按比例缩小图像:
     。 @代码
     。 img * = 1./255;
     。 cvtColor(img,img,COLOR_BGR2Luv);
     。 @endcode
     。 如果将#cvtColor与8位图像一起使用,转换将丢失一些信息。 
      对于许多应用程序来说,这不会引起注意,但是建议在需要全彩范围的应用程序中使用32位图像,或者在进行操作之前先转换图像然后再转换回来。
    .   If conversion adds the alpha channel, its value will set to the maximum of corresponding channel
    .   range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F.
    如果转换添加了Alpha通道,则其值将设置为相应通道范围的最大值:CV_8U为255,CV_16U为65535,CV_32F为1。
    .   
    .   @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision
    .   floating-point.
      输入图像:8位无符号,16位无符号(CV_16UC ...)或单精度浮点。
    .   @param dst output image of the same size and depth as src.
    输出图像:与src具有相同大小和深度。
    .   @param code color space conversion code (see #ColorConversionCodes).
    颜色空间转换代码(请参见#ColorConversionCodes)。
    .   @param dstCn number of channels in the destination image; if the parameter is 0, the number of the
    .   channels is derived automatically from src and code.
      目标图像中的通道数; 如果参数为0,则通道数自动从src和code得出。
    .   
    .   @see @ref imgproc_color_conversions
    """
    pass
import cv2 as cv
# 【读取图像】
image = cv.imread('feiji.jpg')
# 【将图像灰度化】
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 【显示图像】
cv.imshow('win', image)
cv.waitKey(0)


目录
相关文章
|
3天前
|
数据可视化 计算机视觉 Python
Python教程:如何获取颜色的RGB值
Python教程:如何获取颜色的RGB值
24 0
|
3天前
|
Web App开发 JavaScript 前端开发
python实现QQ空间自动点赞功能
python实现QQ空间自动点赞功能
33 0
|
6月前
|
存储 Cloud Native Linux
openCV 图像色彩空间转换
openCV 图像色彩空间转换
|
1天前
|
Python
空间管理大师已上线!(2),Python高级工程师进阶学习】
空间管理大师已上线!(2),Python高级工程师进阶学习】
|
3天前
|
机器学习/深度学习 计算机视觉 Python
使用Python改变图像颜色的实用指南
使用Python改变图像颜色的实用指南
11 1
|
3天前
|
Java Python
|
3天前
|
监控 API 计算机视觉
OpenCV这么简单为啥不学——1.9、cvtColor颜色空间转换(全色值效果演示)
OpenCV这么简单为啥不学——1.9、cvtColor颜色空间转换(全色值效果演示)
21 0
|
3天前
|
数据采集 机器学习/深度学习 开发工具
画【Python折线图】的一百个学习报告(三、设置全局 Label 颜色)
画【Python折线图】的一百个学习报告(三、设置全局 Label 颜色)
33 0
|
3天前
|
算法 Python Java
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
42 0
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
|
3天前
|
Python
python-docx 创建word 设置字体 字号 颜色 居中
python-docx 创建word 设置字体 字号 颜色 居中
29 0