def get_Image_dim_len(png_dir: str,jpg_dir:str): png = Image.open(png_dir) png_w,png_h=png.width,png.height #若第十行报错,说明jpg图片没有对应的png图片 png_dim_len = len(np.array(png).shape) assert png_dim_len==2,"提示:存在三维掩码图" jpg=Image.open(jpg_dir) jpg = ImageOps.exif_transpose(jpg) jpg.save(jpg_dir) jpg_w,jpg_h=jpg.width,jpg.height print(jpg_w,jpg_h,png_w,png_h) assert png_w==jpg_w and png_h==jpg_h,print("提示:%s mask图与原图宽高参数不一致"%(png_dir)) """2.读取单个图像均值和方差""" def pixel_operation(image_path: str): img = cv.imread(image_path, cv.IMREAD_COLOR) means, dev = cv.meanStdDev(img) return means,dev """3.分割数据集,生成label文件""" # 原始数据集 ann上一级 data_root = './work/voc_data02' #图像地址 image_dir="./JPEGImages" # ann图像文件夹 ann_dir = "./SegmentationClass" # txt文件保存路径 split_dir = './ImageSets/Segmentation' mmengine.mkdir_or_exist(osp.join(data_root, split_dir)) png_filename_list = [osp.splitext(filename)[0] for filename in mmengine.scandir( osp.join(data_root, ann_dir), suffix='.png')] jpg_filename_list=[osp.splitext(filename)[0] for filename in mmengine.scandir( osp.join(data_root, image_dir), suffix='.jpg')] assert len(jpg_filename_list)==len(png_filename_list),"提示:原图与掩码图数量不统一" print("数量检查无误") for i in range(10): random.shuffle(jpg_filename_list) red_num=0 black_num=0 with open(osp.join(data_root, split_dir, 'trainval.txt'), 'w+') as f: length = int(len(jpg_filename_list)) for line in jpg_filename_list[:length]: pngpath=osp.join(data_root,ann_dir,line+'.bmp') jpgpath=osp.join(data_root,image_dir,line+'.bmp') get_Image_dim_len(pngpath,jpgpath) img=cv.imread(pngpath,cv.IMREAD_GRAYSCALE) red_num+=len(img)*len(img[0])-len(img[img==0]) black_num+=len(img[img==0]) f.writelines(line + '\n') value=0 train_mean,train_dev=[[0.0,0.0,0.0]],[[0.0,0.0,0.0]] with open(osp.join(data_root, split_dir, 'train.txt'), 'w+') as f: train_length = int(len(jpg_filename_list) * 7/ 10) for line in jpg_filename_list[:train_length]: jpgpath=osp.join(data_root,image_dir,line+'.bmp') mean,dev=pixel_operation(jpgpath) train_mean+=mean train_dev+=dev f.writelines(line + '\n') with open(osp.join(data_root, split_dir, 'val.txt'), 'w+') as f: for line in jpg_filename_list[train_length:]: jpgpath=osp.join(data_root,image_dir,line+'.bmp') mean,dev=pixel_operation(jpgpath) train_mean+=mean train_dev+=dev f.writelines(line + '\n') 帮我把这段代码改成bmp图像可以制作数据集的代码
时间: 2024-04-13 11:28:24 浏览: 186
mempool_hook_userdef_0924
当然,我可以帮你将代码修改为支持bmp图像的数据集制作代码。请看下面的修改后的代码:
```python
from PIL import Image
import os
import random
import cv2 as cv
import numpy as np
def get_Image_dim_len(png_dir: str,jpg_dir:str):
png = Image.open(png_dir)
png_w,png_h=png.width,png.height
#若第十行报错,说明jpg图片没有对应的png图片
png_dim_len = len(np.array(png).shape)
assert png_dim_len==2,"提示:存在三维掩码图"
jpg=Image.open(jpg_dir)
jpg = jpg.transpose(Image.FLIP_LEFT_RIGHT) # 翻转图像
jpg.save(jpg_dir)
jpg_w,jpg_h=jpg.width,jpg.height
print(jpg_w,jpg_h,png_w,png_h)
assert png_w==jpg_w and png_h==jpg_h,print("提示:%s mask图与原图宽高参数不一致"%(png_dir))
def pixel_operation(image_path: str):
img = cv.imread(image_path, cv.IMREAD_COLOR)
means, dev = cv.meanStdDev(img)
return means,dev
data_root = './work/voc_data02'
image_dir="./JPEGImages"
ann_dir = "./SegmentationClass"
split_dir = './ImageSets/Segmentation'
os.makedirs(os.path.join(data_root, split_dir), exist_ok=True)
png_filename_list = [os.path.splitext(filename)[0] for filename in os.listdir(os.path.join(data_root, ann_dir)) if filename.endswith('.bmp')]
jpg_filename_list=[os.path.splitext(filename)[0] for filename in os.listdir(os.path.join(data_root, image_dir)) if filename.endswith('.bmp')]
assert len(jpg_filename_list)==len(png_filename_list),"提示:原图与掩码图数量不统一"
print("数量检查无误")
random.shuffle(jpg_filename_list)
red_num=0
black_num=0
with open(os.path.join(data_root, split_dir, 'trainval.txt'), 'w+') as f:
length = int(len(jpg_filename_list))
for line in jpg_filename_list[:length]:
pngpath=os.path.join(data_root,ann_dir,line+'.bmp')
jpgpath=os.path.join(data_root,image_dir,line+'.bmp')
get_Image_dim_len(pngpath,jpgpath)
img=cv.imread(pngpath,cv.IMREAD_GRAYSCALE)
red_num+=len(img)*len(img[0])-len(img[img==0])
black_num+=len(img[img==0])
f.writelines(line + '\n')
train_mean,train_dev=[[0.0,0.0,0.0]],[[0.0,0.0,0.0]]
with open(os.path.join(data_root, split_dir, 'train.txt'), 'w+') as f:
train_length = int(len(jpg_filename_list) * 7/ 10)
for line in jpg_filename_list[:train_length]:
jpgpath=os.path.join(data_root,image_dir,line+'.bmp')
mean,dev=pixel_operation(jpgpath)
train_mean+=mean
train_dev+=dev
f.writelines(line + '\n')
with open(os.path.join(data_root, split_dir, 'val.txt'), 'w+') as f:
for line in jpg_filename_list[train_length:]:
jpgpath=os.path.join(data_root,image_dir,line+'.bmp')
mean,dev=pixel_operation(jpgpath)
train_mean+=mean
train_dev+=dev
f.writelines(line + '\n')
```
希望这可以帮到你!如果你还有其他问题,请随时提问。
阅读全文