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
示例:
# -*- coding: utf-8 -*-
"""
@File : 191213_测试_阈值分割.py
@Time : 2019/12/13 15:14
@Author : Dontla
@Email : sxana@qq.com
@Software: PyCharm
"""
import cv2 as cv
# 【读取图像】
image = cv.imread('feiji.jpg')
# 【将图像灰度化】
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# 【显示图像】
cv.imshow('win', image)
cv.waitKey(0)
原图:
处理后: