Image quality loss when importing?
이전 댓글 표시
I'm having an issue with importing an image to MatLab.
The image when opened in photos looks like this:

However after running this code:
imagefile = "MRI-brain-tumor-image.png";
I = imread(imagefile);
imshow(I)
The figue from MatLab looks like this:

These are the first lines of code in this script, so I must be using imread or imshow wrong, but looking through their descriptions on mathworks I can't find my error.
Thanks in advance!
댓글 수: 2
Image Analyst
2022년 12월 6일
Please attach 'MRI-brain-tumor-image.png' with the paperclip icon so we can try it on the original file.
Alexandria Baughman
2022년 12월 6일
채택된 답변
추가 답변 (1개)
Image Analyst
2022년 12월 6일
Good question. The image is an indexed image, not a regular gray scale image. I did this in MATLAB:
fileName = 'MRI-brain-tumor-image.png';
% Read indexed image. It is not grayscale.
[grayImage, cmap] = imread(fileName);
whos grayImage
subplot(1, 2, 1);
imshow(grayImage, []);
impixelinfo;
fontSize = 20;
title('In MATLAB Without colormap', 'FontSize', fontSize)
subplot(1, 2, 2);
imshow(grayImage, 'Colormap', cmap);
impixelinfo;
title('In MATLAB With colormap', 'FontSize', fontSize)
colorbar;
g = gcf;
g.Name = 'Displayed from MATLAB'
I also brought it up in the Windows Picture viewer.

The Picture Viewer is on the left. The middle image above is just displaying the raw image in MATLAB. However if you ask it to return a colormap, you will see there is a colormap stored with the image, which means that the image is an indexed image, therefore the middle image would not be correct since it did not use the colormap while displaying it. The right image is displayed in MATLAB using the stored colormap, which should give the correct looking image, but it doesn't. And I don't know why. What's really weird is that the Windows picture viewer seems to be able to figure out the correct colormap and use it, but I don't know how.
Do you know how these images were made? Many or most medical images are saved in dicom format. Did this image originate in a dicom format? If so I'd recommend you use that dicom file and the dicomread function.
댓글 수: 2
Alexandria Baughman
2022년 12월 6일
I would insist the professor give you the good images. Anyway, you can convert the indexed image into a grayscale image with ind2gray
fileName = 'MRI-brain-tumor-image.png';
% Read indexed image. It is not grayscale.
[grayImage, cmap] = imread(fileName);
% Convert from indexed image into grayscale image.
grayImage = ind2gray(grayImage, cmap);
% Display gray scale image.
imshow(grayImage, []);
impixelinfo;
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



