用python写一个图像处理svm多分类代码
时间: 2024-04-29 08:26:47 浏览: 103
svm多分类的python版代码
好的,以下是一个简单的Python图像处理SVM多分类代码示例:
```python
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
# Load data
images = []
labels = []
for i in range(1, 11):
for j in range(1, 11):
img = cv2.imread(f'data/{i}/{j}.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (64, 64))
images.append(img)
labels.append(i)
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)
# Preprocess data
X_train = np.array(X_train).reshape(len(X_train), -1)
X_test = np.array(X_test).reshape(len(X_test), -1)
y_train = np.array(y_train)
y_test = np.array(y_test)
# Train SVM model
svm = SVC(kernel='linear', C=1.0, random_state=42)
svm.fit(X_train, y_train)
# Evaluate model on test set
accuracy = svm.score(X_test, y_test)
print(f'Accuracy: {accuracy}')
```
请注意,此代码仅用于演示目的,并且需要您根据自己的需求进行修改和调整。此外,你需要准备一个数据集和对模型进行优化,从而实现更好的分类效果。
阅读全文