ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用opencv的cvtColor()函数进行色彩空间转换

2021-07-19 02:31:35  阅读:228  来源: 互联网

标签:函数 image 模式 opencv RGB cvtColor 图像 255


转载:使用opencv的cvtColor()函数进行色彩空间转换_DayDayUp-CSDN博客

 

opencv 的cvtColor()函数,用于在图像中不同的色彩空间进行转换(Converts an image from one color space to another),色彩模式决定了打印或显示的图片颜色。

函数原型:

CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );
cvtColor()函数用于实现图像的色彩空间转换,一般包括四个参数,
cvtColor(src,dst,type,channels);
即源图像,目标图像,转换方式,目标图像通道数,有时候在应用时会选择将dst设置和src相同,即实现原图的转换,需要注意在这个过程中,并不是在src的数据矩阵中直接进行修改,而是编译器使用了新的内存空间对数据进行计算,然后将src.data修改为新的地址;

官方解释:

/** @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

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 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.

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.

@param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision
floating-point.
@param dst output image of the same size and depth as src.
@param code color space conversion code (see #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
*/

 

图像色彩模式
位图模式
位图模式是图像中最基本的格式,图像只有黑色和白色像素,是色彩模式中占有空间最小的,同样也叫做黑白图,它包含的信息量最少,无法包含图像中的细节,相当于只有0或者1

一副彩色图如果要转换成黑白模式,则一般不能直接转换,需要首先将图像转换成灰度模式

灰度模式
灰度模式即使用单一色调来表示图像,与位图模式不同,不像位图只有0和1,使用256级的灰度来表示图像,一个像素相当于占用8为一个字节,每个像素值使用0到255的亮度值代表,其中0为黑色,255为白色,相当于从黑->灰->白的过度,通常我们所说的黑白照片就是这种模式,与位图模式相比,能表现出一定的细节,占用空间也比位图模式较大

RGB模式
RGB模式为我们经常见到的,被称为真色彩。RGB模式的图像有3个颜色通道,分布为红(Red),绿(Green)和蓝(Bule),每个都占用8位一个字节来表示颜色信息,这样每个颜色的取值范围为0~255,那么就三种颜色就可以有多种组合,

当三种基色的值相等是,表现出为灰色,三种颜色都为255即为白色,三种颜色都为0,即为黑色。

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.

注意,OpenCV中的默认颜色格式通常被称为RGB,但它实际上是BGR(字节是相反的)。因此,一个标准(24位)彩色图像的第一个字节将是一个8位蓝色组件(B),第二个字节将是绿色的(G),第三个字节将是红色的(R)。第四个、第五个和第六个字节将成为第二个像素(蓝色、绿色、红色),依此类推。

将读入的图片由BGR转成RGB:

cvtColor (imgData,imgData,COLOR_BGR2RGB);


CMYK模式
CMYK模式被称为印刷色彩模式,主要是来源于印刷行业,以打印油墨在纸张上的光线吸收特性为基础,与RGB类似,也是使用三种颜色,分别为青色(Cyan),品红色(Magenta),黄色(Yellow),以及黑色(Black)

与RGB不同的是:RGB模式依靠的是自身发光的色彩模式,而CMYK是一种依靠反光的色彩模式。

 

 

HSB模式
是根据日常生活中人眼的视觉对色彩的观察得而制定的一套色彩模式,最接近与人类对色彩的辨认的思考方式,所有的颜色都是用色彩三属性来描述

H:(色相):是指从物体反射或透过物体传播的颜色

S:(饱和度):是指颜色的强度或纯度,表示色相中灰色成分所占的比例

B:(亮度):是指颜色对相对明暗程度,通常 100%定义为白色;0%为黑色

将图片转成灰度图:

import numpy as np
import cv2 as cv

img = cv.imread(".././datasheet/len_top.jpg",cv.IMREAD_COLOR)
cv.imshow('lenna',img)
cv.waitKey(0)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
cv.waitKey(0)
cv.destroyAllWindows()
注意使用cvtColor RGB转成其他格式时,需要注意RGB图像的三个通道的顺序,是RGB还是BGR.大部分情况下opencv为RGB,但实际上是BGR,这一点需要注意。

需要注意各个值的范围:

CV_8U图像 其通道值范围为0到255

CV_16U时其值通道值范围为0到65535

CV_32F时,其通道值范围为0到1

在线性转换时,其范围没有较大关系,但是如果是非线性转换,RGB图像需要做正确的归一化,以扩展到响应的范围。例如TGB->L*U*V转换,如果32位的浮点图像之间是由8位的图像转换而来,那么将0到255的范围转换成0,1,那么转换之前首先需要需要所需其图像

img *= 1./255;

cvtColor(img, img, COLOR_BGR2Luv);

如果之间使用cvtColor进行转换,将会丢掉一些信息

参考链接:

学习opencv之cvtColor
————————————————
版权声明:本文为CSDN博主「罗三泡泡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/luolaihua2018/article/details/111475391

标签:函数,image,模式,opencv,RGB,cvtColor,图像,255
来源: https://www.cnblogs.com/MCSFX/p/15028545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有