M x Nグレースケール画像、つまりマトリックスまたは2次元配列をRGBヒートマップ、つまりM x N x 3配列に変換するにはどうすればよいですか?
例:
[[0.9, 0.3], [0.2, 0.1]]
になる必要があります
[[red, green-blue], [green-blue, blue]]
ここで、赤は[1, 0, 0]
、青は[0, 0, 1]
、などです。
M x Nグレースケール画像、つまりマトリックスまたは2次元配列をRGBヒートマップ、つまりM x N x 3配列に変換するにはどうすればよいですか?
例:
[[0.9, 0.3], [0.2, 0.1]]
になる必要があります
[[red, green-blue], [green-blue, blue]]
ここで、赤は[1, 0, 0]
、青は[0, 0, 1]
、などです。
import matplotlib.pyplot as plt
img = [[0.9, 0.3], [0.2, 0.1]]
cmap = plt.get_cmap('jet')
rgba_img = cmap(img)
rgb_img = np.delete(rgba_img, 3, 2)
cmap
LinearSegmentedColormap
クラスから派生したmatplotlibのクラスのインスタンスですColormap
。__call__
で定義された関数のために機能しColormap
ます。APIで説明されていないため、参照用にmatplotlibのgitリポジトリからのdocstringを次に示します。
def __call__(self, X, alpha=None, bytes=False):
"""
*X* is either a scalar or an array (of any dimension).
If scalar, a tuple of rgba values is returned, otherwise
an array with the new shape = oldshape+(4,). If the X-values
are integers, then they are used as indices into the array.
If they are floating point, then they must be in the
interval (0.0, 1.0).
Alpha must be a scalar between 0 and 1, or None.
If bytes is False, the rgba values will be floats on a
0-1 scale; if True, they will be uint8, 0-255.
"""
より簡単なオプションは、またはimg
を使用して表示し、結果をRGBまたはRGBA画像としてコピーまたは保存することです。これは私のアプリケーションには遅すぎました(私のマシンでは約30倍遅くなりました)。plt.imshow
plt.matshow