我正在使用svm开发图像分类器。在特征提取阶段,我可以使用pca作为特征。如何使用python和opencv查找图像的pca。我的计划是
我朝正确的方向前进吗,请帮帮我
最佳答案
是的,您可以使用PCA + SVM,有人可能会认为PCA不是最好的功能,或者SVM不是最好的分类算法。但是嘿,有个好的开始比坐在那里好。
要使用OpenCV进行PCA,请尝试以下操作(我尚未验证代码,只是为了让您有所了解):
import os
import cv2
import numpy as np
# Construct the input matrix
in_matrix = None
for f in os.listdir('dirpath'):
# Read the image in as a gray level image. Some modifications
# of the codes are needed if you want to read it in as a color
# image. For simplicity, let's use gray level images for now.
im = cv2.imread(os.path.join('dirpath', f), cv2.IMREAD_GRAYSCALE)
# Assume your images are all the same size, width w, and height h.
# If not, let's resize them to w * h first with cv2.resize(..)
vec = im.reshape(w * h)
# stack them up to form the matrix
try:
in_matrix = np.vstack((in_matrix, vec))
except:
in_matrix = vec
# PCA
if in_matrix is not None:
mean, eigenvectors = cv2.PCACompute(in_matrix, np.mean(in_matrix, axis=0).reshape(1,-1))
关于python - 如何使用python和opencv查找图像的pca?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36213533/