Python 如何对图像进行预处理,以便SVM能够像处理MNIST数据集一样对其进行处理

Python 如何对图像进行预处理,以便SVM能够像处理MNIST数据集一样对其进行处理,python,machine-learning,image-processing,svm,mnist,Python,Machine Learning,Image Processing,Svm,Mnist,我想使用在MNIST数据集上训练的SVM分析我自己的图像。如何对图像进行预处理,使其被模型接受 dataset = datasets.fetch_openml("mnist_784", version=1) (trainX, testX, trainY, testY) = train_test_split( dataset.data / 255.0, dataset.target.astype("int0"), test_size = 0.33) ap = argparse.Argum

我想使用在MNIST数据集上训练的SVM分析我自己的图像。如何对图像进行预处理,使其被模型接受

dataset = datasets.fetch_openml("mnist_784", version=1)
(trainX, testX, trainY, testY) = train_test_split(
    dataset.data / 255.0, dataset.target.astype("int0"), test_size = 0.33)

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", type=str, default="3scenes",
    help="path to directory containing the '3scenes' dataset")
ap.add_argument("-m", "--model", type=str, default="knn",
    help="type of python machine learning model to use")

args = vars(ap.parse_args())

#user input image to classify

userImage = cv.imread('path_to_image/1.jpg')

#preprocess user image
#...

models = {
    "svm": SVC(kernel="linear"),
}

# train the model
print("[INFO] using '{}' model".format(args["model"]))
model = models[args["model"]]
model.fit(trainX, trainY)

print("[INFO] evaluating image...")
predictions = model.predict(userImage)
print(classification_report(userImage, predictions))

MNIST图像具有以下形状:28x28x1、宽28像素、高28像素和一个颜色通道,即灰度

假设模型采用相同的输入形状,则可以使用以下选项:

import cv2
userImage = cv2.imread('path_to_image/1.jpg')
# resize image to 28x28
userImage = cv2.resize(userImage,(28,28))
# convert to grayscale
userImage = cv2.cvtColor(userImage,cv2.COLOR_BGR2GRAY)
# normalize
userImage /= 255.
根据图像的大小,您可能需要手动选择一个28x28补丁。否则,您可能会丢失图像质量,从而丢失信息

如果模型采用矢量作为输入,则可以使用以下方法在将图像馈送到模型之前展平图像:

userImage = np.reshape(userImage,(784,))

这些代码到底是关于什么的?如果看不到这些图像,很难说。我只想使用在MNIST数据集上训练的模型对我的图像进行分类。我的问题是如何将mi图像转换为MNIST数据集的相同格式,以便我可以将其传递给模型并获得分类。也许这不是它的工作方式,我是这个主题的初学者,请让我知道任何见解。