简体   繁体   English

OpenCV convertTo()

[英]OpenCV convertTo()

I came across this code:我遇到了这个代码:

image.convertTo(temp_image,CV_16SC3);

I saw the description of the convertTo() function from here , but what confuses me is image .我从这里看到了 convertTo() 函数的描述,但让我感到困惑的是image How can we read the above code?我们如何阅读上面的代码? What would be the relation between image and temp_image ? imagetemp_image之间的关系是什么?

Thanks.谢谢。

The other answers here are correct, but lack some details.这里的其他答案是正确的,但缺少一些细节。 Let me try.让我试试。

image.convertTo(temp_image,CV_16SC3);

You have a source image image , and a destination image temp_image .您有一个图像image和一个目标图像temp_image You didn't specify the type of image , but probably is CV_8UC3 or CV_32FC3 , ie a 3 channel image (since convertTo doesn't change the number of channels), where each channel has depth 8 bit ( unsigned char , CV_ 8U C3) or 32 bit ( float , CV_ 32F C3).您没有指定image的类型,但可能是CV_8UC3CV_32FC3 ,即 3 通道图像(因为convertTo不会改变通道数),其中每个通道的深度为 8 位( unsigned char ,CV_ 8U C3)或 32 位( float , CV_ 32F C3 )。

This line of code will change the depth of each channel, so that temp_image has each channel of depth 16 bit ( short ).这行代码会改变每个通道的深度,这样temp_image每个通道的深度都是 16 位( short )。 Specifically it's a signed short , since the type specifier has the S : CV_16 S C3.具体来说,它是一个有signed short ,因为类型说明符有S : CV_16 S C3。

Note that if you are narrowing down the depth, as in the case from float to signed short , then saturate_cast will make sure that all the values in temp_image will be in the correct range, ie in [–32768, 32767] for signed short .请注意,如果您正在缩小深度,如从floatsigned short ,则saturate_cast将确保temp_image中的所有值都在正确的范围内,即在 [–32768, 32767] 中表示有signed short

Why you need to change the depth of an image?为什么需要改变图像的深度?

  1. Some OpenCV functions require input images with a specific depth.某些 OpenCV 函数需要具有特定深度的输入图像。
  2. You need a matrix to contain a different range of values.您需要一个矩阵来包含不同范围的值。 Eg if you need to sum (or subtract) some images CV_8UC3 (tipically BGR images), you'd better store the result in a CV_16SC3 or you'll probably get wrong results due to saturations, since the range for CV_8U images is in [0,255]例如,如果您需要对某些图像 CV_8UC3(通常为 BGR 图像)求和(或减去),则最好将结果存储在 CV_16SC3 中,否则您可能会因饱和而得到错误的结果,因为 CV_8U 图像的范围在 [ 0,255]
  3. You read with imread , or want to store with imwrite images with 16bit depth.您使用imread阅读,或者想要使用 16 位深度的imwrite图像存储。 This are usually used (AFAIK) in medical or graphics application to allow a wider range of colors.这通常用于 (AFAIK) 医学或图形应用程序以允许更广泛的颜色。 However, most monitors do not support 16bit image visualization.但是,大多数显示器不支持 16 位图像可视化。
  4. There may be other cases, let me know if I miss the one important to you.可能还有其他情况,如果我错过了对您来说很重要的一个,请告诉我。

An image is a matrix of pixel information (ie a 1080p image will be a 1,920 × 1,080 matrix where each entry contains rbg values for that pixel).图像是像素信息矩阵(即1080p图像将是1,920 × 1,080矩阵,其中每个条目包含该像素的rbg值)。 All you are doing is reformatting that matrix (each pixel entry, iteratively) into a new type ( CV_16SC3 ) so it can be read by different programs.您所做的就是将该矩阵(每个像素条目,迭代地)重新格式化为新类型( CV_16SC3 ),以便不同的程序可以读取它。

The temp_image is a new matrix of pixel information based off of image formatted into CV_16SC3 . temp_image是一个新的像素信息矩阵,基于格式化为CV_16SC3image

The first one is a source , the second one - destination .第一个是,第二个是目的地 So, it takes image, converts it into type CV_16SC3 and stores in temp_image.因此,它获取图像,将其转换为 CV_16SC3 类型并存储在 temp_image 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM