Image has 3 channels but its in a grayscale color. If I change it to 1 channel, it goes into RGB

2024/11/18 3:29:04

I started doing some image-processing in python and I've stumbled upon an issue which is kind of confusing from a beginner's perspective. I have a dataset of 1131 np arrays (images) of MRI on knee. The shape of the images is kind of weird, it is (44, 256, 256) meaning that one array has 44 images with size of 256x256 pixels. For instance, if I show the 22nd image (middle one), with plt.imshow(image[22]) this is what I get:

enter image description here

This image clearly seems like an RGB but if I do image[22].shape, I get (256,256) which is expected but at the same time confusing because it says that it has no channels and from what I know, images that don't have channels should be in grayscale color but clearly this is not the case here.

I dived deeper and tried executing cv2.cvtColor(image[22, :, :], cv2.COLOR_BGR2RGB) which resulted in:

enter image description here

This does seem like a grayscale image but the shape of this image is (256, 256, 3). This is so confusing for me and can someone point it out why does this happen, grayscale image with 3 channels and colorful image with no channels and how can I have this image[22] in grayscale mode with 1 channel? I need it in 1 channel because I want to do a feature extraction but if I do it this way, pure image[22] and the "grayscale" variant have the same values for each feature but from the looks of it, they do not seem exactly.

The numpy array (image) can be taken from here.

Answer

The image is monochrome, but by default imshow is mapping pixels to the default colormap, viridis.

See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html

https://en.xdnf.cn/q/118720.html

Related Q&A

Creating a Barplot using pyqt

I need plotting an animated bar chart with pyqtgraph. With animate i mean a chart, which updates his values given by a serial port. For now, a not-animated plot will be enough. I would like to implemen…

Stop Button in Tkinter

Im trying to have a turtle animation start with a button and stop with a button. Its very easy to start with a button but I cant seem to be able to figure out a stop button? Heres my code so far: imp…

binascii.Error: Incorrect padding How to decode the end with /

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I dont know how to decode it, I havent found the answer, who…

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…

How to plot a ROC curve using dataframe converted from CSV file

I was trying to plot a ROC curve by using the documentation provided by sklearn. My data is in a CSV file, and it looks like this.It has two classes Goodand Badscreenshot of my CSV fileAnd my code look…

SyntaxError: Non-ASCII character. Python

Could somebody tell me which character is a non-ASCII character in the following:Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible column…

A pseudocode algorithm for integer addition based on binary operation

I have tried for ages to come up with a solution but just cant get my head around it.It needs to be based on two integers on the use of standard logical operations which have direct hardware implementa…

How to efficiently split overlapping ranges?

I am looking for an efficient method to split overlapping ranges, I have read many similar questions but none of them solves my problem. The problem is simple, given a list of triplets, the first two e…

pass 2D array to linear regression (sklearn)

I want to pass 2D array to linear regression: x = [[1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1],[0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0,…

How do I fix this OverflowError?

I keep getting a "OverflowError: math range error". No matter what I input, the result is the same. Im running Python 3.3, and its finding the problem at the last line. How do I fix this? (A…