在python和matlab中显示图像显示不同的颜色

问题描述 投票:0回答:1

我正在使用 matlab,但我注意到当原始图像中有灰色时,我正在加载的图像变成黑白图像。我决定在新文件上使用 imshow 检查图像,但它确实使其变成黑白的。我的代码很简单:

image_path = 'no.png';
img = imread(image_path);

imshow(img);

axis off;

然后我尝试将其设为灰度,但问题没有解决。

然后我尝试使用python来显示图像,它正确地显示了图像。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def show_image(image_path):
    img = mpimg.imread(image_path)
    plt.imshow(img)
    plt.axis('off') 
    plt.show()

# Example usage
image_path = "no.png" 
show_image(image_path)

我不知道出了什么问题。 这是原图:

original image

这是 matlab 显示的图像:

matlab image

这是 python 显示的图像:

python image

python image matlab matlab-figure
1个回答
0
投票

实际上,问题来自于你的图像不是灰度的,而是索引的。

image_path = 'no.png';
[img,map] = imread(image_path); % Returns a non-empty map --> indexed image

现在,您可以使用

map
:

获得预期的输出
imshow(img,map);
axis off;

enter image description here

您可以查看这个答案,了解如何检查图像的类型,特别是以下部分:

[imageArray, colorMap] = imread(fullImageFileName);
[rows, columns, numberOfColorChannels] = size(imageArray);
%
%
%
  % Tell user the type of image it is.
  if numberOfColorChannels == 3
    %
    txtInfo = sprintf('%s\nThis is a true color, RGB image.', txtInfo);
  elseif numberOfColorChannels == 1 && isempty(colorMap)
    %
    txtInfo = sprintf('%s\nThis is a gray scale image, with no stored color map.', txtInfo);
  elseif numberOfColorChannels == 1 && ~isempty(colorMap)
    txtInfo = sprintf('%s\nThis is an indexed image.  It has one "value" channel with a stored color map that is used to pseudocolor it.', txtInfo);
© www.soinside.com 2019 - 2024. All rights reserved.