OpenCV学习笔记:颜色转换

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第5天,点击查看活动详情

本文是我之前在微信公众号上的一篇文章记录。原链接为:# OpenCV学习笔记:颜色转换

我们生活中大多数图像是通过红绿蓝三种颜色不同比例的混合展现出五彩斑斓的颜色,就是我们常见的RGB颜色。当然还有其他的颜色模型,比图YUV,HSV以及GRAY等。其中GRAY模型并不是彩色模型,他是一个灰色图像的模型。常用的RGB转换成GRAY方式是:Gray=R∗0.3+G∗0.59+B∗0.11

opencv提供了cvtColor()函数来实现这些功能各种颜色模型间的转换。首先看一下cvtColor函数定义:

/** @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 \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.
 
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.
 
@see @ref imgproc_color_conversions
*/
void cv::cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0);

复制代码
  • src:待转换颜色模型的原始图像。
  • dst:转换颜色模型后的目标图像。
  • code:颜色空间转换的标志,如由RGB空间到HSV空间。部分的标志如下表格。
  • dstCn:目标图像中的通道数,如果参数为0,则从src和代码中自动导出通道数。
标志参数 标志参数 标志参数
COLOR_BGR2BGRA 0 对RGB图像添加alpha通道
COLOR_BGR2RGB 4 彩色图像通道颜色顺序的更改
COLOR_BGR2GRAY 10 彩色图像转成灰度图像
COLOR_GRAY2BGR 8 灰度图像转成彩色图像(伪彩色)
COLOR_BGR2YUV 82 RGB颜色模型转成YUV颜色模型
COLOR_YUV2BGR 84 YUV颜色模型转成RGB颜色模型
COLOR_BGR2HSV 40 RGB颜色模型转成HSV颜色模型
COLOR_HSV2BGR 54 HSV颜色模型转成RGB颜色模型
COLOR_BGR2Lab 44 RGB颜色模型转成Lab颜色模型
COLOR_Lab2BGR 56 Lab颜色模型转成RGB颜色模型/

看如下测试代码,要注意Opencv打开一张图像默认存储的就是BGR颜色模型的,并非RGB。

void testCvtColor()
{
    std::string img_path = "car.png";

    cv::Mat img = cv::imread(img_path, cv::IMREAD_COLOR);
    cv::imshow("src-bgr", img);
    cv::waitKey(0);

    cv::Mat gray;
    cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
    cv::imshow("gray", gray);
    cv::waitKey(0);

    cv::Mat yuv;
    cv::cvtColor(img, yuv, cv::COLOR_BGR2YUV);
    cv::imshow("yuv", yuv);
    cv::waitKey(0);  

    cv::Mat rgb;
    cv::cvtColor(img, rgb, cv::COLOR_BGR2RGB);
    cv::imshow("rgb", rgb);
    cv::waitKey(0); 

    cv::Mat hsv;
    cv::cvtColor(img, hsv, cv::COLOR_BGR2HSV);
    cv::imshow("hsv", hsv);
    cv::waitKey(0); 
}
复制代码

程序输出如下:

image-20210620164831378

image-20210620164900727

image-20210620163815275

猜你喜欢

转载自juejin.im/post/7104261310338203655