我现在有灰度图的bmp图像和二值图的bmp图像,其中二值图我是直接拿来当成label图,我想把这两个文件直接划分成训练集、验证集和测试集,可以给出相应代码吗
时间: 2024-04-19 19:26:18 浏览: 107
0-1change.rar_C 图像二值化_bmp 图像处理
当您有灰度图的BMP图像和对应的二值图作为标签时,您可以使用Python中的一些库来划分数据集。下面是一个示例代码,使用scikit-learn库中的train_test_split函数来进行数据集划分:
```python
import os
import random
from sklearn.model_selection import train_test_split
# 设置图像和标签的文件夹路径
image_folder = "path_to_gray_images_folder"
label_folder = "path_to_binary_images_folder"
# 获取图像和标签文件列表
image_files = os.listdir(image_folder)
label_files = os.listdir(label_folder)
# 确保图像和标签文件数量相同
assert len(image_files) == len(label_files), "Number of images and labels do not match."
# 打乱图像和标签文件顺序
random.shuffle(image_files)
random.shuffle(label_files)
# 划分数据集
train_ratio = 0.7 # 训练集比例
val_ratio = 0.15 # 验证集比例
test_ratio = 0.15 # 测试集比例
# 根据比例计算划分数据集的数量
train_count = int(len(image_files) * train_ratio)
val_count = int(len(image_files) * val_ratio)
test_count = len(image_files) - train_count - val_count
# 划分图像和标签文件列表为训练集、验证集和测试集
train_images, val_images, train_labels, val_labels = train_test_split(image_files[:train_count], label_files[:train_count], test_size=val_count)
test_images, test_labels = image_files[train_count+val_count:], label_files[train_count+val_count:]
# 打印数据集信息
print("Train set: {} images".format(len(train_images)))
print("Validation set: {} images".format(len(val_images)))
print("Test set: {} images".format(len(test_images)))
# 在每个数据集中移动图像和标签文件到相应的文件夹
def move_files(files, src_folder, dest_folder):
for file in files:
src_path = os.path.join(src_folder, file)
dest_path = os.path.join(dest_folder, file)
os.rename(src_path, dest_path)
# 移动图像和标签文件到训练集文件夹
move_files(train_images, image_folder, "path_to_train_images_folder")
move_files(train_labels, label_folder, "path_to_train_labels_folder")
# 移动图像和标签文件到验证集文件夹
move_files(val_images, image_folder, "path_to_val_images_folder")
move_files(val_labels, label_folder, "path_to_val_labels_folder")
# 移动图像和标签文件到测试集文件夹
move_files(test_images, image_folder, "path_to_test_images_folder")
move_files(test_labels, label_folder, "path_to_test_labels_folder")
```
请根据您的实际情况修改代码中的文件夹路径,并确保文件夹和文件命名的正确性。这段代码将根据指定的比例划分图像和标签文件为训练集、验证集和测试集,并将它们移动到相应的文件夹中。
阅读全文