栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python脚本011:将目标检测的框截图并重命名

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python脚本011:将目标检测的框截图并重命名

Author:XuLiu
Time:20211105   
Function:使用OpenCV截取图片,命名id_c0022.jpg 

Input:
1.txt文件

idxywh
5673573989469

2.一张大图片

Output:

命名为id_c_.jpg的截图

import cv2
import os
import codecs

def image_cut_save(path, x, y, w, h, save_path):
    """
        所截区域图片保存
    :param path: 图片路径
    :param x: 区块左上角位置的像素点离图片左边界的距离
    :param y:区块左上角位置的像素点离图片上边界的距离
    :param x+w:区块右下角位置的像素点离图片左边界的距离
    :param y+h:区块右下角位置的像素点离图片上边界的距离
    :param save_path: 所截图片保存位置
    """

    img = cv2.imread(path)  # 打开图像
    cropped = img[y:y+h, x:x+w]
    cv2.imwrite(save_path, cropped)
    # embed()


if __name__ == '__main__':
    root_path = '/home/jy/xl/workstation/Datasets/Car/Car_dataset'    #图片的路径
    save_path = '/home/jy/xl/workstation/Datasets/Car/Car_dataset_Cut'  #截圖的路径
    txt_path = '/home/jy/xl/workstation/Datasets/Car/Dataset/IDAnnotations1'  #txt的路径,包括id+目标检测的坐标
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    txt = os.listdir(txt_path)
    
    for i in txt:
        portion = os.path.splitext(i)
        if portion[1] == '.txt':
            image = portion[0]
        pic_path = os.path.join(root_path, image)   #图片的文件名

        f = codecs.open(os.path.join(txt_path, i), mode='r', encoding='utf-8')
        lines = f.readlines()   #一个txt的文件的行数
        for l in lines:            #对每一行遍历
            temp = l.split()       #以空格分割
            id = int(temp[0])     
            xmin = int(temp[1])  
            ymin = int(temp[2])
            xmax = int(temp[3])
            ymax = int(temp[4])
            pic_save_dir_path = os.path.join(save_path, str(id) + '_'+image) 
            image_cut_save(pic_path, xmin, ymin, xmax, ymax, pic_save_dir_path) 
            # from IPython import embed
            # embed()
        f.close()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号