OpenCV: How does imshow handle negative values?

According to the documentation, imshow will work like this

  • If the image is unsigned 8-bit, it is displayed as is. 2. If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the range of values ​​[0.255 * 256] is mapped to [0.255].
  • If the image is a 32-bit floating point, the pixel values ​​are multiplied by 255. That is, the value range [0.1] is mapped to [0.255].

What if my matrix contains a negative value in 32bit floating point. How will he feel about this?

+3


source to share


1 answer


The key bits of the Open_CV source are

#define CV_8S   1
#define CV_32S  4
#define CV_32F  5

double scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255;
double shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0;

dst[x] = saturate_cast<DT>(src[x]*scale + shift);

      

Ultimately imshow creates a CV_8 Mat before displaying it, so saturate_cast when DT is uchar binds the argument to 0 and 255.

For floating point depth == CV_32F:

  • src_depth is less than CV_8S or CV_32S, so scale == 255 (which is consistent with doco).
  • src_depth ia is not equal to CV_8S or CV_16S, so scale == 0.


This means that for CV_32F

  • values ​​greater than 1.0 end in 255 (white)
  • negative values ​​end as 0 (black)

Now, to answer your question:

What if my matrix contains a negative value in 32bit floating point. How will he feel about this?

Negative values ​​will be displayed as if they were 0.

+7


source







All Articles