上效果图
原图:
效果图
代码
from cv2 import cv2
import numpy as np
import random
pic_path = './negative_samples/1.jpg' # 切割原图
pic_target = './img/' # 分割后的图片保存的文件夹 @这是你需要修改的地方
#要分割后的尺寸
cut_width = 20
cut_length = 20
# 读取要分割的图片,以及其尺寸等数据
picture = cv2.imread(pic_path)
(width, length, depth) = picture.shape
# 预处理生成0矩阵
pic = np.zeros((cut_width, cut_length, depth))
# 计算可以划分的横纵的个数
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):
name_ID = random.randint(1,10000000)
pic = picture[i*cut_width : (i+1)*cut_width, j*cut_length : (j+1)*cut_length, :]
result_path = pic_target + '{}_{}.jpg'.format(i+1, j+1)
cv2.imwrite(result_path, pic)
print("done!!!")