【python】单通道图片的读取和显示方法

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

0. 前言

深度学习中,具体来说是语义分割中,标签通常是单通道的。下面是对其读取和显示的一般方法介绍
更多图片相关知识参考基础知识

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

猜你喜欢

转载自blog.csdn.net/weixin_39190382/article/details/113615763
今日推荐