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

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

imshow(img);

axis off;

然后我试图将其变成灰度级,但问题没有解决.

然后我try 使用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

推荐答案

实际上,问题来自于您的图像不是灰度级的,而是被索引的.

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

您可以查看this answer以了解如何判断图像的类型,特别是以下部分(为清晰起见进行修剪):

[imageArray, colorMap] = imread(fullImageFileName);
[rows, columns, numberOfColorChannels] = size(imageArray);
%
%
%
  % Tell user the type of image it is.
  if numberOfColorChannels == 3
    % This is a true color, RGB image.
    %
  elseif numberOfColorChannels == 1 && isempty(colorMap)
    % This is a gray scale image, with no stored color map.
    %
  elseif numberOfColorChannels == 1 && ~isempty(colorMap)
    % This is an indexed image.  It has one "value" channel with a
    % stored color map that is used to pseudocolor it.

Python相关问答推荐

如何使用scikit-learn Python库中的Agglomerative集群算法以及集群中声明的对象数量?

每个组每第n行就有Pandas

Python中两个矩阵的自定义Hadamard风格产物

如何用symy更新分段函数

三个给定的坐标可以是矩形的点吗

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

即使在可见的情况下也不相互作用

使用numpy提取数据块

如何在箱形图中添加绘制线的传奇?

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

用合并列替换现有列并重命名

在Python中管理打开对话框

删除字符串中第一次出现单词后的所有内容

ODE集成中如何终止solve_ivp的无限运行

在Python argparse包中添加formatter_class MetavarTypeHelpFormatter时, - help不再工作""""

如何在UserSerializer中添加显式字段?

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

手动设置seborn/matplotlib散点图连续变量图例中显示的值

Pandas:计算中间时间条目的总时间增量

python—telegraph—bot send_voice发送空文件