python - 在 Python 中读入索引彩色图像

标签 python image matlab image-processing

索引彩色图像是像素为整数 (1,2, .. N) 的图像,对于每个整数,关联的颜色从给定的颜色映射映射到该像素。在 MATLAB 中,可以通过以下方式读取索引彩色图像:

[im, colormap] = imread('indexed.png');

如何在 Python 中做同样的事情?我尝试过 OpenCV、scikit-image,但它们都会自动转换为 RGB。

最佳答案

经过一些研究,这就是我想出的。您可以使用 Python Imaging Library - 特别是枕叉:https://python-pillow.github.io/

安装包,然后就可以使用Image.openImage 类中的方法打开图像。如果您的图片有色图,则该图片将作为索引图片自动加载。要使其可用,请使用 NumPy 并使用 numpy.array构造函数。我假设您可以使用 NumPy,因为 scikit-image 和 OpenCV 都使用 NumPy 作为图像处理的基本支柱:

from PIL import Image
import numpy as np
im = Image.open("image.png") # Replace with your image name here
indexed = np.array(im) # Convert to NumPy array to easier access

最后,如果您想要图像实际使用的颜色映射/调色板,请使用 Image.getpalette Image 类的一部分的方法。但是,这将为您提供 num_colours x 3 元素列表。因此,要确定您有多少种颜色,只需将此列表的长度除以 3。但是,加载到 MATLAB 中的颜色图是标准化,而 getpalette 不是对此进行规范化并默认为加载的图像类型。因此,您必须通过查看转换后的 NumPy 图像版本来推断图像类型是什么,然后使用它来规范化您的颜色图:

因此:

# Get the colour palette
palette = im.getpalette()

# Determine the total number of colours
num_colours = len(palette)/3

# Determine maximum value of the image data type
max_val = float(np.iinfo(indexed.dtype).max)

# Create a colour map matrix
map = np.array(palette).reshape(num_colours, 3) / max_val

为了证明我们是正确的,这里有一张索引图像 from a question that I helped solve a while ago :

使用 MATLAB 加载此图像:

[indexed, map] = imread('http://i.stack.imgur.com/OxFwB.png');

当我关注索引图像中的第 280 到 290 行和第 400 到 410 列作为双重检查时,我得到了这个:

>> indexed(280:290, 400:410)

ans =

   59   60   61   62   65   64   59   56   56   53   49
   61   61   64   65   65   60   60   57   58   53   53
   67   62   67   56   60   62   60   61   51   59   55
   65   60   62   61   58   58   53   55   57   55   54
   66   58   56   59   56   56   52   55   52   55   52
   68   68   61   61   61   56   56   55   55   57   59
   66   59   59   66   68   62   62   60   60   60   53
   70   68   64   58   61   63   67   61   67   56   59
   69   67   63   64   62   65   63   68   67   64   58
   61   68   68   72   71   73   70   66   63   64   64
   68   67   70   71   71   69   64   64   65   64   58

这是我在运行等效代码以获取索引图像时在 Python 中得到的结果。请注意,我将图像物理下载到我的计算机上并从磁盘加载它。请注意,NumPy 从 0 而不是 1 开始索引,因此我必须将范围减去 1。另请注意,范围运算符的结尾是独占:

In [29]: indexed[279:290, 399:410]
Out[29]: 
array([[59, 60, 61, 62, 65, 64, 59, 56, 56, 53, 49],
       [61, 61, 64, 65, 65, 60, 60, 57, 58, 53, 53],
       [67, 62, 67, 56, 60, 62, 60, 61, 51, 59, 55],
       [65, 60, 62, 61, 58, 58, 53, 55, 57, 55, 54],
       [66, 58, 56, 59, 56, 56, 52, 55, 52, 55, 52],
       [68, 68, 61, 61, 61, 56, 56, 55, 55, 57, 59],
       [66, 59, 59, 66, 68, 62, 62, 60, 60, 60, 53],
       [70, 68, 64, 58, 61, 63, 67, 61, 67, 56, 59],
       [69, 67, 63, 64, 62, 65, 63, 68, 67, 64, 58],
       [61, 68, 68, 72, 71, 73, 70, 66, 63, 64, 64],
       [68, 67, 70, 71, 71, 69, 64, 64, 65, 64, 58]], dtype=uint8)

匹配...现在颜色图呢?让我们看一下 MATLAB 和 Python 之间的颜色映射的前 10 行:

MATLAB

>> format long g;
>> map(1:10,:)

ans =

                         0                         0                         0
        0.0156862745098039       0.00392156862745098        0.0274509803921569
        0.0313725490196078       0.00784313725490196        0.0588235294117647
        0.0470588235294118        0.0117647058823529        0.0901960784313725
        0.0627450980392157        0.0156862745098039          0.12156862745098
        0.0784313725490196        0.0196078431372549         0.152941176470588
        0.0941176470588235        0.0235294117647059         0.184313725490196
         0.109803921568627        0.0274509803921569         0.215686274509804
         0.125490196078431        0.0313725490196078         0.247058823529412
         0.141176470588235        0.0352941176470588          0.27843137254902

python

In [30]: map[:10,:]
Out[30]: 
array([[ 0.        ,  0.        ,  0.        ],
       [ 0.01568627,  0.00392157,  0.02745098],
       [ 0.03137255,  0.00784314,  0.05882353],
       [ 0.04705882,  0.01176471,  0.09019608],
       [ 0.0627451 ,  0.01568627,  0.12156863],
       [ 0.07843137,  0.01960784,  0.15294118],
       [ 0.09411765,  0.02352941,  0.18431373],
       [ 0.10980392,  0.02745098,  0.21568627],
       [ 0.1254902 ,  0.03137255,  0.24705882],
       [ 0.14117647,  0.03529412,  0.27843137]])

...看起来很匹配!

关于python - 在 Python 中读入索引彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022983/

相关文章:

python - 有没有更简单的方法从Python中的字符串中减去季度(3个月)?

c++ - 使用 Mat OpenCV 访问像素

android - 如何在android中解决这个图像缩放问题?

java - 在MATLAB中编写Java的pw.println()等

MATLAB - 如何找到值大于阈值的第一个索引

python - 获取 TypeError : expected str, 字节或 os.PathLike 对象,而不是 InMemoryUploadedFile

python - 为类和静态方法配置lru_cache

python - 我尝试在我的Django应用程序中使用Google Analytics(分析),但无法正常工作

c - libjpeg 图像失真问题

matlab - 直线和折线之间的交点