3

描画にPythonとMatplotlibを使用しています。

次のような 2 つのマトリックス (グレー レベルの画像) があります。

x = np.array([[0,1,0], [1,0,1]])
y = np.array([[0,0.4,0], [1,0,1]])

x と y の違い (緑色の点としましょう) を示す新しい画像 z をプロットし、他の点を灰色のスケールのままにしたいと思います。したがって、前の例で、1 が黒で 0 が白の場合、z は、x と y の差 (この場合は 0.4) に対応する緑色の点を持つ同一の画像になります。

これの目的は、手書きのデータ画像で k-means アルゴリズムの実行をアニメーション化して、アルゴリズムがどのように機能しているかを確認することです。

これが明確であることを願っています。私の英語で申し訳ありません。

4

2 に答える 2

4

最も簡単な解決策は、最初に入力データの RGBA 色を計算し、それを操作して特別な色 (緑) とは異なる値を設定し、次に単純なimshow()変更された RGBA 配列でプロットすることです。これを行う方法は次のとおりです。

>>> rgba_values = cm.gray(y)  # All RGBA values for your input value, with levels of gray
>>> rgba_values[x != y] = [0, 1, 0, 1]  # Set the points where x and y differ to green (RBG = 0, 1, 0)
>>> imshow(rgba_values, interpolation='nearest')

配列間で異なり、現在は緑色になっているデータxポイントy: 共通データがグレー、違いが緑の画像

以前に表示された画像に緑色の点を重ねる場合は、同様のことを行い、元の画像を変更したくない場所でアルファ チャネルを 0 に設定できます。

>>> y_gray = cm.gray(y)  # RGBA image with gray levels
>>> imshow(y_gray, interpolation='nearest')  # Image of one of the arrays
>>> diff_points = numpy.empty_like(y_gray)  # Preparation of an overlay with marked points only
>>> diff_points[x == y, 3] = 0  # Common points not overwritten: alpha layer set to 0
>>> diff_points[x != y] = [0, 1, 0, 1]  # Green for points that differ
>>> imshow(diff_points, interpolation='nearest')
于 2012-04-08T14:15:06.200 に答える
0

もう 1 つの考えられる方法は、特定の値を異なる方法で表示するためにカラーマップを変更することです。

import matplotlib.cm, matplotlib.pyplot as plt, numpy as np, copy
x = np.array([[0,1,0], [1,0,1]])
y = np.array([[0,0.4,0], [1,0,1]])
y_mod = y.copy()
y_mod[x != y] = np.nan # filling the differing pixels with NaNs    
xcm = copy.copy(matplotlib.cm.gray) # copying the gray colormap
xcm.set_bad(color='green', alpha=0.5) # setting the color for "bad" pixels"
plt.imshow(y_mod, cmap=xcm, interpolation='none')

このアプローチの利点の 1 つは、後でこのカラーマップを再利用できることです。

于 2012-07-19T10:47:20.730 に答える