Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV convertTo()

Tags:

c++

opencv

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. How can we read the above code? What would be the relation between image and temp_image?

Thanks.

like image 724
Simplicity Avatar asked Aug 30 '26 06:08

Simplicity


2 Answers

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. You didn't specify the type of image, but probably is CV_8UC3 or CV_32FC3, i.e. a 3 channel image (since convertTo doesn't change the number of channels), where each channel has depth 8 bit (unsigned char, CV_8UC3) or 32 bit (float, CV_32FC3).

This line of code will change the depth of each channel, so that temp_image has each channel of depth 16 bit (short). Specifically it's a signed short, since the type specifier has the S: CV_16SC3.

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, i.e. in [–32768, 32767] for signed short.

Why you need to change the depth of an image?

  1. Some OpenCV functions require input images with a specific depth.
  2. You need a matrix to contain a different range of values. E.g. 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]
  3. You read with imread, or want to store with imwrite images with 16bit depth. This are usually used (AFAIK) in medical or graphics application to allow a wider range of colors. However, most monitors do not support 16bit image visualization.
  4. There may be other cases, let me know if I miss the one important to you.
like image 96
Miki Avatar answered Sep 02 '26 06:09

Miki


An image is a matrix of pixel information (i.e. a 1080p image will be a 1,920 × 1,080 matrix where each entry contains rbg values for that pixel). 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.

The temp_image is a new matrix of pixel information based off of image formatted into CV_16SC3.

like image 37
asdf Avatar answered Sep 02 '26 07:09

asdf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!