img_path = os.path.join(img_dir, filename)
时间: 2024-03-06 17:33:33 浏览: 72
这行代码的作用是将一个文件名拼接到一个文件夹路径中,生成完整的文件路径。
具体来说,os.path.join() 方法会将多个路径组合成一个完整的路径,它会根据操作系统的不同自动选择使用正确的路径分隔符。在这里,我们将文件夹路径 img_dir 和文件名 filename 组合起来,生成完整的图片路径 img_path。这个路径可以用于读取或写入图片文件。
相关问题
filedir= "D:\\python projects\\SegNet_PyTorch_0\\src\\Camvid\\val_raw\\" #输入文件路径 path=os.path.join(filedir) img_list=os.listdir(path)
这段代码的作用是获取指定文件夹下的所有文件名,并将文件名存储在一个列表中。
首先,`filedir`是一个字符串变量,表示文件夹的路径。在这个例子中,路径是"D:\\python projects\\SegNet_PyTorch_0\\src\\Camvid\\val_raw\\"。
然后,`os.path.join()`函数用于将路径字符串与文件名拼接在一起,生成完整的文件路径。在这里,`path`变量存储了拼接后的完整路径。
最后,`os.listdir()`函数用于获取指定路径下的所有文件和文件夹的名称,并将它们存储在一个列表中。在这个例子中,`img_list`变量存储了文件夹中所有文件的名称。
如果你想获取文件夹中的所有文件的完整路径,可以使用以下代码:
```python
filedir = "D:\\python projects\\SegNet_PyTorch_0\\src\\Camvid\\val_raw\\"
img_list = [os.path.join(filedir, filename) for filename in os.listdir(filedir)]
```
这样,`img_list`列表中的每个元素都是一个完整的文件路径。
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图像可以制作数据集的代码
当然,我可以帮你将代码修改为支持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')
```
希望这可以帮到你!如果你还有其他问题,请随时提问。
阅读全文