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

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

# -*- 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)#裁出来保存的照片是右下角的位置

你可能感兴趣的:(-,数据集,-,-,编码理解,-,计算机视觉,深度学习,python)