使用 matplotlib 按类别着色

发布于 2025-01-15 20:16:44 字数 887 浏览 0 评论 0原文

这是我用 coco json 和图像创建带注释的图像的代码。

I = io.imread('%s/images/%s'%(dataDir,img['file_name']))

axes = plt.gca()
axes.set_frame_on(False)
axes.set_xticks([]); axes.set_yticks([])

annpath = os.path.join(dataDir, 'AnnImages/')
if not os.path.exists(annpath):
 os.makedirs(annpath)
path=annpath+str(img['file_name'])[:-4]+'.jpg'

annotation_ids = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
annotations = coco.loadAnns(annotation_ids)
fig = plt.figure(figsize=(16, 9), dpi=155.9)
plt.imshow(I, interpolation='nearest')
plt.axis('off')

coco.showAnns(annotations)
    
plt.savefig(path, bbox_inches='tight', pad_inches=0)

但它为每个对象以不同的颜色输出。

我想为每个类别着色。

你能帮助我吗?

我的注释类别: test , test2

在此处输入图像描述

This is my code to create an annotated image with coco json and image.

I = io.imread('%s/images/%s'%(dataDir,img['file_name']))

axes = plt.gca()
axes.set_frame_on(False)
axes.set_xticks([]); axes.set_yticks([])

annpath = os.path.join(dataDir, 'AnnImages/')
if not os.path.exists(annpath):
 os.makedirs(annpath)
path=annpath+str(img['file_name'])[:-4]+'.jpg'

annotation_ids = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
annotations = coco.loadAnns(annotation_ids)
fig = plt.figure(figsize=(16, 9), dpi=155.9)
plt.imshow(I, interpolation='nearest')
plt.axis('off')

coco.showAnns(annotations)
    
plt.savefig(path, bbox_inches='tight', pad_inches=0)

But it is output in different colors for each object..

I want to color each category.

Can you help me?

My annotate category : test , test2

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文