Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...