Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openCV: How imshow treat negative values?

Tags:

c++

opencv

According to documentation, imshow will work like this

  1. If the image is 8-bit unsigned, 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 value range [0,255*256] is mapped to [0,255].
  2. If the image is 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 contain negative value in 32-bit floating point. How it will treat it?

like image 848
user3747190 Avatar asked Dec 26 '22 06:12

user3747190


1 Answers

The key bits of 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, clamps the argument to 0 and 255.

For floating point depth == CV_32F:

  • src_depth is nether less than CV_8S nor CV_32S, so scale == 255 (which agrees with the doco).
  • src_depth ia neither equal to CV_8S nor CV_16S so scale == 0.

That means for CV_32F

  • values larger than 1.0 end up as 255 (white)
  • negative values end up as 0 (black)

Now to answer you question:

What if my Matrix contain negative value in 32-bit floating point. How it will treat it?

The negative values will be displayed as if they are 0.

like image 186
Bull Avatar answered Dec 28 '22 20:12

Bull