← 返回首页
状态:已完成

【Python 代码】实现图像读取与预处理函数

```python import cv2 import numpy as np def read_and_preprocess_image(image_path, target_size=(224, 224)): # 读取图像 image = cv2.imread(image_path) if image is None: raise FileNotFoundError(f"Image not found: {image_path}") # 转换为RGB image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 调整大小 image = cv2.resize(image, target_size) # 归一化 image = image.astype(np.float32) / 255.0 return image ```