openCV学习之路(2-4)—深度解析cvtColor函数

代码显示如下:

#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> 

using namespace cv;
using namespace std;

int main()
{
	// 读入一张图片    
	Mat img = imread("D:\\faceDetection.png",1);
	if (img.data == NULL) {
		cout << "could not load image..." << endl;
		getchar();
		return -1;
	}

	// 创建一个名为 "图片"窗口    
	namedWindow("图片");
	// 在窗口中显示图片   
	imshow("图片", img);

	Mat result;
	cvtColor(img, result, CV_BGR2Luv);
	namedWindow("灰度图", WINDOW_AUTOSIZE);
	imshow("灰度图", result);

	// 等待6000 ms后窗口自动关闭    
	waitKey(6000);
	return 0;
}
@param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point.
@param src 输入图像:8 位无符号、16 位无符号 (CV_16UC...) 或单精度浮点。
@param dst output image of the same size and depth as src.
@param dst 输出与 src 大小和深度相同的图像。
@param code color space conversion code (see ColorConversionCodes).
@param code 颜色空间转换代码(请参阅 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.
@param dstCn 目标图像中的通道数; 如果参数为 0,则通道数由 src 和 code 自动得出。

CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );

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:
R、G 和 B 通道值的常规范围是:

  • 0 to 255 for CV_8U images
  • 0 到 255 用于 CV_8U 图像
  • 0 to 65535 for CV_16U images
  • 0 到 65535 用于 CV_16U 图像
  • 0 to 1 for CV_32F images
  • 0 到 1 用于 CV_32F 图像

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 TGB->LUV 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:
在线性变换的情况下,范围无关紧要。 但在非线性变换的情况下,应将输入的 RGB 图像归一化到适当的值范围以获得正确的结果,例如,对于 TGB->LUV 变换。 例如,如果您有一个 32 位浮点图像直接从 8 位图像转换而来,没有任何缩放,那么它将具有 0…255 的值范围,而不是函数假定的 0…1。 因此,在调用 #cvtColor 之前,您需要先缩小图像:

img *= 1./255;
    cvtColor(img, img, COLOR_BGR2Luv);

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.
如果您对 8 位图像使用 #cvtColor,则转换会丢失一些信息。 对于许多应用程序,这并不明显,但建议在需要全范围颜色或在操作前转换图像然后再转换回来的应用程序中使用 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。

版权声明:本文为博主功底码哥原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/weixin_40423516/article/details/123186252

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2022年3月1日 下午7:40
下一篇 2022年3月1日 下午7:55

相关推荐