2021-08-07

数据集中图像通道数不一致


前言

我们在网页上爬取图像数据集后会出现图像通道数不一致或者尺寸太小的问题,为了便于统一管理可以选择删除这部分图像


代码如下:

import os
from skimage import io, color, transform

path = ""   # 数据集的上一级路径
count = 0   
for file_name in os.listdir(path): # file_name为数据集的文件夹名字
    file_path = os.path.join(path, file_name)   # 获取数据集的路径
    for img_name in os.listdir(file_path):  # img_name获取图片名字
        img = io.imread(file_path+ "\\"+ img_name, plugin='matplotlib')  # 读取图像
        count += 1
        if count % 1000 == 0:
            print(count)
        if len(img.shape) < 3:  # 如果数据只是二维的删除   
            os.remove(file_path + "\\" + img_name)
        elif img.shape[2] != 3: # 如果不是三通道的图像删除
            os.remove(file_path+ "\\"+ img_name)
        elif img.shape[0] < 32:   #  如果图像太小,这里选择是尺寸门槛是32,根据对图像transform来修改
            os.remove(file_path + "\\" + img_name)
        elif img.shape[1] < 32:
            os.remove(file_path + "\\" + img_name)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值