图像去雾matlab代码,图像处理去雾

  图像去雾matlab代码,图像处理去雾

  本文将利用《bringingoldphotosbacktolife》 的开源代码,并在此基础上进行修改,从而实现图像去雾的效果,感兴趣的小伙伴可以学习一下

  

目录
修改部分训练测试数据集下载地址

  

修改部分

  我利用该代码进行了去雾任务,并对原始代码进行了增删,去掉了人脸提取并对提取人脸美化的部分,如下图

  增改了一些数据处理代码,Create_Bigfile2.py和Load_Bigfilev2为特定任务需要加的代码,这里数据处理用的是原始方法,即将训练数据打包成一个文件,一次性载入,可能会内存爆炸。去雾的如下

  另外,为了节省内存,可以不使用原始方法,我改写了在线_数据集_ for _ odl _照片。巴拉圭文件

  用于我的加雾论文,此时可以不使用原始的创建_大文件和Load_bigfile代码如下

  #版权所有(三)微软公司。

  #根据麻省理工学院许可证授权。

  导入os.path

  导入超正析象管

  导入活力文件

  从数据库数据集导入BaseDataset,获取参数,获取转换,规范化

  从数据.图像_文件夹导入制作数据集

  来自数据Load_Bigfile导入BigFileMemoryLoader

  将火炬视觉.转换作为定期融资计划导入

  从火炬视觉.转换导入函数为消防

  从太平航运进口图片

  将火炬视觉.转换作为转换导入

  将数组作为铭牌导入

  随机导入

  导入cv2

  从超正析象管导入字节

  #图片转矩阵

  太平航运国际机场:

  ""将太平航运格式的图像转换为np .数组

  来自宽x高x高[0.255]到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[无,]

  返回ar。astype(NP。浮动32)/255 .

  #矩阵转图片

  定义(img_np):

  ""将np .数组格式的图像转换为太平航运图像。

  从C x W x H [0.1]到宽x高x高[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)

  返回来自数组(ar)

  ##

  #以下合成噪声图片

  ##

  def合成_盐_胡椒(图片,数量,盐_胡椒):

  #给PIL,还吵闹的太平航运

  img_pil=pil_to_np(图像)

  out=img_pil.copy()

  p=数量

  q=盐和胡椒

  flipped=np.random.choice([True,False],size=img_pil.shape,

  p=[p,1 - p])

  咸=NP。随机的。选择([真,假],size=img_pil.sh

  ape,

   p=[q, 1 - q])

   peppered = ~salted

   out[flipped & salted] = 1

   out[flipped & peppered] = 0.

   noisy = np.clip(out, 0, 1).astype(np.float32)

   return np_to_pil(noisy)

  def synthesize_gaussian(image,std_l,std_r):

   ## Give PIL, return the noisy PIL

   img_pil=pil_to_np(image)

   mean=0

   std=random.uniform(std_l/255.,std_r/255.)

   gauss=np.random.normal(loc=mean,scale=std,size=img_pil.shape)

   noisy=img_pil+gauss

   noisy=np.clip(noisy,0,1).astype(np.float32)

   return np_to_pil(noisy)

  def synthesize_speckle(image,std_l,std_r):

   ## Give PIL, return the noisy PIL

   img_pil=pil_to_np(image)

   mean=0

   std=random.uniform(std_l/255.,std_r/255.)

   gauss=np.random.normal(loc=mean,scale=std,size=img_pil.shape)

   noisy=img_pil+gauss*img_pil

   noisy=np.clip(noisy,0,1).astype(np.float32)

   return np_to_pil(noisy)

  #图片缩小

  def synthesize_low_resolution(img):

   w,h=img.size

   new_w=random.randint(int(w/2),w)

   new_h=random.randint(int(h/2),h)

   img=img.resize((new_w,new_h),Image.BICUBIC)

   if random.uniform(0,1)<0.5:

   img=img.resize((w,h),Image.NEAREST)

   else:

   img = img.resize((w, h), Image.BILINEAR)

   return img

  #处理图片

  def convertToJpeg(im,quality):

   #在内存中读写bytes

   with BytesIO() as f:

   im.save(f, format=JPEG,quality=quality)

   f.seek(0)

   #使用Image.open读出图像,然后转换为RGB通道,去掉透明通道A

   return Image.open(f).convert(RGB)

  #由(高斯)噪声生成图片

  def blur_image_v2(img):

   x=np.array(img)

   kernel_size_candidate=[(3,3),(5,5),(7,7)]

   kernel_size=random.sample(kernel_size_candidate,1)[0]

   std=random.uniform(1.,5.)

   #print("The gaussian kernel size: (%d,%d) std: %.2f"%(kernel_size[0],kernel_size[1],std))

   blur=cv2.GaussianBlur(x,kernel_size,std)

   return Image.fromarray(blur.astype(np.uint8))

  #由以上噪声函数随机生成含有噪声的图片

  def online_add_degradation_v2(img):

   task_id=np.random.permutation(4)

   for x in task_id:

   if x==0 and random.uniform(0,1)<0.7:

   img = blur_image_v2(img)

   if x==1 and random.uniform(0,1)<0.7:

   flag = random.choice([1, 2, 3])

   if flag == 1:

   img = synthesize_gaussian(img, 5, 50)

   if flag == 2:

   img = synthesize_speckle(img, 5, 50)

   if flag == 3:

   img = synthesize_salt_pepper(img, random.uniform(0, 0.01), random.uniform(0.3, 0.8))

   if x==2 and random.uniform(0,1)<0.7:

   img=synthesize_low_resolution(img)

   if x==3 and random.uniform(0,1)<0.7:

   img=convertToJpeg(img,random.randint(40,100))

   return img

  #根据mask生成带有折痕的图片

  #原论文中对于一些复杂的折痕会出现处理不佳的情况,在此进行改进,而不是简单进行加mask,

  def irregular_hole_synthesize(img,mask):

   img_np=np.array(img).astype(uint8)

   mask_np=np.array(mask).astype(uint8)

   mask_np=mask_np/255

   img_new=img_np*(1-mask_np)+mask_np*255

   hole_img=Image.fromarray(img_new.astype(uint8)).convert("RGB")

   #L为灰度图像

   return hole_img,mask.convert("L")

  #生成全黑三通道图像mask

  def zero_mask(size):

   x=np.zeros((size,size,3)).astype(uint8)

   mask=Image.fromarray(x).convert("RGB")

   return mask

  ######################################### my ################################

  class UnPairOldPhotos_SRv2(BaseDataset): ## Synthetic + Real Old

   def initialize(self, opt):

   self.opt = opt

   self.isImage = domainA in opt.name

   self.task = old_photo_restoration_training_vae

   self.dir_AB = opt.dataroot

   # 载入VOC以及真实灰度、彩色图

   #dominA

   if self.isImage:

   path_clear = r/home/vip/shy/ots/clear_images/ ##self.opt.path_clear

   path_old = r/home/vip/shy/Bringing-Old-Photos-Back-to-Life_v1/voc2007/Real_RGB_old ##self.opt.path_old

   path_haze = r/home/vip/shy/ots/hazy/ ##self.opt.path_haze

   #self.load_img_dir_L_old=os.path.join(self.dir_AB,"Real_L_old.bigfile")

   self.load_img_dir_RGB_old=path_old

   self.load_img_dir_clean=path_clear

   self.load_img_dir_Synhaze=path_haze

   self.img_dir_Synhaze = os.listdir(self.load_img_dir_Synhaze)

   self.loaded_imgs_Synhaze=[os.path.join(self.load_img_dir_Synhaze,img) for img in self.img_dir_Synhaze]

   self.img_dir_RGB_old = os.listdir(self.load_img_dir_RGB_old)

   self.loaded_imgs_RGB_old = [os.path.join(self.load_img_dir_RGB_old,img) for img in self.img_dir_RGB_old]

   self.loaded_imgs_clean = []

   for path_i in self.loaded_imgs_Synhaze:

   p,n = os.path.split(path_i)

   pre,ex = os.path.splitext(n)

   clear_pre = pre.split(_)[0]

   clear_path = os.path.join(path_clear,clear_pre+ex)

   self.loaded_imgs_clean.append(clear_path)

   print(________________filter whose size <256)

   self.filtered_imgs_clean = []

   self.filtered_imgs_Synhaze = []

   self.filtered_imgs_old = []

   print(________________now filter syn and clean size <256)

   for i in range(len(self.loaded_imgs_Synhaze)):

   img_name_syn = self.loaded_imgs_Synhaze[i]

   img = Image.open(img_name_syn)

   h, w = img.size

   img_name_clear = self.loaded_imgs_clean[i]

   if h < 256 or w < 256:

   continue

   self.filtered_imgs_clean.append(img_name_clear)

   self.filtered_imgs_Synhaze.append(img_name_syn)

   print(________________now filter old size <256)

   for i in range(len(self.loaded_imgs_RGB_old)):

   img_name_old = self.loaded_imgs_RGB_old[i]

   img = Image.open(img_name_old)

   h, w = img.size

   if h < 256 or w < 256:

   continue

   self.filtered_imgs_old.append(img_name_old)

   #dominB: if dominA not in experiments name ,load VOC defultly

   else:

   path_clear = r/home/vip/shy/ots/clear_images/ ##self.opt.path_clear

   self.load_img_dir_clean=path_clear

   self.loaded_imgs_clean = []

   self.img_dir_clean = os.listdir(self.load_img_dir_clean)

   self.loaded_imgs_clean = [os.path.join(self.load_img_dir_clean, img) for img in self.img_dir_clean]

   print(________________now filter old size <256)

   self.filtered_imgs_clean = []

   for i in range(len(self.loaded_imgs_clean)):

   img_name_clean = self.loaded_imgs_clean[i]

   img = Image.open(img_name_clean)

   h, w = img.size

   if h < 256 or w < 256:

   continue

   self.filtered_imgs_clean.append(img_name_clean)

   ####

   print("-------------Filter the imgs whose size <256 finished -------------")

   self.pid = os.getpid()

   def __getitem__(self, index):

   is_real_old=0

   sampled_dataset=None

   degradation=None

   #随机抽取一张图片(从合成的老照片 和 真实老照片 中)

   if self.isImage: ## domain A , contains 2 kinds of data: synthetic + real_old

   P=random.uniform(0,2)

   if P>=0 and P<1:

   sampled_dataset=self.filtered_imgs_old

   self.load_img_dir=self.load_img_dir_RGB_old

   self.Num = len(sampled_dataset)

   is_real_old=1

   if P>=1 and P<2:

   sampled_dataset=self.filtered_imgs_Synhaze

   self.load_img_dir=self.load_img_dir_Synhaze

   self.Num = len(sampled_dataset)

   degradation=1

   #domin B

   else:

   #载入过滤后小于256大小的图

   sampled_dataset=self.filtered_imgs_clean

   self.load_img_dir=self.load_img_dir_clean

   self.Num = len(sampled_dataset)

   index=random.randint(0,self.Num-1)

   img_name = sampled_dataset[index]

   A = Image.open(img_name)

   path = img_name

   #########################################################################

   # i, j, h, w = tfs.RandomCrop.get_params(A, output_size=(256, 256))

   # A = FF.crop(A, i, j, h, w)

   # A = A.convert("RGB")

   # A_tensor = #tfs.ToTensor()(A)

   #########################################################################

   transform_params = get_params(self.opt, A.size)

   A_transform = get_transform(self.opt, transform_params)

   A_tensor = A_transform(A.convert("RGB"))

   B_tensor = inst_tensor = feat_tensor = 0

   input_dict = {label: A_tensor, inst: is_real_old, image: A_tensor,

   feat: feat_tensor, path: path}

   return input_dict

   def __len__(self):

   return len(self.filtered_imgs_clean)## actually, this is useless, since the selected index is just a random number

   #control the epoch through the iters =len(loaded_imgs_clean)

   def name(self):

   return UnPairOldPhotos_SR

  # ###################################################################################3

  # #非成对的老照片图像载入器(合成的老的和真实的老的照片,他们并非对应的,合成的老的照片由VOC数据集经处理生成)

  # class UnPairOldPhotos_SR(BaseDataset): ## Synthetic + Real Old

  # def initialize(self, opt):

  # self.opt = opt

  # self.isImage = domainA in opt.name

  # self.task = old_photo_restoration_training_vae

  # self.dir_AB = opt.dataroot

  # # 载入VOC以及真实灰度、彩色图

  # #dominA

  # if self.isImage:

  #

  # #self.load_img_dir_L_old=os.path.join(self.dir_AB,"Real_L_old.bigfile")

  # self.load_img_dir_RGB_old=os.path.join(self.dir_AB,"Real_RGB_old.bigfile")

  # self.load_img_dir_clean=os.path.join(self.dir_AB,"VOC_RGB_JPEGImages.bigfile")

  # self.load_img_dir_Synhaze=os.path.join(self.dir_AB,"VOC_RGB_Synhaze.bigfile")

  #

  # #self.loaded_imgs_L_old=BigFileMemoryLoader(self.load_img_dir_L_old)

  # self.loaded_imgs_RGB_old=BigFileMemoryLoader(self.load_img_dir_RGB_old)

  # self.loaded_imgs_clean=BigFileMemoryLoader(self.load_img_dir_clean)

  # self.loaded_imgs_Synhaze=BigFileMemoryLoader(self.load_img_dir_Synhaze)

  #

  # #dominB: if dominA not in experiments name ,load VOC defultly

  # else:

  # # self.load_img_dir_clean=os.path.join(self.dir_AB,self.opt.test_dataset)

  # self.load_img_dir_clean=os.path.join(self.dir_AB,"VOC_RGB_JPEGImages.bigfile")

  # self.loaded_imgs_clean=BigFileMemoryLoader(self.load_img_dir_clean)

  # self.load_img_dir_Synhaze=os.path.join(self.dir_AB,"VOC_RGB_Synhaze.bigfile")

  # self.loaded_imgs_Synhaze=BigFileMemoryLoader(self.load_img_dir_Synhaze)

  #

  # ####

  # print("-------------Filter the imgs whose size <256 in VOC-------------")

  # self.filtered_imgs_clean=[]

  # self.filtered_imgs_Synhaze=[]

  #

  # # 过滤出VOC中小于256的图片

  # for i in range(len(self.loaded_imgs_clean)):

  # img_name,img=self.loaded_imgs_clean[i]

  # synimg_name,synimg=self.loaded_imgs_Synhaze[i]

  #

  # h,w=img.size

  # if h<256 or w<256:

  # continue

  # self.filtered_imgs_clean.append((img_name,img))

  # self.filtered_imgs_Synhaze.append((synimg_name,synimg))

  #

  #

  # print("--------Origin image num is [%d], filtered result is [%d]--------" % (

  # len(self.loaded_imgs_clean), len(self.filtered_imgs_clean)))

  # ## Filter these images whose size is less than 256

  #

  # # self.img_list=os.listdir(load_img_dir)

  # self.pid = os.getpid()

  #

  # def __getitem__(self, index):

  #

  #

  # is_real_old=0

  #

  # sampled_dataset=None

  # degradation=None

  # #随机抽取一张图片(从合成的老照片 和 真实老照片 中)

  # if self.isImage: ## domain A , contains 2 kinds of data: synthetic + real_old

  # P=random.uniform(0,2)

  # if P>=0 and P<1:

  # if random.uniform(0,1)<0.5:

  # # sampled_dataset=self.loaded_imgs_L_old

  # # self.load_img_dir=self.load_img_dir_L_old

  #

  # sampled_dataset=self.loaded_imgs_RGB_old

  # self.load_img_dir=self.load_img_dir_RGB_old

  # else:

  # sampled_dataset=self.loaded_imgs_RGB_old

  # self.load_img_dir=self.load_img_dir_RGB_old

  # is_real_old=1

  # if P>=1 and P<2:

  # sampled_dataset=self.filtered_imgs_Synhaze

  # self.load_img_dir=self.load_img_dir_Synhaze

  #

  # degradation=1

  # #domin B

  # else:

  # #载入过滤后小于256大小的图

  # sampled_dataset=self.filtered_imgs_clean

  # self.load_img_dir=self.load_img_dir_clean

  #

  # sampled_dataset_len=len(sampled_dataset)

  #

  # index=random.randint(0,sampled_dataset_len-1)

  #

  # img_name,img = sampled_dataset[index]

  #

  # #already old

  # #if degradation is not None:

  # # #对图片进行降质做旧处理

  # # img=online_add_degradation_v2(img)

  #

  # path=os.path.join(self.load_img_dir,img_name)

  #

  # # AB = Image.open(path).convert(RGB)

  # # split AB image into A and B

  #

  # # apply the same transform to both A and B

  # #随机对图片转换为灰度图

  # if random.uniform(0,1) <0.1:

  # img=img.convert("L")

  # img=img.convert("RGB")

  # ## Give a probability P, we convert the RGB image into L

  #

  # #调整大小

  # A=img

  # w,h=A.size

  # if w<256 or h<256:

  # A=transforms.Scale(256,Image.BICUBIC)(A)

  # # 将图片裁剪为256*256,对于一些小于256的老照片,先进行调整大小

  # ## Since we want to only crop the images (256*256), for those old photos whose size is smaller than 256, we first resize them.

  # transform_params = get_params(self.opt, A.size)

  # A_transform = get_transform(self.opt, transform_params)

  #

  # B_tensor = inst_tensor = feat_tensor = 0

  # A_tensor = A_transform(A)

  #

  # #存入字典

  # #A_tensor : old or Syn imgtensor;

  # #is_real_old: 1:old ; 0:Syn

  # #feat : 0

  # input_dict = {label: A_tensor, inst: is_real_old, image: A_tensor,

  # feat: feat_tensor, path: path}

  # return input_dict

  #

  # def __len__(self):

  # return len(self.loaded_imgs_clean) ## actually, this is useless, since the selected index is just a random number

  #

  # def name(self):

  # return UnPairOldPhotos_SR

  ################################# my #################### if self.isImage:

  #成对图像载入器(原始图及其合成旧图)

  # mapping

  class PairOldPhotosv2(BaseDataset):

   def initialize(self, opt):

   self.opt = opt

   self.isImage = imagan in opt.name #actually ,useless ;

   self.task = old_photo_restoration_training_mapping

   self.dir_AB = opt.dataroot

   #训练模式,载入

   if opt.isTrain:

   path_clear = r/home/vip/shy/ots/clear_images/

   path_haze = r/home/vip/shy/ots/hazy/

   self.load_img_dir_clean=path_clear

   self.load_img_dir_Synhaze=path_haze

   self.img_dir_Synhaze = os.listdir(self.load_img_dir_Synhaze)

   self.loaded_imgs_Synhaze=[os.path.join(self.load_img_dir_Synhaze,img) for img in self.img_dir_Synhaze]

   self.loaded_imgs_clean = []

   for path_i in self.loaded_imgs_Synhaze:

   p,n = os.path.split(path_i)

   pre,ex = os.path.splitext(n)

   clear_pre = pre.split(_)[0]

   clear_path = os.path.join(path_clear,clear_pre+ex)

   self.loaded_imgs_clean.append(clear_path)

   print(________________filter whose size <256)

   self.filtered_imgs_clean = []

   self.filtered_imgs_Synhaze = []

   print(________________now filter syn and clean size <256)

   for i in range(len(self.loaded_imgs_Synhaze)):

   img_name_syn = self.loaded_imgs_Synhaze[i]

   img = Image.open(img_name_syn)

   h, w = img.size

   img_name_clear = self.loaded_imgs_clean[i]

   if h < 256 or w < 256:

   continue

   self.filtered_imgs_clean.append(img_name_clear)

   self.filtered_imgs_Synhaze.append(img_name_syn)

   print("--------Origin image num is [%d], filtered result is [%d]--------" % (

   len(self.loaded_imgs_clean), len(self.filtered_imgs_clean)))

   #测试模式时,仅载入测试集

   else:

   if self.opt.test_on_synthetic:

   ############valset#########

   path_val_clear = r/home/vip/shy/SOTS/outdoor/gt ######none###############self.opt.path_clear

   path_val_haze = r/home/vip/shy/SOTS/outdoor/hazy #########none#############self.opt.path_haze

   self.load_img_dir_clean = path_val_clear

   self.load_img_dir_Synhaze = path_val_haze

   self.img_dir_Synhaze = os.listdir(self.load_img_dir_Synhaze)

   self.loaded_imgs_Synhaze = [os.path.join(self.load_img_dir_Synhaze, img) for img in

   self.img_dir_Synhaze]

   self.loaded_imgs_clean = []

   for path_i in self.loaded_imgs_Synhaze:

   p, n = os.path.split(path_i)

   pre, ex = os.path.splitext(n)

   clear_pre = pre.split(_)[0]

   clear_path = os.path.join(self.load_img_dir_clean, clear_pre + ex)

   self.loaded_imgs_clean.append(clear_path)

   print(________________filter whose size <256)

   self.filtered_val_imgs_clean = []

   self.filtered_val_imgs_Synhaze = []

   print(________________now filter val syn and clean size <256)

   for i in range(len(self.loaded_imgs_Synhaze)):

   img_name_syn = self.loaded_imgs_Synhaze[i]

   img = Image.open(img_name_syn)

   h, w = img.size

   img_name_clear = self.loaded_imgs_clean[i]

   if h < 256 or w < 256:

   continue

   self.filtered_val_imgs_clean.append(img_name_clear)

   self.filtered_val_imgs_Synhaze.append(img_name_syn)

   print(________________finished filter val syn and clean )

   else:

   ############testset#########

   path_test_clear = r/home/vip/shy/SOTS/outdoor/gt ##################self.opt.path_test_clear

   path_test_haze = r/home/vip/shy/SOTS/outdoor/hazy ###################self.opt.path_test_haze

   self.load_img_dir_clean=path_test_clear

   self.load_img_dir_Synhaze=path_test_haze

   self.img_dir_Synhaze = os.listdir(self.load_img_dir_Synhaze)

   self.loaded_imgs_Synhaze=[os.path.join(self.load_img_dir_Synhaze,img) for img in self.img_dir_Synhaze]

   self.loaded_imgs_clean = []

   for path_i in self.loaded_imgs_Synhaze:

   p,n = os.path.split(path_i)

   pre,ex = os.path.splitext(n)

   clear_pre = pre.split(_)[0]

   clear_path = os.path.join(self.load_img_dir_clean,clear_pre+ex)

   self.loaded_imgs_clean.append(clear_path)

   print(________________filter whose size <256)

   self.filtered_test_imgs_clean = []

   self.filtered_test_imgs_Synhaze = []

   print(________________now filter testset syn and clean size <256)

   for i in range(len(self.loaded_imgs_Synhaze)):

   img_name_syn = self.loaded_imgs_Synhaze[i]

   img = Image.open(img_name_syn)

   h, w = img.size

   img_name_clear = self.loaded_imgs_clean[i]

   if h < 256 or w < 256:

   continue

   self.filtered_test_imgs_clean.append(img_name_clear)

   self.filtered_test_imgs_Synhaze.append(img_name_syn)

   print(________________finished filter testset syn and clean )

   print("--------Origin image num is [%d], filtered result is [%d]--------" % (

   len(self.loaded_imgs_Synhaze), len(self.filtered_test_imgs_Synhaze)))

   self.pid = os.getpid()

   def __getitem__(self, index):

   #训练模式

   if self.opt.isTrain:

   #(B为清晰VOC数据集)

   img_name_clean = self.filtered_imgs_clean[index]

   B = Image.open(img_name_clean)

   img_name_synhaze = self.filtered_imgs_Synhaze[index]

   S = Image.open(img_name_synhaze)

   path = os.path.join(img_name_clean)

   #生成成对图像(B为清晰VOC数据集,A对应的含噪声的图像)

   A=S

   ### Remind: A is the input and B is corresponding GT

   #ceshi daima wei xiugai #####################################################

   else:

   #测试模式

   #(B为清晰VOC数据集,A对应的含噪声的图像)

   if self.opt.test_on_synthetic:

   #valset

   img_name_B = self.filtered_test_imgs_clean[index]

   B = Image.open(img_name_B)

   img_name_A=self.filtered_test_imgs_Synhaze[index]

   A = Image.open(img_name_A)

   path = os.path.join(img_name_A)

   else:

   #testset

   img_name_B = self.filtered_val_imgs_clean[index]

   B = Image.open(img_name_B)

   img_name_A=self.filtered_val_imgs_Synhaze[index]

   A = Image.open(img_name_A)

   path = os.path.join(img_name_A)

   #去掉透明通道

   # if random.uniform(0,1)<0.1 and self.opt.isTrain:

   # A=A.convert("L")

   # B=B.convert("L")

   A=A.convert("RGB")

   B=B.convert("RGB")

   # apply the same transform to both A and B

   #获取变换相关参数test_dataset

   transform_params = get_params(self.opt, A.size)

   #变换数据,数据增强

   A_transform = get_transform(self.opt, transform_params)

   B_transform = get_transform(self.opt, transform_params)

   B_tensor = inst_tensor = feat_tensor = 0

   A_tensor = A_transform(A)

   B_tensor = B_transform(B)

   # input_dict = {label: A_tensor, inst: inst_tensor, image: B_tensor,

   # feat: feat_tensor, path: path}

   input_dict = {label: B_tensor, inst: inst_tensor, image: A_tensor,

   feat: feat_tensor, path: path}

   return input_dict

   def __len__(self):

   if self.opt.isTrain:

   return len(self.filtered_imgs_clean)

   else:

   return len(self.filtered_test_imgs_clean)

   def name(self):

   return PairOldPhotos

  #

  #

  #

  # #成对图像载入器(原始图及其合成旧图)

  # # mapping

  # class PairOldPhotos(BaseDataset):

  # def initialize(self, opt):

  # self.opt = opt

  # self.isImage = imagan in opt.name #actually ,useless ;

  # self.task = old_photo_restoration_training_mapping

  # self.dir_AB = opt.dataroot

  # #训练模式,载入VOC

  # if opt.isTrain:

  # self.load_img_dir_clean= os.path.join(self.dir_AB, "VOC_RGB_JPEGImages.bigfile")

  # self.loaded_imgs_clean = BigFileMemoryLoader(self.load_img_dir_clean)

  #

  # self.load_img_dir_Synhaze= os.path.join(self.dir_AB, "VOC_RGB_Synhaze.bigfile")

  # self.loaded_imgs_Synhaze = BigFileMemoryLoader(self.load_img_dir_Synhaze)

  #

  # print("-------------Filter the imgs whose size <256 in VOC-------------")

  # #过滤出VOC中小于256的图片

  # self.filtered_imgs_clean = []

  # self.filtered_imgs_Synhaze = []

  #

  # for i in range(len(self.loaded_imgs_clean)):

  # img_name, img = self.loaded_imgs_clean[i]

  # synhazeimg_name, synhazeimg = self.loaded_imgs_clean[i]

  #

  # h, w = img.size

  # if h < 256 or w < 256:

  # continue

  # self.filtered_imgs_clean.append((img_name, img))

  # self.filtered_imgs_Synhaze.append((synhazeimg_name, synhazeimg))

  #

  # print("--------Origin image num is [%d], filtered result is [%d]--------" % (

  # len(self.loaded_imgs_clean), len(self.filtered_imgs_clean)))

  # #测试模式时,仅载入测试集

  # else:

  # self.load_img_dir=os.path.join(self.dir_AB,opt.test_dataset)

  # self.loaded_imgs=BigFileMemoryLoader(self.load_img_dir)

  #

  # self.pid = os.getpid()

  #

  # def __getitem__(self, index):

  #

  #

  # #训练模式

  # if self.opt.isTrain:

  # #(B为清晰VOC数据集)

  # img_name_clean,B = self.filtered_imgs_clean[index]

  # img_name_synhaze,S = self.filtered_imgs_Synhaze[index]

  #

  # path = os.path.join(self.load_img_dir_clean, img_name_clean)

  # #生成成对图像(B为清晰VOC数据集,A对应的含噪声的图像)

  # if self.opt.use_v2_degradation:

  # A=S

  # ### Remind: A is the input and B is corresponding GT

  # #ceshi daima wei xiugai #####################################################

  # else:

  # #测试模式

  # #(B为清晰VOC数据集,A对应的含噪声的图像)

  # if self.opt.test_on_synthetic:

  #

  # img_name_B,B=self.loaded_imgs[index]

  # A=online_add_degradation_v2(B)

  # img_name_A=img_name_B

  # path = os.path.join(self.load_img_dir, img_name_A)

  # else:

  # img_name_A,A=self.loaded_imgs[index]

  # img_name_B,B=self.loaded_imgs[index]

  # path = os.path.join(self.load_img_dir, img_name_A)

  #

  # #去掉透明通道

  # if random.uniform(0,1)<0.1 and self.opt.isTrain:

  # A=A.convert("L")

  # B=B.convert("L")

  # A=A.convert("RGB")

  # B=B.convert("RGB")

  # ## In P, we convert the RGB into L

  #

  #

  # ##test on L

  #

  # # split AB image into A and B

  # # w, h = img.size

  # # w2 = int(w / 2)

  # # A = img.crop((0, 0, w2, h))

  # # B = img.crop((w2, 0, w, h))

  # w,h=A.size

  # if w<256 or h<256:

  # A=transforms.Scale(256,Image.BICUBIC)(A)

  # B=transforms.Scale(256, Image.BICUBIC)(B)

  #

  # # apply the same transform to both A and B

  # #获取变换相关参数

  # transform_params = get_params(self.opt, A.size)

  # #变换数据,数据增强

  # A_transform = get_transform(self.opt, transform_params)

  # B_transform = get_transform(self.opt, transform_params)

  #

  # B_tensor = inst_tensor = feat_tensor = 0

  # A_tensor = A_transform(A)

  # B_tensor = B_transform(B)

  #

  # input_dict = {label: A_tensor, inst: inst_tensor, image: B_tensor,

  # feat: feat_tensor, path: path}

  # return input_dict

  #

  # def __len__(self):

  #

  # if self.opt.isTrain:

  # return len(self.filtered_imgs_clean)

  # else:

  # return len(self.loaded_imgs)

  #

  # def name(self):

  # return PairOldPhotos

  # #####################################################################

  # #成对带折痕图像载入器

  # class PairOldPhotos_with_hole(BaseDataset):

  # def initialize(self, opt):

  # self.opt = opt

  # self.isImage = imagegan in opt.name

  # self.task = old_photo_restoration_training_mapping

  # self.dir_AB = opt.dataroot

  # #训练模式。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: