Filter löschen
Filter löschen

Issue with importing coloured image into MATLAB

23 Ansichten (letzte 30 Tage)
Vaibhav am 2 Dez. 2022
Bearbeitet: DGM am 6 Dez. 2022
Hello everyone!
I have been working with images recently for my college project and everytime I try to import a coloured image, the image turns out as black and white. I did see some other codes to deal with this but it hasn't worked.
Can someone explain what I am doing wrong?
  1 Kommentar
Image Analyst
Image Analyst am 2 Dez. 2022
Not without seeing the code and the image. The image probably isn't what you think it is, or you're displaying it with the wrong code. For example this works fine:
rgbImage = imread('peppers.png');
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Gokul Nath S J
Gokul Nath S J am 5 Dez. 2022
Hi Vaibhav,
There is a possibility that your image might be in grayscale. Kindly check the dimension of the image matrix (img) imported after the following command.
img = imread(image.jpg);
Please check whether there is any third dimension in the img matrix.
It would be helpful if you can share the code along with the image for further clarification.

Image Analyst
Image Analyst am 5 Dez. 2022
Is your colored image floating point with values outside the range of 0 to 1? If so everything 1 or larger will show up as white. Again, attach your image with the paperclip icon.

DGM am 6 Dez. 2022
Bearbeitet: DGM am 6 Dez. 2022
@Image Analyst is right. The easiest way to know what's wrong is to attach an example of the image in question and show how you're trying to read it.
Since we're guessing, I'm going to place my bet as well. I'm going to guess that it's an indexed image. If the image is indexed color, then you need to read the corresponding map as well and use it throughout whatever you do with the image.
[inpict map] = imread('canoe.tif'); % an indexed color image
imshow(inpict) % the index array is a 2D array and will be rendered as grayscale
imshow(inpict,map) % for the image to be rendered in color, a nondefault map is needed
This also demonstrates the problem with assuming that 2D images are grayscale. Request both outputs from imread() and check the map argument. If the map is empty, you can probably assume it's grayscale.
That said, there are plenty of other possible problems that can't be revealed by providing an example image. For example, you might be doing something like:
inpict = imread('peppers.png'); % an actual RGB image
roirangex = 100:200; % let's say we wanted to do some sort of indexing into it
roirangey = 100:200;
imshow(inpict(roirangey,roirangex,:)) % display a region of interest
In this case, the image is being read as color just fine, but if you had made a tiny abbreviation to the indexing expression by accident, you would get this instead:
imshow(inpict(roirangey,roirangex)) % display a region of interest (red channel only)
Note how dissimilar our suggestions are. That's how conversations struggle with poor specificity.
  4 Kommentare
Image Analyst
Image Analyst am 6 Dez. 2022
Yeah, I was just referring to what I personally do in the case of unknown images. If you know for a fact that the image is 2-D (grayscale or indexed) then you don't need to check for the number of color channels. But many people don't know, and we've all seen cases where people see a gray scale image thumbnail in File Explorer and assume that it's 2-D grayscale and not 3-D RGB. So when they do
[rows, columns] = size(grayImage);
they get the wrong number of columns. For those who don't know why (not referring to you DGM) you can look at Steve's blog:
So just to be sure I always ask for the third return argument from size and check that it's either 1 or 3.
I never use imread with multiple image planes/slices. If it's known to be a video or a multipage TIFF image, I use read, or if it's a hyperspectral image there is a hyperspectral add-on package that someone might want to look into (from the Add-ons Explorer on the Tool Ribbon).
What kind of non-video image would have F greater than 1? (Maybe attach one.) But if you knew for a fact that it was you could do
[rows, columns, numberOfColorChannels, F] = size(imageArray);
or to be 100% general for multidimensional arrays
theSize = size(imageArray);
Anyway, thanks for your always thorough answers. They go above and beyond what most people do.
P.S. I'm still digesting Steve's answer in the other thread about how colormaps work. I may work up a demo that illustrates the subtle difference.
DGM am 6 Dez. 2022
Bearbeitet: DGM am 6 Dez. 2022
I know. I just work with a lot of transparent images myself. Depending on the format and how it's written, you might end up with the alpha content being returned as the third output, or it might be attached to the first output instead, so imageArray may have 1,2,3, or 4 channels, and alpha might be empty even when the image has alpha content.
[inpict map alpha] = imread('cmanrgba.tif.txt'); % RGBA
ans = 1×3
256 256 4
ans = 1×2
0 0
[inpict map alpha] = imread('cmanrgba.png'); % RGB + A
ans = 1×3
256 256 3
ans = 1×2
256 256
I don't do anything with video files proper, but I used to use a lot of multiframe GIFs. It's easy enough to get a 4D array out of imread() if the input is a multiframe GIF (extant imread() bugs notwithstanding). CUR/ICO/HDF/TIFF files can also be multiframe, but off the top of my head, I don't think there's a simple syntax to dump all frames into a single array with one call to imread(). I know imread() also supports multiframe PGM/PBM/PPM, but I don't have any examples with which I can test the behavior, and imwrite() can't create one, so I'm going to assume it'll be similar to TIFF.
What I almost never use (except for forum tasks) are indexed images other than GIF. For GIF, I don't use imread() (at least not directly).
I know you've already seen my other comment on size(). I often avoid size(), but if I have to use it without dimensionality contraints, I'd discard the last output. That said, it's not like imread() can return a 5D array ... at least I don't think it can.

Melden Sie sich an, um zu kommentieren.


Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by