labelme把绘制的区域抠出来

1、标注图:

2、效果图:

3、代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 20:28:58 2019

@author: Herr-kun
"""

import json
import matplotlib.pyplot as plt
import numpy as np

from labelme import utils

json_file=r'C:\Users\Herr-kun\Desktop\000f0f2bf.json'

data = json.load(open(json_file))

img = utils.img_b64_to_arr(data['imageData'])
lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes']) # lbl:array 0、1 (区域内的为1,之外为0)
#  lbl_names :dict   _background_ :0   label_wyk:1
captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
lbl_viz = utils.draw_label(lbl, img, captions)

mask=[]
class_id=[]
for i in range(1,len(lbl_names)): #若有多个class(物体) 跳过第一个class(默认为背景)
    mask.append((lbl==i).astype(np.uint8)) # 解析出像素值为1的对应,对应第一个对象 mask 为0、1组成的(0为背景,1为对象)
    class_id.append(i) # mask与clas 一一对应

mask=np.transpose(np.asarray(mask,np.uint8),[1,2,0]) # 转成[h,w,instance count]
class_id=np.asarray(class_id,np.uint8) # [instance count,]

plt.subplot(221)
plt.imshow(img)
plt.title("original image")
plt.axis('off')
plt.subplot(222)
plt.imshow(lbl_viz)
plt.axis('off')
plt.subplot(223)
plt.imshow(mask[:,:,0],'gray')

mask2=np.ones_like(img)
for i in range(mask2.shape[2]):
    mask2[:,:,i]=mask.squeeze()
plt.subplot(224)
plt.imshow(mask2*img)
plt.axis('off')

plt.show()

 

参考博客:https://blog.csdn.net/wc781708249/article/details/79595174

Python中,我们可以利用`matplotlib`库配合`labelme`工具来进行图像标注区域的涂色。`labelme`是一个用于数据集标注的Python工具,它生成的是`.json`格式的标注文件,而`matplotlib`则常用于绘制和修改图像。 首先,你需要安装`labelme`和相关的依赖,如`PIL`和`matplotlib`。然后,你可以按照以下步骤操作: 1. **读取标注数据**: 使用`json`模块加载`.json`文件中的标注信息,这通常包括每个区域的边界框、类别等。 ```python import json with open('labels.json', 'r') as f: labels = json.load(f) ``` 2. **准备图像数据**: 确保你已经有了原始图像,并将其转换为`numpy`数组以便于绘图。 ```python import cv2 img = cv2.imread('image.jpg') ``` 3. **绘制区域**: 遍历`labelme`返回的标注,使用`matplotlib`的`Rectangle`或`Polygon`对象创建形状并上色。 ```python import matplotlib.pyplot as plt from matplotlib.patches import Polygon def draw_regions(img, regions): fig, ax = plt.subplots() ax.imshow(img) for region in regions['regions']: if 'shape_attributes' in region: if region['shape_attributes']['name'] == 'rectangle': x, y, w, h = (region['shape_attributes']['all_points_x'], region['shape_attributes']['all_points_y'], max(region['shape_attributes']['width'], 1), max(region['shape_attributes']['height'], 1)) poly = Polygon([(x[i], y[i]) for i in range(0, len(x), 2)], closed=True) elif region['shape_attributes']['name'] == 'polygon': points = [(point['x'], point['y']) for point in region['shape_attributes']['all_points']] poly = Polygon(points, closed=True) else: continue color = region.get('fill_color', 'red') # 可以自定义颜色 poly.set_facecolor(color) ax.add_patch(poly) plt.show() draw_regions(img, labels) ``` 4. **保存结果**: 如果需要保存涂色后的图像,可以将`plt.figure()`内的内容保存成图片。 ```python fig.savefig('colored_image.png') ```
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值