import os
from xml.dom.minidom import Document
import numpy as np
import copy
import cv2
import sys
def clip_image(file_idx, image, width, height, stride_w, stride_h):
shape = image.shape
# os.mkdir(os.path.join(save_dir, file_idx)) #classes/airplane/clip\000140
# print(os.path.join(save_dir, file_idx))
# exit()
for start_h in range(0, shape[0], stride_h):
for start_w in range(0, shape[1], stride_w):
start_h_new = start_h
start_w_new = start_w
if start_h + height > shape[0]:
start_h_new = shape[0] - height
if start_w + width > shape[1]:
start_w_new = shape[1] - width
top_left_row = max(start_h_new, 0)
top_left_col = max(start_w_new, 0)
bottom_right_row = min(start_h + height, shape[0])
bottom_right_col = min(start_w + width, shape[1])
subImage = image[top_left_row:bottom_right_row, top_left_col: bottom_right_col]
if (subImage.shape[0] > 5 and subImage.shape[1] > 5):
# os.mkdir(os.path.join(save_dir, 'images'))
img = os.path.join(save_dir,
"%s_%04d_%04d.jpg" % (file_idx, top_left_row, top_left_col))
cv2.imwrite(img, subImage)
raw_data = 'classes/vehicle'
raw_images_dir = os.path.join(raw_data, 'img')
save_dir = 'classes/vehicle/clip'
images = [i for i in os.listdir(raw_images_dir) if 'jpg' in i]
min_length = 1e10
max_length = 1
img_h, img_w, stride_h, stride_w = 256, 256, 192, 192
for idx, img in enumerate(images):
print(idx, 'read image', img)
img_data = cv2.imread(os.path.join(raw_images_dir, img))
clip_image(img.strip('.jpg'), img_data, img_w, img_h, stride_w, stride_h)
图像切分成小图
最新推荐文章于 2024-06-30 17:53:33 发布