python图像处理课程设计(代码,训练,测试)
时间: 2023-10-25 17:28:11 浏览: 59
以下是一个完整的 Python 图像处理课程设计的代码示例,包括图像读取和显示、图像保存、图像缩放和旋转、图像裁剪、图像滤波、模型训练和测试等操作:
```python
# 导入必要的库
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# 1. 图像读取和显示
img = Image.open("image.jpg")
plt.imshow(img)
plt.show()
# 2. 图像保存
img.save("new_image.jpg")
# 3. 图像缩放和旋转
width, height = img.size
new_img = img.resize((int(width/2), int(height/2)))
new_img = new_img.rotate(45)
plt.imshow(new_img)
plt.show()
# 4. 图像裁剪
box = (100, 100, 400, 400)
crop_img = img.crop(box)
plt.imshow(crop_img)
plt.show()
# 5. 图像滤波
img_cv = cv2.imread("image.jpg")
blur_img = cv2.GaussianBlur(img_cv, (5, 5), 0)
plt.imshow(blur_img)
plt.show()
# 6. 数据准备
def load_data(data_dir):
images = []
labels = []
for label in os.listdir(data_dir):
path = os.path.join(data_dir, label)
for img_file in os.listdir(path):
img = cv2.imread(os.path.join(path, img_file))
img = cv2.resize(img, (32, 32))
images.append(img)
labels.append(label)
images = np.array(images)
labels = np.array(labels)
return images, labels
images, labels = load_data("data")
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2)
# 对标签进行 one-hot 编码
num_classes = len(set(labels))
y_train = np.eye(num_classes)[y_train.astype(int)]
y_test = np.eye(num_classes)[y_test.astype(int)]
# 7. 模型构建和训练
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=128, epochs=10, validation_data=(X_test, y_test))
# 8. 模型测试
y_pred = np.argmax(model.predict(X_test), axis=-1)
y_true = np.argmax(y_test, axis=-1)
accuracy = accuracy_score(y_true, y_pred)
print("模型准确率:", accuracy)
```
以上代码中,我们首先使用 Python 的 PIL 库和 matplotlib 库实现了图像的读取、显示和保存,使用 PIL 库实现了图像的缩放、旋转和裁剪,使用 Python 的 OpenCV 库实现了图像的滤波操作。然后我们使用了 scikit-learn 库的 train_test_split 函数将数据集划分为训练集和测试集,并对标签进行 one-hot 编码。接着我们使用 TensorFlow 的 Keras API 构建了一个简单的卷积神经网络模型,并使用 fit 函数进行训练。最后我们使用 accuracy_score 函数计算了模型在测试集上的准确率。
当然,这只是一个简单的示例,根据实际需求,可以对模型进行更加复杂的构建和训练,使用更加高级的图像处理技术等,以得到更好的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)