码迷,mamicode.com
首页 > 其他好文 > 详细

基于opencv简单的图片截取

时间:2019-12-20 13:56:28      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:user   enc   简单   findall   div   text   imwrite   std   import   

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‘))

  

基于opencv简单的图片截取

标签:user   enc   简单   findall   div   text   imwrite   std   import   

原文地址:https://www.cnblogs.com/intruder/p/12072420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!