繁体   English   中英

如何使用python和opencv查找图像的pca?

[英]how to find the pca of an image using python and opencv?

我正在使用svm开发图像分类器。在特征提取阶段,我可以使用pca作为特征。如何使用python和opencv查找图像的pca。我的计划是

  1. 在训练集中找到每个图像的pca并将其存储在数组中。它可能是列表的列表
  2. 将类别标签存储在另一个列表中
  3. 将此作为参数传递给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))       

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM