由于groundtruth是以mat格式存储的,所以有时用的时候需要先转换成jpg格式。从网上看了一下没找到能直接用的,于是参考bolg写了一个处理的代码。
将代码中的root换成你数据集实际所在位置,然后直接运行代码,就可以在你数据集文件夹 BSR\BSDS500\data 中多出一个GT_convert文件夹,就是转换后的结果。
from scipy importioimportscipyimportos###运行时需要改变root值为BSD500所在的相应根目录
root = 'F:\\lyc\\Data_Set\\BSR\\BSDS500'PATH= os.path.join(root,'data\\groundTruth')for sub_dir_name in ['train','test','val']:
sub_pth=os.path.join(PATH,sub_dir_name)##为生成的图片新建个文件夹保存
save_pth = os.path.join(root,'data\\GT_convert',sub_dir_name)
os.makedirs(save_pth,exist_ok=True)print('开始转换'+sub_dir_name+'文件夹中内容')for filename inos.listdir(sub_pth):#读取mat文件中所有数据
#mat文件里面是以字典形式存储的数据
#包括 dict_keys(['__globals__', 'groundTruth', '__header__', '__version__'])
#我们要用到'groundTruth']中的轮廓
#x['groundTruth'][0][0][0][0][1]为轮廓
#x['groundTruth'][0][0][0][0][0]为分割图
data =io.loadmat(os.path.join(sub_pth,filename))
edge_data= data['groundTruth'][0][0][0][0][1]#存储的是归一化后的数据:0
#因此需要还原回0
edge_data_255 = edge_data * 255new_img_name= filename.split('.')[0]+'.jpg'
print(new_img_name)
scipy.misc.imsave(os.path.join(save_pth,new_img_name), edge_data_255)#保存图片
参考其他bolg对数据集进行的处理,但是crf对其中中间模糊像素不进行处理,一直以为是作者自己对数据集进行扩充的,后来发现不是,如果自己扩充他就不会与未扩充的方法进行比较,原来多个ground truth就藏在这个数据集中
a = data['groundTruth'][0][1][0][0][1]*255
b = data['groundTruth'][0][2][0][0][1]*255
c = data['groundTruth'][0][3][0][0][1]*255
d = data['groundTruth'][0][4][0][0][1]*255
e = data['groundTruth'][0][5][0][0][1]*255
f = data['groundTruth'][0][0][0][0][1]*255
分别是6个ground truth。BSDS500数据集提出时只说是对BSDS300进行扩充,因此要看数据集具体构成要看BSDS300的论文。
另外:以mat存储的矩阵是可以用python直接读取的,因此并不一定要先转换成图片存储:
import scipy.io as scio
mat_path = './test.mat'
mat = scio.loadmat(mat_path)
就可以获取mat矩阵了