Issue with importing coloured image into MATLAB

조회 수: 1(최근 30일)
Vaibhav 2022년 12월 2일
편집: DGM 2022년 12월 6일
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
Image Analyst
Image Analyst 2022년 12월 2일
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:

댓글을 달려면 로그인하십시오.


Gokul Nath S J
Gokul Nath S J 2022년 12월 5일
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 2022년 12월 5일
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 2022년 12월 6일
편집: DGM 2022년 12월 6일
@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
DGM 2022년 12월 6일
편집: DGM 2022년 12월 6일
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.

댓글을 달려면 로그인하십시오.


Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by