hazerd论文中mat格式深度图转提取并转npy文件

该代码遍历指定目录下的所有MAT文件,读取其中的imDepth字段,然后将这些深度数据保存为.npy文件。文件路径信息存储在result_depth列表中,并且每个文件的名称加上.npy后缀。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy
import scipy.io as scio
import os
dataFile = r'C:\Users\Administrator\Desktop\test\hazerd\data\depth'

result_depth = []
for root, _, fnames in sorted(os.walk(dataFile)):
    for fname in fnames:
        path = os.path.join(root, fname)
        result_depth.append(path)

for i in result_depth:
    data = scio.loadmat(i)
    f,n = os.path.split(i)
    name = os.path.join(f,n+'.npy')
    depth = data['imDepth']
    numpy.save(name,depth)
第一部分: # -*- coding: gbk -*- import os import glob import cv2, math import numpy as np from PIL import Image import random import numpy as np import h5py import os from PIL import Image import scipy.io IMG_EXTENSIONS = [ '.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', ] #图片矩阵 def pil_to_np(img_PIL): '''Converts image in PIL format to np.array. From W x H x C [0...255] to C x W x H [0..1] ''' ar = np.array(img_PIL) if len(ar.shape) == 3: ar = ar.transpose(2, 0, 1) else: ar = ar[None, ...] return ar.astype(np.float32) / 255. #矩阵图片 def np_to_pil(img_np): '''Converts image in np.array format to PIL image. From C x W x H [0..1] to W x H x C [0...255] ''' ar = np.clip(img_np * 255, 0, 255).astype(np.uint8) if img_np.shape[0] == 1: ar = ar[0] else: ar = ar.transpose(1, 2, 0) return Image.fromarray(ar) # 判断文件夹中是否有以上类型图片,没有则返回0 def is_image_file(filename): #如果不都为空、0、false,则any()返回true return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) #返回文件夹内文件绝对路径组成的列表 def make_dataset(dir): images = [] assert os.path.isdir(dir), '%s is not a valid directory' % dir # os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) 通过在目录树中游走输出在目录中的文件名,top返回三项(root,dirs,files),分别代表: # 当前正在遍历的这个文件夹的本身的地址; list类型,内容是该文件夹中所有的目录的名字(不包括子目录); list类型,内容是该文件夹中所有的文件(不包括子目录) for root, _, fnames in sorted(os.walk(dir)): for fname in fnames: #print(fname) #拼接出图片的地址,并加入到images列表 path = os.path.join(root, fname) images.append(path) return images def hazy_simu(img_name,depth_or_trans_name,save_dir,pert_perlin = 0,airlight=0.76,is_imdepth=1,visual_range = [0.05, 0.1, 0.2, 0.5, 1]): """ This is the function for haze simulation with the parameters given by the paper: HAZERD: an outdoor scene dataset and benchmark for single image dehazing IEEE Internation Conference on Image Processing, Sep 2017 The paper and additional information on the project are available at: https://labsites.rochester.edu/gsharma/research/computer-vision/hazerd/ If you use this code, please cite our paper. IMPORTANT NOTE: The code uses the convention that pixel locations with a depth value of 0 correspond to objects that are very far and for the simulation of haze these are placed a distance of 2 times the visual range. Authors: Yanfu Zhang: yzh185@ur.rochester.edu Li Ding: l.ding@rochester.edu Gaurav Sharma: gaurav.sharma@rochester.edu Last update: May 2017 python version update : Aug 2021 Authors : Haoying Sun : 1913434222@qq.com parse inputs and set default values Set default parameter values. Some of these are used only if they are not passed in :param img_name: the directory and name of a haze-free RGB image, the name should be in the format of ..._RGB.jpg :param depth_name: the corresponding directory and name of the depth map, in .mat file, the name should be in the format of ..._depth.mat :param save_dir: the directory to save the simulated images :param pert_perlin: 1 for adding perlin noise, default 0 :param airlight: 3*1 matrix in the range [0,1] :param visual_range: a vector of any size :return: image name of hazy image """
最新发布
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值