私は画像using this code by AlyssaのPCAをpythonで計算しようとしています。最初の2つの主成分が逆転します
# Read the image into palmFingers_mask:
# we use PCA and otain 2 principal components
y, x = np.nonzero(palmFingers_mask)
# Subtract mean from each dimension
x = x - np.mean(x)
y = y - np.mean(y)
coords = np.vstack([x, y])
# Covariance matrix and its eigenvectors and eigenvalues
cov = np.cov(coords)
evals[:,frm_cnt], evecs = np.linalg.eig(cov)
# Sort eigenvalues in decreasing order
sort_indices = np.argsort(evals[:,frm_cnt])[::-1]
evec1[:,frm_cnt], evec2[:,frm_cnt] = evecs[:, sort_indices]
x_v1, y_v1 = evec1[:,frm_cnt] # Eigenvector with largest eigenvalue
x_v2, y_v2 = evec2[:,frm_cnt]
# Plot the principal components
scale = 20
plt.figure(1)
plt.plot(x, y, 'y.')
plt.plot([x_v1*-scale*2, x_v1*scale*2],
[y_v1*-scale*2, y_v1*scale*2], color='red')
plt.plot([x_v2*-scale, x_v2*scale],
[y_v2*-scale, y_v2*scale], color='blue')
plt.axis('equal')
plt.gca().invert_yaxis() # Match the image system with origin at top left
plt.show()
I(赤が大きい成分であり、線の長さは何も意味しない)赤と青の線を有する2つの軸をマークします。
そうすることが、私は以下の通り、1故障した場合に出くわした:
どのようにそれが可能になることができますか?これは、赤の固有ベクトルが大きいことを示していますが、直観的には、そうではありません。軸(青い線に沿って)は、より大きなものでなければなりません。私のデータベースの他のすべての画像については、軸が適切であるとわかります。このイメージの場合にのみ、それは交換されます。入力画像を添付しました(Img 209.png)
私の理解に間違いはありますか?
これは私が期待するものである:例えば
、私はそれが働くためBinImg_20.pngを添付しています。
Attached files are on Dropbox。
ブログページの変更提案:https://github.com/alyssaq/alyssaq.github.io/pull/4 – MvG