import os
import cv2
import math
def crop_img(w_im, h_im, image, file_name):
patch_w = 224
patch_h = 224
num_y = math.ceil(h_im / patch_h)
num_x = math.ceil(w_im / patch_w)
save_path = r'C:\Users\zhangguowei_sx\Desktop\qipu4730071192608400_yzjz01' + '/cropped/'
if not os.path.exists(save_path):
os.mkdir(save_path)
for k in range(0, num_y):
# when last row
if k == (num_y - 1):
for j in range(0, num_x):
# when last col
if j == (num_x - 1):
img = image[h_im - 224:h_im, w_im - 224:w_im]
cv2.imwrite(save_path + file_name + '_{}_{}.png'.format(k, j), img)
else:
img = image[h_im - 224:h_im, j * 224:(j + 1) * 224]
cv2.imwrite(save_path + file_name + '_{}_{}.png'.format(k, j), img)
else:
for j in range(0, num_x):
# when last col
if j == (num_x - 1):
img = image[k * 224:(k + 1) * 224, w_im - 224:w_im]
cv2.imwrite(save_path + file_name + '_{}_{}.png'.format(k, j), img)
else:
img = image[k * 224:(k + 1) * 224, j * 224:(j + 1) * 224]
cv2.imwrite(save_path + file_name + '_{}_{}.png'.format(k, j), img)
利用python将图像切块
最新推荐文章于 2023-11-05 21:30:02 发布