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))
抠图,labelme打标之后,提取图片中的目标
最新推荐文章于 2026-06-29 12:33:31 发布
这段代码读取包含边界框信息的JSON文件,然后使用PIL库打开对应的JPEG图片,根据边界框坐标裁剪图片,并用OpenCV保存每个裁剪后的区域。主要涉及图像处理和文件操作。
9879

被折叠的 条评论
为什么被折叠?



