基于opencv简单的图片截取

import xml.etree.ElementTree as ET
import os, cv2
from tqdm import tqdm

annota_dir = 'C:\\Users\\Administrator\\Desktop\\1_stage_template\\Annotations'
origin_dir = 'C:\\Users\\Administrator\\Desktop\\1_stage_template\\IMG'
target_dir1 = 'C:\\Users\\Administrator\\Desktop\\1_stage_template\\cut'


def divide_img(oriname):
    img_file = os.path.join(origin_dir, oriname + '.jpg')
    im = cv2.imread(img_file)

    # 读取每个原图像的xml文件
    xml_file = os.path.join(annota_dir, oriname + '.xml')
    tree = ET.parse(xml_file)
    root = tree.getroot()

    for object in root.findall('object'):

        object_name = object.find('name').text
        Xmin = int(object.find('bndbox').find('xmin').text)
        Ymin = int(object.find('bndbox').find('ymin').text)
        Xmax = int(object.find('bndbox').find('xmax').text)
        Ymax = int(object.find('bndbox').find('ymax').text)
        color = (10, 250, 7)

        cropped = im[Ymin:Ymax,Xmin:Xmax]
        cv2.imshow('im', im)

    img_name = oriname + '.jpg'
    to_name = os.path.join(target_dir1, img_name)
    cv2.imwrite(to_name, cropped)


img_list = os.listdir(origin_dir)
for name in img_list:
    divide_img(name.rstrip('.jpg'))

  

Aiden的技术屋 文章被收录于专栏

虽然还是个菜鸟,但是总有一天会长大,翱翔于天空,热爱技术

全部评论

相关推荐

没有offer的呆呆:日常和暑期都投试一试,3月份机会挺多的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务