学习opencv之cvtColor

2022-08-02 12:53:33 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

opencv 提供了cvtColor()函数,用于在图像中不同的色彩空间进行转换,用于后续处理。在使用cvtColor之前首先需要了解下基本的图像色彩模式,色彩模式决定了打印或显示的图片颜色。

图像色彩模式

位图模式

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

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

灰度模式

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

RGB模式

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

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

RGB模式的图像占用空间要比位图,灰度图都要大,但表现出的细节更加明显

CMYK模式

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

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

HSB模式

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

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

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

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

除了上述以上之外,还有索引模式,多通道模式等等不再介绍

YUV格式

除了上述图像模式之外,由于历史原因大部分摄像头输入的图片格式都是YUV格式,开始主要用于电视系统以及模拟视频领域。YUV,分为三个分量,“Y”表示明亮度(Luminance或Luma),也就是灰度值;而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素的颜色。如果没用UV信息,只有Y信息,也可以进行成像不过只是黑白的,这样就能很好解决彩色电视与黑白电视的兼容问题,与RGB相比,YUV占用带宽较少,目前摄像头输出格式普遍采用YUV格式。具体介绍可以见https://blog.csdn.net/weixin_42730667/article/details/97233856

而在图像的处理过程中,其实很少使用YUV格式,一般都需要转成RGB格式或者灰度图格式进行转换,而opencv的cvtColor()函数支持这种转换

cvtColor

opencv的python接口如下:

代码语言:javascript复制
st	=	cv.cvtColor(	src, code[, dst[, dstCn]]

其中src:为原图片

code:需要进行色彩空间转换的结果

其C 接口为:

代码语言:javascript复制
void cv::cvtColor	(	InputArray 	src,
                         OutputArray 	dst,
                       int 	code,
                      int 	dstCn = 0 
                      )	

使用例子将一个RGB图,转换成灰度图

代码语言:javascript复制
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,那么转换之前首先需要需要所需其图像

代码语言:javascript复制
img *= 1./255;
cvtColor(img, img, COLOR_BGR2Luv);

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

cvtColor Code

opencv cvtcolor支持的空间转发code,如下,基本能够满足日常需要

enum

cv::ColorConversionCodes { cv::COLOR_BGR2BGRA = 0, cv::COLOR_RGB2RGBA = COLOR_BGR2BGRA, cv::COLOR_BGRA2BGR = 1, cv::COLOR_RGBA2RGB = COLOR_BGRA2BGR, cv::COLOR_BGR2RGBA = 2, cv::COLOR_RGB2BGRA = COLOR_BGR2RGBA, cv::COLOR_RGBA2BGR = 3, cv::COLOR_BGRA2RGB = COLOR_RGBA2BGR, cv::COLOR_BGR2RGB = 4, cv::COLOR_RGB2BGR = COLOR_BGR2RGB, cv::COLOR_BGRA2RGBA = 5, cv::COLOR_RGBA2BGRA = COLOR_BGRA2RGBA, cv::COLOR_BGR2GRAY = 6, cv::COLOR_RGB2GRAY = 7, cv::COLOR_GRAY2BGR = 8, cv::COLOR_GRAY2RGB = COLOR_GRAY2BGR, cv::COLOR_GRAY2BGRA = 9, cv::COLOR_GRAY2RGBA = COLOR_GRAY2BGRA, cv::COLOR_BGRA2GRAY = 10, cv::COLOR_RGBA2GRAY = 11, cv::COLOR_BGR2BGR565 = 12, cv::COLOR_RGB2BGR565 = 13, cv::COLOR_BGR5652BGR = 14, cv::COLOR_BGR5652RGB = 15, cv::COLOR_BGRA2BGR565 = 16, cv::COLOR_RGBA2BGR565 = 17, cv::COLOR_BGR5652BGRA = 18, cv::COLOR_BGR5652RGBA = 19, cv::COLOR_GRAY2BGR565 = 20, cv::COLOR_BGR5652GRAY = 21, cv::COLOR_BGR2BGR555 = 22, cv::COLOR_RGB2BGR555 = 23, cv::COLOR_BGR5552BGR = 24, cv::COLOR_BGR5552RGB = 25, cv::COLOR_BGRA2BGR555 = 26, cv::COLOR_RGBA2BGR555 = 27, cv::COLOR_BGR5552BGRA = 28, cv::COLOR_BGR5552RGBA = 29, cv::COLOR_GRAY2BGR555 = 30, cv::COLOR_BGR5552GRAY = 31, cv::COLOR_BGR2XYZ = 32, cv::COLOR_RGB2XYZ = 33, cv::COLOR_XYZ2BGR = 34, cv::COLOR_XYZ2RGB = 35, cv::COLOR_BGR2YCrCb = 36, cv::COLOR_RGB2YCrCb = 37, cv::COLOR_YCrCb2BGR = 38, cv::COLOR_YCrCb2RGB = 39, cv::COLOR_BGR2HSV = 40, cv::COLOR_RGB2HSV = 41, cv::COLOR_BGR2Lab = 44, cv::COLOR_RGB2Lab = 45, cv::COLOR_BGR2Luv = 50, cv::COLOR_RGB2Luv = 51, cv::COLOR_BGR2HLS = 52, cv::COLOR_RGB2HLS = 53, cv::COLOR_HSV2BGR = 54, cv::COLOR_HSV2RGB = 55, cv::COLOR_Lab2BGR = 56, cv::COLOR_Lab2RGB = 57, cv::COLOR_Luv2BGR = 58, cv::COLOR_Luv2RGB = 59, cv::COLOR_HLS2BGR = 60, cv::COLOR_HLS2RGB = 61, cv::COLOR_BGR2HSV_FULL = 66, cv::COLOR_RGB2HSV_FULL = 67, cv::COLOR_BGR2HLS_FULL = 68, cv::COLOR_RGB2HLS_FULL = 69, cv::COLOR_HSV2BGR_FULL = 70, cv::COLOR_HSV2RGB_FULL = 71, cv::COLOR_HLS2BGR_FULL = 72, cv::COLOR_HLS2RGB_FULL = 73, cv::COLOR_LBGR2Lab = 74, cv::COLOR_LRGB2Lab = 75, cv::COLOR_LBGR2Luv = 76, cv::COLOR_LRGB2Luv = 77, cv::COLOR_Lab2LBGR = 78, cv::COLOR_Lab2LRGB = 79, cv::COLOR_Luv2LBGR = 80, cv::COLOR_Luv2LRGB = 81, cv::COLOR_BGR2YUV = 82, cv::COLOR_RGB2YUV = 83, cv::COLOR_YUV2BGR = 84, cv::COLOR_YUV2RGB = 85, cv::COLOR_YUV2RGB_NV12 = 90, cv::COLOR_YUV2BGR_NV12 = 91, cv::COLOR_YUV2RGB_NV21 = 92, cv::COLOR_YUV2BGR_NV21 = 93, cv::COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21, cv::COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21, cv::COLOR_YUV2RGBA_NV12 = 94, cv::COLOR_YUV2BGRA_NV12 = 95, cv::COLOR_YUV2RGBA_NV21 = 96, cv::COLOR_YUV2BGRA_NV21 = 97, cv::COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21, cv::COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21, cv::COLOR_YUV2RGB_YV12 = 98, cv::COLOR_YUV2BGR_YV12 = 99, cv::COLOR_YUV2RGB_IYUV = 100, cv::COLOR_YUV2BGR_IYUV = 101, cv::COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV, cv::COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV, cv::COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12, cv::COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12, cv::COLOR_YUV2RGBA_YV12 = 102, cv::COLOR_YUV2BGRA_YV12 = 103, cv::COLOR_YUV2RGBA_IYUV = 104, cv::COLOR_YUV2BGRA_IYUV = 105, cv::COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV, cv::COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV, cv::COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12, cv::COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12, cv::COLOR_YUV2GRAY_420 = 106, cv::COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420, cv::COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420, cv::COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420, cv::COLOR_YUV2RGB_UYVY = 107, cv::COLOR_YUV2BGR_UYVY = 108, cv::COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY, cv::COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY, cv::COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2RGBA_UYVY = 111, cv::COLOR_YUV2BGRA_UYVY = 112, cv::COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY, cv::COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY, cv::COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY, cv::COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY, cv::COLOR_YUV2RGB_YUY2 = 115, cv::COLOR_YUV2BGR_YUY2 = 116, cv::COLOR_YUV2RGB_YVYU = 117, cv::COLOR_YUV2BGR_YVYU = 118, cv::COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2, cv::COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2, cv::COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2, cv::COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2, cv::COLOR_YUV2RGBA_YUY2 = 119, cv::COLOR_YUV2BGRA_YUY2 = 120, cv::COLOR_YUV2RGBA_YVYU = 121, cv::COLOR_YUV2BGRA_YVYU = 122, cv::COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2, cv::COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2, cv::COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2, cv::COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2, cv::COLOR_YUV2GRAY_UYVY = 123, cv::COLOR_YUV2GRAY_YUY2 = 124, cv::COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY, cv::COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY, cv::COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2, cv::COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2, cv::COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2, cv::COLOR_RGBA2mRGBA = 125, cv::COLOR_mRGBA2RGBA = 126, cv::COLOR_RGB2YUV_I420 = 127, cv::COLOR_BGR2YUV_I420 = 128, cv::COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420, cv::COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420, cv::COLOR_RGBA2YUV_I420 = 129, cv::COLOR_BGRA2YUV_I420 = 130, cv::COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420, cv::COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420, cv::COLOR_RGB2YUV_YV12 = 131, cv::COLOR_BGR2YUV_YV12 = 132, cv::COLOR_RGBA2YUV_YV12 = 133, cv::COLOR_BGRA2YUV_YV12 = 134, cv::COLOR_BayerBG2BGR = 46, cv::COLOR_BayerGB2BGR = 47, cv::COLOR_BayerRG2BGR = 48, cv::COLOR_BayerGR2BGR = 49, cv::COLOR_BayerBG2RGB = COLOR_BayerRG2BGR, cv::COLOR_BayerGB2RGB = COLOR_BayerGR2BGR, cv::COLOR_BayerRG2RGB = COLOR_BayerBG2BGR, cv::COLOR_BayerGR2RGB = COLOR_BayerGB2BGR, cv::COLOR_BayerBG2GRAY = 86, cv::COLOR_BayerGB2GRAY = 87, cv::COLOR_BayerRG2GRAY = 88, cv::COLOR_BayerGR2GRAY = 89, cv::COLOR_BayerBG2BGR_VNG = 62, cv::COLOR_BayerGB2BGR_VNG = 63, cv::COLOR_BayerRG2BGR_VNG = 64, cv::COLOR_BayerGR2BGR_VNG = 65, cv::COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG, cv::COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG, cv::COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG, cv::COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG, cv::COLOR_BayerBG2BGR_EA = 135, cv::COLOR_BayerGB2BGR_EA = 136, cv::COLOR_BayerRG2BGR_EA = 137, cv::COLOR_BayerGR2BGR_EA = 138, cv::COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA, cv::COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA, cv::COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA, cv::COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA, cv::COLOR_BayerBG2BGRA = 139, cv::COLOR_BayerGB2BGRA = 140, cv::COLOR_BayerRG2BGRA = 141, cv::COLOR_BayerGR2BGRA = 142, cv::COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA, cv::COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA, cv::COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA, cv::COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA, cv::COLOR_COLORCVT_MAX = 143 }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125412.html原文链接:https://javaforall.cn

0 人点赞