[英]Read image in tensorflow with indexed color value for semantic segmentation task
[英]Read in an indexed color image in Python
索引彩色圖像是具有整數(1,2,... N)的像素的圖像,並且對於每個整數,關聯的顏色從給定的彩色圖映射到該像素。 在MATLAB中,可以通過以下方式讀取索引彩色圖像:
[im, colormap] = imread('indexed.png');
我怎樣才能在Python中做同樣的事情? 我嘗試過OpenCV, scikit-image
但它們都會自動轉換為RGB。
經過一番研究,這就是我想出的。 您可以使用Python Imaging Library - 特別是Pillow fork: https : //python-pillow.github.io/
安裝包,然后您可以使用Image
類中的Image.open
方法打開圖像。 如果您的圖像具有顏色貼圖,則圖像將自動作為索引圖像加載 。 要使其可用,請使用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
為了證明我們有這個正確,這是我剛才幫助解決的一個問題的索引圖像:
使用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行顏色映射:
>> 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
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]])
......看起來很匹配!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.