博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据集整理
阅读量:3943 次
发布时间:2019-05-24

本文共 3069 字,大约阅读时间需要 10 分钟。

python脚本实现批量下采样

项目简介

在超分辨领域,下载到的数据集只有高分辨图像,低分辨图像需要根据高分辨率图像通过相应的下采样方式获取。本项目使用python脚本实现对高分辨图像的批量下采样。

实现思路

1、获取用户输入参数。包括高分辨图像路径、下采样因子等。

2、获取文件列表,也就是说我们需要知道用户传入的路径中有多少图片,然后记录图片的位置。
3、遍历整理好的文件列表,逐张打开图片。
4、先将原图作为高分辨率图像复制到相应的文件夹。
5、对图片进行下采样。
6、将下采样得到的图片作为低分辨率图像存放在相应文件夹。

完整代码

import cv2import osfrom tqdm import *from pathlib import Pathimport timeimport argparse# 整理文件def countFile(dir):    tmp = 0  # 用于记录图片数量    f = open(os.path.join(os.getcwd(), "filelist.txt"), mode='a+') # 在当前目录创建文本记录文件位置    for item in os.listdir(dir):  # 遍历文件夹        if os.path.isfile(os.path.join(dir, item)):  # 如果该项是文件            tmp += 1  # 图片数量加一            f.write(os.path.join(dir, item) + '\n')  # 记录文件位置        else:            f.close()            tmp += countFile(os.path.join(dir, item))    f.close()    return tmpdef main(args):    # 1、相关参数设置(已在args中完成)    # 2、获取文件数量    filenum = 0    if args.fileListFlag:        if os.path.exists(os.path.join(os.getcwd(), "filelist.txt")):            os.remove(os.path.join(os.getcwd(), "filelist.txt"))        print("正在整理文件...")        filenum = countFile(args.originalPath)  # 返回图片的张数并生成文件列表        print('给定路径中的文件数量:' , filenum)    # 3、遍历打开每幅图片    if filenum != 0:        f = open(os.path.join(os.getcwd(), "filelist.txt"), mode='r+')        pbar = tqdm(total=filenum)        for originalPicPath in f.readlines():            if not originalPicPath=='\n':                originalPicPath = originalPicPath.replace('\n','')                originalPic = cv2.imread(originalPicPath)                # 4、复制原图片到指定文件夹                PicName_HR = os.path.join(args.savePath, "HR", originalPicPath[-18:])                # 如果文件夹不存在就创建文件夹                if not os.path.exists(PicName_HR[:-7]):                    os.makedirs(PicName_HR[:-7])                cv2.imwrite(PicName_HR,originalPic)                # 5、对打开的图片进行下采样                rows, cols, channels = originalPic.shape                BicubicPic = cv2.resize(originalPic, (int(cols/args.scale), int(rows/args.scale)), interpolation=cv2.INTER_CUBIC)                # 6、将下采样图片保存到指定文件夹                PicName_LR = os.path.join(args.savePath, "LR_bicubic", originalPicPath[-18:])                # 如果文件夹不存在就创建文件夹                if not os.path.exists(PicName_LR[:-7]):                    os.makedirs(PicName_LR[:-7])                cv2.imwrite(PicName_LR,BicubicPic)            pbar.update(1)  # 更新进度条        pbar.close()  # 关闭进度条        f.close()  # 关闭文件    else:        print("该文件夹中没有图片")if __name__ == "__main__":    # 获取用户传入的参数    description = '搜索给定文件夹中的图片并使用插值方法下采样'    parser = argparse.ArgumentParser(description=description)    parser.add_argument('--fileListFlag', type=bool, default=True, help='是否新建文件列表')    parser.add_argument('--scale', type=int, default=4, help='下采样因子,默认为4')    parser.add_argument('--originalPath', type=str, default=r"D:\BaiduNetdiskDownload\vimeo_septuplet\vimeo_septuplet\sequences", help='图片文件路径')    parser.add_argument('--savePath', type=str, default=os.getcwd(), help='整理图片的保存路径')    args = parser.parse_args()    main(args)

实现效果

下载到的关于视频超分辨的数据集:

下载到的数据集
整理后的数据集:
整理后的数据集

本项目参考博客:

转载地址:http://vdiwi.baihongyu.com/

你可能感兴趣的文章
小米笔记本解决风扇异响
查看>>
Linux下 socket-udp通信
查看>>
Linux - 守护进程-1
查看>>
syslog 和 rsyslog
查看>>
Linux下,write/read,recv/send, recvfrom/sendto的区别
查看>>
ubuntu下 rc.local的脚本不运行
查看>>
Linux下简单Makefile文件的编写
查看>>
linux下配置JDK JAVA环境
查看>>
解决Ubuntu 14.04 grub选择启动项10秒等待时间
查看>>
Python函数操作集锦之字符串测试、判断函数
查看>>
Python字符串操作集锦之字符串映射表
查看>>
Python字符串操作集锦之字符串编码解码函数
查看>>
Python字符串类型转换函数
查看>>
Python有用的命令
查看>>
Python条件语句
查看>>
Python eval()函数
查看>>
Linux rz和sz命令详解
查看>>
Python 集合set
查看>>
Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
查看>>
IDEA中Struts2文件上传时404错误The origin server did not find a current representation for the target resour
查看>>