Getting black plots with plt.imshow after multiplying RGB image array by a scalar

YellowPillow picture YellowPillow · Feb 4, 2017 · Viewed 10.7k times · Source

So I am a bit confused as to why this is happening.

I have a binary image: enter image description here

Now I want to convert this binary image into RGB space, so therefore I use the dstack function to concatenate the 3rd axis

enter image description here

Everything works fine so far, but now I have to multiply the out_image array by 255 to reflect white in RGB space, and this is where the problem occurs everything turns blackenter image description here

But if I plot another random image, everything is fine so what is happening here, I've also played around with the cmap but regardless of what kind of cmap I use it always turns out to be black when multiplied by 255

Any ideas?

Answer

ImportanceOfBeingErnest picture ImportanceOfBeingErnest · Feb 4, 2017

The solution for the problem in the question would be not to multiply the array with 255.
The other option is to reduce the datatype of the image to unsigned int8,
out_image = out_image.astype(np.uint8)

Let me explain why:

A single channel image can have arbitrary values and datatype. The color will be determined by the colormap to be used, and if required, normalized to a certain range.

enter image description here

In contrast, 3 channel RGB arrays are assumed by imshow to be in two ranges, [0., 1.] or [0,255]. ("3-dimensional arrays must be of dtype unsigned byte, unsigned short, float32 or float64").
The range to use will be selected by the datatype of the array:

  1. A float array should be in the [0., 1.] range,
  2. an integer array should be in the range [0,255]. Also note that integer arrays must be of datatype int8 and not int32.

enter image description here

As can be seen in the RGB case, an integer array in the range [0,1] stays black, as well as a float array of range [0., 255.].