labelme 标注生成24位深度图像转换为8位

 

新版本的 labelme 标注完成图像后,将 json 文件转换为图像时已经转换为 8 通道图像,如下图所示:

 

最近在看别人程序时发现经过图像标注生成的 label 一片黑,且图像深度为 24 位,如下图所示

原始图像
生成的 label 图

 

label 图信息

 

运用matlab读入显示0,1 ; 说明图像中存在两种像素

 而我的程序需要标签深度为 8 位的彩色图像,转换如下(python 代码)

(1) 首先将 RGB 图像转换为 L

     其中模式"L"为灰度图像,它的每个像素用8个bit位表示,其中0表示黑,255表示白,其它数字表示不同的灰度。在PIL模式中, 从模式"RGB"转换到模式"L",有一个计算公式,即:L = R * 299/1000 + G * 587/1000+ B * 114/1000(只取整数部分)。

import numpy as np
from PIL import Image

# 24位转8位灰度
im_r = Image.open(r'C:\Users\Administrator\Desktop\test_li\1_uR9wnYMe.png')
im_rr = Image.fromarray(np.uint8(im_r))
print(im_rr.mode)
t = im_rr.convert("L")
print(t.mode)
im_rrr = Image.fromarray(np.uint8(t)*255)
print(im_rrr.mode)
im_rrr.save(r'C:\Users\Administrator\Desktop\test_li\convert_mode_l.png')
im_rrr

 

(2) 其次将 L 图像转换为 P 图像

     其中模式"P"为8位彩色模式,它的每一个像素用8个bit表示, 其对应的色彩是按照调色板查询出来的。

im_mode_8 = Image.open(r'C:\Users\Administrator\Desktop\test_li\convert_mode_l.png')
print(im_mode_8.mode)
colo8 = im_mode_8.convert("P")
colo8.save(r'C:\Users\Administrator\Desktop\test_li\convert_mode_8_color.png')
print(colo8.mode)
colo8

 

在 ImageWatch 中查看结果如图所示

 

效果图如下所示

效果图

 

 

参考文献:

             [1]  https://blog.csdn.net/qq_15969343/article/details/79840517

             [2]  http://www.pianshen.com/article/9838288269/

             [3]  https://www.jianshu.com/p/2e9539bdc307

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值