C++ opencv图像处理使用cvtColor实现颜色转换

目录
  • 前言
  • 源码
  • 效果图
  • 代码颜色空间转换代码

前言

在我们读取图像时通常会用到imread()函数,里面flags可以决定通道数,来得到我们想要的图像,比如:

-1 按解码得到的方式读入图像;

0 单通道 灰度图;

1 三通道 彩色;

当我们想要其他类型的图像时,这种方法就有一些局限性了,所以我们有必要了解cvtColor 这是一种颜色空间转换函数。

源码

void cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0);
src 输入图像
dst 输出图像
code 代码颜色空间转换代码
dstCn 目标图像中的信道数; 如果该参数为0,则通道的数量自动从SRC和code派生。

可以支持RGB HSV 等颜色空间转换(建议不要使用人脸尝试,有些效果懂的都懂)

代码:

int main()
{
	Mat img1, img2, img3, img4;
	img1 = imread("猫1.jpg");
	imshow("原图", img1);
	cvtColor(img1, img2, COLOR_RGB2GRAY);
	imshow("灰度图", img2);
	cvtColor(img1, img3, COLOR_RGB2HSV);
	imshow("HSV", img3);
	cvtColor(img1, img4, COLOR_RGB2BGR);
	imshow("BGR", img4);
	waitKey(0);
}

效果图

代码颜色空间转换代码

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

以上就是C++ opencv图像处理cvtColor实现颜色转换的详细内容,更多关于C++ opencv cvtColor颜色转换的资料请关注我们其它相关文章!

(0)

相关推荐

  • C++使用OpenCV实现证件照蓝底换成白底功能(或其他颜色如红色)详解

    本文实例讲述了C++使用OpenCV实现证件照蓝底换成白底功能(或其他颜色如红色).分享给大家供大家参考,具体如下: 今天刚好老师要办点事情,老师唯一的一张证件照是蓝色的,但是需要的底色是白色的,于是乎,好久不折腾的PS也忘记了,还好旁边的刚来的小学弟懂一点, 在那里慢慢的帮老师一点点的处理,PS在边缘的地方效果还真不咋地,确实是一门技术活. 于是我就想OpenCV能不能实现呢?一搜百度第一篇就是,但是人家转成红色,然后我又对HSV颜色空间不是很懂,最后在一个学习群里 但是文中未对HSV那一块做

  • C++ opencv图像处理实现灰度变换示例

    目录 灰度变换概念 灰度变换的作用 灰度变换的方法 灰度化 灰度的概念 对彩色图进行灰度化 1.加权平均值法 2.取最大值 3.平均值 灰度的线性变换 1.线性变换 2.分段线性变换 灰度的非线性变换 1.对数变换 2.幂律变换 总结 灰度变换概念 在图像预处理中,图像的灰度变换是图像增强的重要手段,灰度变换可以使图像对比度扩展,图像清晰,特征明显,灰度变换主要利用点运算来修正像素灰度,由输入像素点的灰度值确定相应输出点的灰度值,是一种基于图像变换的操作. 灰度变换的作用 1.改善图像是质量,显

  • C++ opencv实现的把蓝底照片转化为白底照片功能完整示例

    本文实例讲述了C++ opencv实现的把蓝底照片转化为白底照片功能.分享给大家供大家参考,具体如下: #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace c

  • opencv实现图像颜色空间转换

    opencv常用的样色空间包括RGB, HSV和YUV等.RGB颜色空间是基于三基色原理二形成的,常用于图像显示系统中:HSV描述的色度,饱和度,亮度这些表示颜色得方法,常用于描述色彩变化:YUV是通过亮度和色度来描述颜色,色度由UV通道组合而成. opencv提供cvtColor(inputArray src, outputArray dst, int code, int dstCn = 0) src是输入图像原,可以是8位CV_8U或者16位CV_16U无符号整形,或者单精度浮点数CV_32

  • C++ opencv图像处理使用cvtColor实现颜色转换

    目录 前言 源码 效果图 代码颜色空间转换代码 前言 在我们读取图像时通常会用到imread()函数,里面flags可以决定通道数,来得到我们想要的图像,比如: -1 按解码得到的方式读入图像: 0 单通道 灰度图: 1 三通道 彩色: 当我们想要其他类型的图像时,这种方法就有一些局限性了,所以我们有必要了解cvtColor 这是一种颜色空间转换函数. 源码 void cvtColor(InputArray src, OutputArray dst, int code, int dstCn =

  • 基于python的opencv图像处理实现对斑马线的检测示例

    基本思路 斑马线检测通过opencv图像处理来进行灰度值转换.高斯滤波去噪.阈值处理.腐蚀和膨胀后对图像进行轮廓检测,通过判断车辆和行人的位置,以及他们之间的距离信息,当车速到超过一定阈值时并且与行人距离较近时,则会被判定车辆为未礼让行人. 结果示例 实验流程 先通过视频截取一张图片来进行测试,如果结果满意之后再嵌套到视频中,从而达到想要的效果. 1.预处理(灰度值转换.高斯滤波去噪.阈值处理.腐蚀和膨胀)> 根据自己的需求来修改一些值 #灰度值转换 imgGray = cv2.cvtColor

  • Python+OpenCV图像处理—— 色彩空间转换

    一.色彩空间的转换 代码如下: #色彩空间转换 import cv2 as cv def color_space_demo(img): gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #RGB转换为GRAY 这里的GRAY是单通道的 cv.imshow("gray", gray) hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) #RGB转换为HSV cv.imshow("hsv", hsv) y

  • python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)

    图像素描特效 图像素描特效主要经过以下几个步骤: 调用cv.cvtColor()函数将彩色图像灰度化处理: 通过cv.GaussianBlur()函数实现高斯滤波降噪: 边缘检测采用Canny算子实现: 最后通过cv.threshold()反二进制阈值化处理实现素描特效. #coding:utf-8 import cv2 as cv import numpy as np #读取原始图像 img = cv.imread('d:/paojie.png') #图像灰度处理 gray = cv.cvtC

  • python opencv 图像处理之图像算数运算及修改颜色空间

    目录 1.图像加法 1.1Numpy加法 1.2OpenCV加法 2.图像融合 3.改变颜色空间 1.图像加法 图像加法有两种方式,一种是通过 Numpy 直接对两个图像进行相加,另一种是通过 OpenCV 的 add() 函数进行相加. 不管使用哪种方法,相加的两个图像必须具有相同的深度和类型,简单理解就是图像的大小和类型必须一致. 1.1Numpy加法 Numpy 的运算方法是: img = img1 + img2 ,然后再对最终的运算结果取模. 当最终的像素值 <= 255 时,则运算结果

  • opencv+图像处理(Image Processing in OpenCV) 4-0改变颜色空间的过程

    目录 0.本节涉及的opencv新函数 1.彩色的形成 2.彩色空间 3.为什么需要选择合适的彩色空间 4.如何选择合适的彩色空间 5.常用的彩色空间 RGB 6.变换彩色空间数学公式 7.opencv变换彩色空间代码+注释+效果 本专栏代码地址https://github.com/xiawei20161308104/xv_opencv_tutorials本节代码路径xv_opencv_tutorials/ImageProcessinginOpenCV/changing_colorspaces.

  • Opencv图像处理:如何判断图片里某个颜色值占的比例

    一.功能 这里的需求是,判断摄像头有没有被物体遮挡.这里只考虑用手遮挡---->判断黑色颜色的范围. 二.使用OpenCV的Mat格式图片遍历图片 下面代码里,传入的图片的尺寸是640*480,判断黑色范围. /* 在图片里查找指定颜色的比例 */ int Widget::Mat_color_Find(QImage qimage) { Mat image = QImage2cvMat(qimage);//将图片加载进来 int num = 0;//记录颜色的像素点 float rate;//要计

  • Python+OpenCV图像处理——打印图片属性、设置存储路径、调用摄像头

    一. 打印图片属性.设置图片存储路径 代码如下: #打印图片的属性.保存图片位置 import cv2 as cv import numpy as np #numpy是一个开源的Python科学计算库 def get_image_info(image): print(type(image)) #type() 函数如果只有第一个参数则返回对象的类型 在这里函数显示图片类型为 numpy类型的数组 print(image.shape) #图像矩阵的shape属性表示图像的大小,shape会返回tup

  • Python+OpenCV图像处理——实现轮廓发现

    简介:轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓发现结果. 代码如下: import cv2 as cv import numpy as np def contours_demo(image): dst = cv.GaussianBlur(image, (3, 3), 0) #高斯模糊去噪 gray = cv.cvtColor(dst, cv.COLOR_RGB2GRAY) ret, binary = cv.threshold(gray, 0, 25

  • OpenCV图像处理基本操作详解

    本文实例为大家分享了OpenCV图像处理基本操作的具体代码,供大家参考,具体内容如下 图像的读取 cv2.IMREAD_COLOR 彩色图像 cv2.IMREAD_GRAYSCALE 灰色图像 import cv2#opencv 的读取格式是BGR import matplotlib.pyplot as plt import numpy as np #图像的显示,也可以创建多个窗口 img=cv2.imread('tu.jpg') cv2.imshow('name',img) #等待时间毫秒级,

随机推荐