​​​​​​​目录

完整代码matlab:

python版本:

使用时记得下载相关数据集:

效果:

官方:

下一步:


完整代码matlab:

参见:https://blog.csdn.net/Crystal_remember/article/details/119529529

python版本:

# -*- 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.ioIMG_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()返回truereturn 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 imagesdef 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 bythe paper:HAZERD: an outdoor scene dataset and benchmark for single image dehazingIEEE Internation Conference on Image Processing, Sep 2017The 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 adepth value of 0 correspond to objects that are very far and for thesimulation of haze these are placed a distance of 2 times the visualrange.Authors:Yanfu Zhang: yzh185@ur.rochester.eduLi Ding: l.ding@rochester.eduGaurav Sharma: gaurav.sharma@rochester.eduLast update: May 2017python version update : Aug 2021Authors :Haoying Sun : 1913434222@qq.comparse inputs and set default valuesSet default parameter values. Some of these are used only if they are notpassed in:param img_name: the directory and name of a haze-free RGB image, the nameshould 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"""visual_range = [0.05, 0.1, 0.2, 0.5, 1]  #  visual range in km #可自行调整,或者使用range函数设置区间,此时需要修改beta_param,尚未研究beta_param = 3.912     #Default beta parameter corresponding to visual range of 1000m# A = np.zeros((1, 1, 3))# A[0, 0, 0] = airlight# A[0, 0, 1] = airlight# A[0, 0, 2] = airlightA = airlightif save_dir != '':if not os.path.exists(save_dir):os.makedirs(save_dir)print('Simulating hazy image for:{}'.format(img_name))VR = random.choice(visual_range)print('Viusal value: {} km'.format(VR) )im1 = cv2.imread(img_name)img_pil = pil_to_np(im1)#convert sRGB to linear RGBI = srgb2lrgb(img_pil)if is_imdepth:depths = get_depth(depth_or_trans_name)d = depths/1000   # convert meter to kilometer#Set regions where depth value is set to 0 to indicate no valid depth to#a distance of two times the visual range. These regions typically#correspond to sky areasd = np.where(d==0,2*VR,d)if pert_perlin:d = d * ((perlin_noise(np.zeros(d.shape)) - 0.5) + 1)#convert depth map to transmissionbeta = beta_param / VRbeta = np.ones(d.shape) * betatransmission = np.exp((-beta*d))transmission_3 = np.array([transmission,transmission,transmission])#Obtain simulated linear RGB hazy image.Eq. 3 in the HazeRD paperIc = transmission_3 * I+ (1 - transmission_3) * Aelse:Ic = pil_to_np(depth_or_trans_name) * I + (1 - pil_to_np(depth_or_trans_name)) * A# convert linear RGB to sRGBI2 = lrgb2srgb(Ic)haze_img = np_to_pil(I2)return haze_imgdef perlin_noise(im, decay=2):"""——————————————————————————————————————————————————————————————————————————————非均匀雾(柏林噪声)通常用柏林噪声来模拟大气湍流引起的非均匀雾,论文Towards Simulating Foggy and Hazy Images and Evaluating Their Authenticity生成三个不同频率参数(f=130、60、10)的三维柏林噪声,并通过以下方法组合:noise = 1/3 Sum_i(noise_i/2^(i-1)),i从1到3求得的noise与消光系数相乘,带入到Eq.2,模拟非均匀雾。I = Iex + Op*Ial    EQ.1Iex = Ipexp(-βex * rp)   EQ.2O = 1 - exp(-βex * rp)   EQ.3Iex:目标反射光经过大气中微粒衰减;Ial:环境光经过微粒散射,形成背景光; Op:模糊度;βex:消光系数——————————————————————————————————————————————————————————————————————————————_This is the function for adding perlin noise to the depth map. It is asimplified implementation of the paper:an image sunthesizerKen Perlin, SIGGRAPH, Jul. 1985The bicubic interpolation is used, compared to the original version.Reference:HAZERD: an outdoor scene dataset and benchmark for single image dehazingIEEE International Conference on Image Processing, Sep 2017The 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.Input:im: depth mapvarargin{1}: decay termOutput:im: result of transmission with perlin noise addedAuthors:Yanfu Zhang: yzh185@ur.rochester.eduLi Ding: l.ding@rochester.eduGaurav Sharma: gaurav.sharma@rochester.eduLast update: May 2017:return:"""(h, w, c) = im.shapei = 1l_bound = min(h,w)while i <= l_bound:#d = imresize(randn(i, i)*decay, im.shape, 'bicubic')d = cv2.resize(np.random.randn(i,i)*decay, im.shape, interpolation=cv2.INTER_CUBIC)im = im+di = i*2 im = (im - np.min(im)) / (np.max(im) - np.min(im))return imdef srgb2lrgb(I0):gamma = ((I0 + 0.055) / 1.055)**2.4scale = I0 / 12.92return np.where (I0 > 0.04045, gamma, scale)def lrgb2srgb(I1):gamma =  1.055*I1**(1/2.4)-0.055scale = I1 * 12.92return np.where (I1 > 0.0031308, gamma, scale)#获取深度矩阵
def get_depth(depth_or_trans_name):#depth_or_trans_name为mat类型文件或者img类型文件地址data = scipy.io.loadmat(depth_or_trans_name)depths = data['imDepth'] #深度变量#print(data.keys())  #打印mat文件中所有变量depths = np.array(depths)return depths
def AddHaze1(img):img = cv2.imread(img)img = np.array(img)img_f = img(row, col, chs) = img.shapeA = 0.5  # 亮度beta = 0.08  # 雾的浓度size = math.sqrt(max(row, col))  # 雾化尺寸center = (row // 2, col // 2)  # 雾化中心for j in range(row):for l in range(col):d = -0.04 * math.sqrt((j - center[0]) ** 2 + (l - center[1]) ** 2) + sizetd = math.exp(-beta * d)img_f[j][l][:] = img_f[j][l][:] * td + A * (1 - td)return Image.fromarray(img_f.astype(np.uint8))#请修改以下路径
img_dir = 'I:\HazeRD\data\img'
depth_dir = 'I:\HazeRD\data\depth'
save_dir = 'I:\HazeRD\data\depth\simu' #存放生成雾图结果
img_list = make_dataset(img_dir)
depth_list = make_dataset(depth_dir)# # 生成雾图结果可视化
# a = hazy_simu(img_list[0],depth_list[0],save_dir)
# Image.Image.show(a)
# cv2.waitKey(1000)# #原始图片可视化
# img_name= img_list[0]
# im1 = cv2.imread(img_name)
# im1 = Image.fromarray(im1.astype(np.uint8))
# Image.Image.show(im1)
# cv2.waitKey(1000)# #深度图可视化
# depth_name = depth_list[0]
# depths = get_depth(depth_name)
# im1 = Image.fromarray(depths.astype(np.uint8))
# Image.Image.show(im1)
# cv2.waitKey(1000)

使用时记得下载相关数据集:

下载地址见底部,注意输入数据格式一致,请下载原始数据!

目录安排如下:

效果:

原始图:

深度图:

能见度为1KM时的雾图情况:

能见度为0.2KM的雾图情况:

官方:

论文: HazeRD: An Outdoor Scene Dataset and Benchmark for Single Image Dehazing

网址: https://labsites.rochester.edu/gsharma/research/computer-vision/hazerd/

下一步:

代码注释、优化和精简!

输入数据格式限制问题优化!

透射图作为输入时可能会出现的问题优化!

【python】通过深度图生成雾图(HAZERD)相关推荐

  1. python从语音生成语谱图

    python从语音生成语谱图 文章目录 python从语音生成语谱图 生成语谱图的过程 引入必要的库 语音生成语谱图 结果图 语音信号的原始数据 语音信号转为语谱图 生成语谱图的过程 参考文档 预增强 ...

  2. python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】

    文章目录 API ColormapTypes 完整应用代码[将深度图的黑白图映射为彩色图] map原理 能否map CV_24UC3的? API def applyColorMap(src, colo ...

  3. 乐视三合一体感摄像头Astra pro开发记录1(深度图、彩色图及点云简单显示)

    在某鱼上淘的乐视三合一体感摄像头,捡漏价九十几块,买来玩玩. 网上已经有一些关于此款摄像头的开发资料. 官方的开发资料:官网链接 按官方网站以及其他帖子,下载并安装相机的驱动和SDK,不难配置好相机. ...

  4. python之matplotlib制作雷达图

    python之matplotlib制作雷达图 示例代码: import numpy as np import matplotlib.pyplot as plt import matplotlibmat ...

  5. Python使用matplotlib可视化箱图、seaborn中的boxplot函数可视化分组箱图、在箱图中添加抖动数据点(Dot + Box Plot)

    Python使用matplotlib可视化箱图.seaborn中的boxplot函数可视化分组箱图.在箱图中添加抖动数据点(Dot + Box Plot) 目录

  6. Python使用matplotlib可视化Treemap图、treemap将分层数据显示为一组嵌套矩形,每一组都用一个矩形表示,该矩形的面积与其值成正比(Treemap)

    Python使用matplotlib可视化Treemap图.treemap将分层数据显示为一组嵌套矩形,每一组都用一个矩形表示,该矩形的面积与其值成正比(Treemap) 目录

  7. python使用matplotlib可视化线图(line plot)、移除可视化结果的所有坐标轴信息(remove all axis in matplotlib graph)

    python使用matplotlib可视化线图(line plot).移除可视化结果的所有坐标轴信息(remove all axis in matplotlib graph) 目录

  8. Python使用matplotlib可视化面积图(Area Chart)、通过给坐标轴和曲线之间的区域着色可视化面积图、在面积图的指定区域添加箭头和数值标签

    Python使用matplotlib可视化面积图(Area Chart).通过给坐标轴和曲线之间的区域着色可视化面积图.在面积图的指定区域添加箭头和数值标签 目录

  9. Python使用matplotlib可视化小提琴图、seaborn中的violinplot函数可视化多分类变量的小提琴图(Violin Plot)

    Python使用matplotlib可视化小提琴图.seaborn中的violinplot函数可视化多分类变量的小提琴图(Violin Plot) 目录

最新文章

  1. Micropython TPYBoard v102 自动浇花实验
  2. 两次深度debug经历,希望大家不要踩坑
  3. 函数的参数-列表使用+=本质上是调用extend方法
  4. 使用Lightbox制作照片条
  5. mysql之group_concat函数
  6. Python实现经典七种排序算法
  7. Windows中使用包管理器(类似于apt/yum的) - Chocolatey
  8. 实现截图页面并导出word
  9. 使用canto+w3m实现在控制台上完美阅读RSS
  10. Win7下如何显示文件的扩展名?
  11. 从计算机复试看中国教育——一个面试官的经历
  12. 解决mapper.xml不在resource的时出现的错误nvalid bound statement (not found): com.dxl.system.mapper
  13. 环比同比YOY\QoQ及QQ\PP图Q-Q\P-P…
  14. 杰奇cms,杰奇cms程序,杰奇cms建站方法
  15. Mac book air在浏览器中显示flash过期问题
  16. latex 矩阵分块(block matrix)
  17. 长程蓝牙温湿度及光照度传感器 , 带四通道开关及指示灯(集成太阳能微能量采集功能)
  18. android 阿拉伯数字转汉字,Android将阿拉伯数字转换为英文数字
  19. 配置java运行时环境时出现could not find java.dll
  20. ( 纪中)1296. 城墙【暴力】

热门文章

  1. 爬取豆瓣电影中:华语、欧美、韩国、日本电影每个标签下按评价排序的全部电影。
  2. 【计算机毕业设计】网上购物商城
  3. 编程中变量命名方法:驼峰命名法?
  4. 股票数据写入MySQL——从零到实盘9
  5. Android 频道管理仿今日头条
  6. NBA篮球可视化分析网站
  7. 竞赛中的STL库函数使用
  8. 黑鲨3pro手机如何升级鸿蒙5g系统,黑鲨 3 /黑鲨 3 Pro 到港:最平 5G 电竞手机上手测试!...
  9. 计算机的广义定义和狭义定义,计算机辅助制造有广义和狭义两种定义.doc
  10. Word公式键入——UnicodeMath语法对照简表