mask2small.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. # File : mask2iou.py
  3. # Time :2024-04-08 13:35
  4. # Author :FEANGYANG
  5. # version :python 3.7
  6. # Contact :1071082183@qq.com
  7. # Description:
  8. """
  9. import os
  10. import cv2
  11. import numpy as np
  12. def cut_small_images(image_path, pic_target='./result_small_img/'):
  13. if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹
  14. os.makedirs(pic_target)
  15. image = cv2.imread(image_path)
  16. name = os.path.split(image_path)[-1]
  17. (width, length, depth) = image.shape
  18. w, h, _ = image.shape
  19. cut_width = cut_length = 128
  20. # 计算可以划分的横纵的个数
  21. num_width = int(width / cut_width)
  22. num_length = int(length / cut_length)
  23. # for循环迭代生成
  24. for i in range(0, num_width):
  25. for j in range(0, num_length):
  26. im = image[i * cut_width: (i + 1) * cut_width, j * cut_length: (j + 1) * cut_length, :]
  27. result_path = pic_target + os.path.splitext(name)[0] + '_{}_{}_image.jpg'.format(i + 1, j + 1)
  28. cv2.imwrite(result_path, im)
  29. if __name__ == '__main__':
  30. images_dir_path = './data1/fire'
  31. images_list_path = os.listdir(images_dir_path)
  32. for image_path in images_list_path:
  33. cut_small_images(os.path.join(images_dir_path, image_path))