letterbox

import cv2
import xml.etree.ElementTree as ET
import pdb
import os
import math
#图片拓展,使用letterbox方式填充,补黑填充
#960*300----->960*960,补黑边填充
#300*400----->400*400,

def letterbox(old_img_path, new_img_path, old_xml_path, new_xml_path):#old_img_path new_img_path old_xml_path new_xml_path
    
    lines = os.listdir(old_img_path)
    for line in lines:
        try:
            jpg_file = line.strip()
            jpg_id = jpg_file.split('.')[0] 
            xml_file = jpg_id + '.xml'
            img = cv2.imread(old_img_path + line, -1)
            ih, iw = img.shape[:2]
            eh =  max(ih, iw)
            # scale = min(eh * 1.0/ ih, eh * 1.0/ iw)

            # nh = int(ih * scale)
            # nw = int(iw * scale)
            #image = cv2.resize(img, (nw, nh), interpolation=cv2.INTER_CUBIC)
            top = (eh - ih) // 2
            print("top:",top)
            bottom = eh - ih - top
            left = (eh - iw) // 2
            print("left:",left)
            right = eh - iw - left
            shrink = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT)
            new_jpg_file = new_img_path + jpg_file
            cv2.imwrite(new_jpg_file, shrink)

            xml_file = os.path.join(old_xml_path, xml_file)
            updateTree = ET.parse(xml_file)

            root = updateTree.getroot()
            root.find("size").find('width').text = str(eh)
            root.find("size").find('height').text = str(eh)
            element_object = root.findall('object')
            for i in range(len(element_object)):
                xmin = element_object[i].find('bndbox').find('xmin').text
                xmax = element_object[i].find('bndbox').find('xmax').text
                ymin = element_object[i].find('bndbox').find('ymin').text
                ymax = element_object[i].find('bndbox').find('ymax').text
                element_object[i].find('bndbox').find('xmin').text = str(int(int(float(xmin)) + left))
                element_object[i].find('bndbox').find('xmax').text = str(int(int(float(xmax)) + left))
                element_object[i].find('bndbox').find('ymin').text = str(int(int(float(ymin)) + top))
                element_object[i].find('bndbox').find('ymax').text = str(int(int(float(ymax)) + top))
            new_xml_file = new_xml_path + xml_file.split('/')[-1]
            updateTree.write(new_xml_file)
        except Exception as e:
            print(e)
            continue
def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)

if __name__ == '__main__':

    allPath = '/home/hs/data/data_jiaxingyili/goods/smalldeal'
    oldImg = allPath + '/imgC/'
    oldXml = allPath + '/xmlC/'
    newImg = allPath + '/imgL/'
    newXml = allPath + '/xmlL/'

    create_dir_not_exist(newImg)
    create_dir_not_exist(newXml)

    letterbox(oldImg, newImg, oldXml, newXml)



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值