[Python] How to read and display single-channel pictures

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

0. Preface

In deep learning, specifically semantic segmentation, tags are usually single-channel. The following is an introduction to the general method of reading and displaying it.
More pictures related knowledge refer to basic knowledge

1. Text

1.1 Method 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)

result:
Insert picture description here
Insert picture description here

1.2 Method two: 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)

Insert picture description here

references

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

Guess you like

Origin blog.csdn.net/weixin_39190382/article/details/113615763