图像切分成小图

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值