【发布时间】:2016-07-12 19:57:06
【问题描述】:
我正在使用 svm 开发图像分类器。在特征提取阶段我可以使用 pca 作为特征吗。如何使用 python 和 opencv 找到图像的 pca。我的计划是什么
- 在训练集中查找每张图像的 pca 并将其存储在一个数组中。它可能是列表列表
- 将类标签存储在另一个列表中
- 将此作为参数传递给 svm
我的方向正确吗?请帮助我
【问题讨论】:
标签: python opencv image-processing svm
我正在使用 svm 开发图像分类器。在特征提取阶段我可以使用 pca 作为特征吗。如何使用 python 和 opencv 找到图像的 pca。我的计划是什么
我的方向正确吗?请帮助我
【问题讨论】:
标签: python opencv image-processing svm
是的,你可以做 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))
【讨论】: