python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)-程序员宅基地

技术标签: python  Opencv  计算机视觉  opencv  

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   : [email protected]
@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)

原图:
在这里插入图片描述
处理后:
在这里插入图片描述

参考文章:python opencv ColorConversionCodes

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Dontla/article/details/102953243

智能推荐

win10微软账号登陆报错:0x80190001解决方案_微软账户登录0x8019001-程序员宅基地

文章浏览阅读3.5w次,点赞19次,收藏18次。win10微软账号登陆报错:0x80190001解决方案_微软账户登录0x8019001

从微软AzureDevOps看实施基于DevOps全流程软件交付-程序员宅基地

文章浏览阅读1.5k次。Azure DevOpsAzure DevOps 汇集人员、流程和技术,实现软件交付自动化,为用户提供持续的价值。借助 Azure DevOps 解决方案,帮助您全流程构建你的软件产品,它使流程和产品更可靠。Azure DevOps帮助你用敏捷工具计划项目;用Git管理你的代码;..._azure devops approve

Typora中使用LaTeX:多行公式左对齐_typora对齐公式-程序员宅基地

文章浏览阅读1.4w次,点赞7次,收藏40次。Typora中使用LaTeX:多行公式左对齐有时候公式太长,用=号对齐很难看(有的公式左边很长,右边很短),此时难免需要进行"公式左对齐"。所需要的环境还是"align"(或者是align*,不带公式编号)。语法如下:\begin{align*}\label{2} & X(0) = x(0)W_{N}^{0\cdot0} + x(1)W_{N}^{0\cdot1} + \cdots + x(N-1)W_{N}^{0\cdot(N-1)}\\ & X(1) = x(0)W_{N}_typora对齐公式

springboot配置文件加载顺序, java启动参数优先级_nacos默认覆盖本地吗-程序员宅基地

文章浏览阅读1k次。(12)、jar包外面的 Profile-specific application properties (application- {profile} .properties和YAML)(13)、jar包内的 Profile-specific application properties (application-{profile}.properties和YAML)(1)、在您的HOME目录设置的Devtools全局属性(~/.spring-boot-devtools.properties)。_nacos默认覆盖本地吗

适合写技术文档的工具_技术文档编写工具-程序员宅基地

文章浏览阅读3.6k次。https://www.showdoc.com.cn/_技术文档编写工具

面向食品领域的命名实体识别技术-程序员宅基地

文章浏览阅读706次,点赞17次,收藏24次。非常感谢您的详细任务描述和要求。作为一位世界级人工智能专家和计算机领域大师,我将以专业的技术语言和深入的见解,为您撰写这篇《面向食品领域的命名实体识别技术》的技术博客文章。面向食品领域的命名实体识别技术作者:禅与计算机程序设计艺术1. 背景介绍食品行业是一

随便推点

lua面向对象编程之点号与冒号的差异详细比较-程序员宅基地

文章浏览阅读41次。首先,先来一段在lua创建一个类与对象的代码 1 Class = {} 2 Class.__index = Class 3 4 function Class:new(x,y) 5 local temp = {} 6 setmetatable(temp, Class) 7 temp.x = x 8 temp.y = y 9 return...

百度云虚假下载_虚假新闻:关于公共云的5种常见误解-程序员宅基地

文章浏览阅读212次。百度云虚假下载 In the complex world of IT, there are many misconceptions about migrating to the public cloud. Some of these portray the public cloud as the panacea for every IT issue, whereas others consider..._from diggers to data centres从淘金企业到数据中心

Tesseract图像识别OCR的学习1_tesseract doocr-程序员宅基地

文章浏览阅读1.1k次。领导让做一个识别发票的服务,之前都是写增删改查,完全没接触过图像识别这种高大上的东西,记录一下吧新建一个项目,导入tess4j <dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j&l..._tesseract doocr

不同层级的Android开发者的不同行为,我们该如何进阶和规划?-程序员宅基地

文章浏览阅读874次,点赞11次,收藏14次。首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶注解原理丶注解原理丶序列化Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)代码框架结构优化(数据结构丶排序算法丶设计模式)APP性能优化(用户体验优化丶适配丶代码调优)热修复丶热升级丶Hook技术丶IOC架构设计。

Pelee: A real-time object detection system on mobile devices-程序员宅基地

文章浏览阅读237次。Pelee: A real-time object detection system on mobile devices属于densenet的变种文章,没有使用可分离卷积,因为可分离卷积不同的框架实现方法不一样。Before first dense blockDynamic Number of channels in Bottleneck layerThe nu..._a real-time object detection system on mobile devices

Hadoop环境搭建(保姆级教学)_hadoop平台搭建步骤-程序员宅基地

文章浏览阅读5.4k次,点赞10次,收藏64次。HADOOP环境搭建过程详解_hadoop平台搭建步骤