如何使用Python OpenCV cv2.PCAProject压缩单个数组

时间:2016-05-03 18:15:08

标签: python opencv pca

背景:说我已经使用PCACompute在python中训练了一个PCA,如下所示:

import numpy as np
import cv2 as cv

# generate some random data
data = np.random.sample(128)
for x in xrange(63): data = np.vstack((data, np.random.sample(128)))
print data.shape # (64, 128) i.e. 64 arrays of 128 dimensions

# train the PCA
mean, eigenvectors = cv.PCACompute(data, maxComponents=32)
print mean.shape # (1, 128)
print eigenvectors.shape # (32, 128)

问题:现在我想要使用PCA压缩一个数组

sample = np.random.sample(128)
print sample.shape # (128,)

compressed_sample = cv.PCAProject(sample, mean, eigenvectors)
  

OpenCV错误:断言失败(mean.data&& eigenvectors.data&&((mean.rows == 1&& mean.cols == data.cols)||(mean.cols == 1&& mean.rows == data.rows)))

1 个答案:

答案 0 :(得分:1)

解决方案:输入后我解决了它,也可以继续发帖,以防其他人遇到同样的问题。

sample = sample.reshape((1,128))
compressed_sample = cv.PCAProject(sample, mean, eigenvectors)