抠图,labelme打标之后,提取图片中的目标

这段代码读取包含边界框信息的JSON文件,然后使用PIL库打开对应的JPEG图片,根据边界框坐标裁剪图片,并用OpenCV保存每个裁剪后的区域。主要涉及图像处理和文件操作。
import os
import json
import cv2
# from numpy import imag
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np


path_ori = r"E:\dabiao\DES_ori"
path_dest = r"E:\dabiao\DES_EXTRACT"

files = os.listdir(path_ori)

for file in files:
    if file.endswith(".json"):
        f = open(path_ori + '\\' +file)
        data = json.load(f)
        img = Image.open(path_ori + '\\' + file.split('.')[0] + '.JPG')
        for i in range(len(data["shapes"])):
            points =  data["shapes"][i]["points"]
            
            points_01 = round(points[0][0])
            points_02 = round(points[0][1])
            points_03 = round(points[1][0])
            points_04 = round(points[1][1])

            box = [points_01,points_02,points_03,points_04]
            roi = img.crop(box)
            cv2.imwrite(path_dest + '\\' + file.split('.')[0] + '-' + str(i) + '.jpg', np.array(roi))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值