""" # File : mask2iou.py # Time :2024-04-08 13:35 # Author :FEANGYANG # version :python 3.7 # Contact :1071082183@qq.com # Description: """ import os import cv2 import numpy as np def cut_small_images(image_path, pic_target='./result_small_img/'): if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(pic_target) image = cv2.imread(image_path) name = os.path.split(image_path)[-1] (width, length, depth) = image.shape w, h, _ = image.shape cut_width = cut_length = 128 # 计算可以划分的横纵的个数 num_width = int(width / cut_width) num_length = int(length / cut_length) # for循环迭代生成 for i in range(0, num_width): for j in range(0, num_length): im = image[i * cut_width: (i + 1) * cut_width, j * cut_length: (j + 1) * cut_length, :] result_path = pic_target + os.path.splitext(name)[0] + '_{}_{}_image.jpg'.format(i + 1, j + 1) cv2.imwrite(result_path, im) if __name__ == '__main__': images_dir_path = './data1/fire' images_list_path = os.listdir(images_dir_path) for image_path in images_list_path: cut_small_images(os.path.join(images_dir_path, image_path))