【python】语义分割(标签)黑色(单通道)图片的读取和显示方法

every blog every motto: Just live your life cause we don’t live twice.

0. 前言

深度学习中,具体来说是语义分割中,标签通常是单通道的。

通常像素值代表类别,所以有时候像素值会比较小,如,0,1,2,3等(我们知道0表示黑色,255表示白色),这也就导致我们的标签看起来是黑色的。

下面是对其读取和显示的一般方法介绍
更多图片相关知识参考基础知识

1. 正文

1.1 方法一:matplotlib

import matplotlib.pyplot as plt
import matplotlib.image as mp

def show_img(path):
    """ 读取并展示图片

    :param path: 图片路径
    :return:
    """
    img = mp.imread(path)
    print('图片的shape:', img.shape)
    plt.imshow(img)
    plt.show()


show_img(y_path)

结果:
在这里插入图片描述
在这里插入图片描述

1.2 方法二:opencv

def image_normalization(img, img_min=0, img_max=255):
    """数据正则化,将数据从一个小范围变换到另一个范围
        默认参数:从(0,1) -> (0,255)

    :param img: 输入数据
    :param img_min: 数据最小值
    :param img_max: 数据最大值
    :return: 返回变换后的结果结果
    """
    img = np.float32(img)
    epsilon = 1e-12
    img = (img - np.min(img)) * (img_max - img_min) / ((np.max(img) - np.min(img)) + epsilon) + img_min

    return img


def show_img3(path):
    """ 利用opencv 读取并显示单通道图片

    :param path: 图片路径
    :return:
    """
    # 读取图片
    img = cv.imread(path, cv.IMREAD_UNCHANGED)
    # 将图片的值从一个小范围 转换到大范围
    img = image_normalization(img)
    # 改为uint8型
    img = img.astype('uint8')
    # 显示
    cv.imshow('single channel', img)
    cv.waitKey(0)


show_img3(y_path)

在这里插入图片描述

参考文献

[1] https://blog.csdn.net/weixin_39190382/article/details/105917690
[2] https://www.jb51.net/article/102981.htm

  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡侃有料

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值