❀数据集❀关于数据集的处理_裁剪一定尺寸并保存

 关于大量数据集有时需要裁剪后保存到新的文件夹,只需要修改路径文件。

# -*- coding:utf-8 -*-
#方法1对文件下所有图片遍历裁剪一定尺寸并保存
'''
import glob
import os
import cv2
import numpy as np
from multiprocessing import Pool
from PIL import Image
import scipy.io as io


def cut(img_dir, output_img, output_npy, output_mat, patch_size_w, patch_size_h, stride_w, stride_h):
    """
    传入图片的路径,遍历切割;
    img_dir 为图片待切割图片路径;
    out_img:切割好的img的输出路径;
    output_npy:输出npy
    output_mat:输出mat数据的路径
    patch_size_w:切割的图像宽度
    patch_size_h:切割的图像的高度
    stride_w:横向移动大小
    stride_h:高度移动大小
    """
    img_dir = img_dir
    file_list = glob.glob(img_dir + '*.jpg')#遍历文件里的图片
    print(len(file_list))
    for i in range(len(file_list)):
        img = Image.open(file_list[i])
        # img = cv2.imread(),cv2生成的是numpy
        # AttributeError: 'numpy.ndarray' object has no attribute 'crop'
        # 如果是cv2,可以使用img.shape,
        # weight, height, channel = img.size
        weight, height = img.size
        # 偏移量
        stride_w = stride_w
        stride_h = stride_h
        # 切割开始点
        x1 = 0
        y1 = 0
        x2 = patch_size_w
        y2 = patch_size_h
        n = 0
        while x2 <= weight:
            while y2 <= height:
                crop_name = str(i) + str(n) + ".jpg"
                # 保存切割后的图片
                img2 = img.crop((y1, x1, y2, x2))
                img2.save(output_img + crop_name)

                # 转npy并保存
                res_npy = np.array(img2, dtype='uint16')
                np.save(output_npy + str(i) + str(n) + '.npy', res_npy)

                # 转mat并保存
                numpy_file = np.load(output_npy + str(i) + str(n) + '.npy')
                io.savemat(output_mat + str(i) + str(n) + '.mat', {'data': numpy_file})
                n = n + 1
                # 切割移动方式为从上到下,再从左到右
                y1 = y1 + stride_h
                y2 = y1 + patch_size_h
            # 左右移动
            x1 = x1 + stride_w
            x2 = x1 + patch_size_w
            y1 = 0
            y2 = patch_size_h


if __name__ == "__main__":
    # 参数尺寸
    patch_size_w = 200
    patch_size_h = 150
    stride_w = 200
    stride_h = 150
    # 参数路径
    src_dir = 'E:/pythonProject/flowerclass/data/train/tulip/'#数据集的路径
    save_dir = 'E:/pythonProject/cv2/copyimages/'#必须要自己提前建好文件夹
    save_dir1 = 'E:/pythonProject/cv2/img_npy/'#每张照片对应一个npy文件
    save_dir2 = 'E:/pythonProject/cv2/img_mat/'#每张照片对应一个mat文件

    cut(src_dir, save_dir, save_dir1, save_dir2, patch_size_w, patch_size_h, stride_w, stride_h)
    print('DONE')
    '''
#方法2:对文件下所有图片遍历裁剪一定尺寸并保存
import cv2
import os

rootdir = r'E:\pythonProject\cv2\images'  # 指明被遍历的文件夹
for parent, dirnames, filenames in os.walk(rootdir):
	for filename in filenames:
		currentPath = os.path.join(parent, filename)
		img = cv2.imread(currentPath)
		cropped = img[600:1280, 500:1340]  # 裁剪坐标为[y0:y1, x0:x1]
		cv2.imwrite('E:\pythonProject\cv2\p_after\cut' + filename, cropped)#裁出来保存的照片是右下角的位置

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏天|여름이다

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值