开发者社区> 问答> 正文

python中怎么读取图片

python中怎么读取图片

展开
收起
云计算小粉 2018-05-10 20:09:58 2520 0
1 条回答
写回答
取消 提交回答
  • 阿里云ping https://ping.gaomeluo.com/aliyun/
    1. opencv-python包
      opencv的像素值在0,1之间,存储的时候转换到0,255,show的时候转换到[0,255]

    import cv2
    img = cv2.imread("imgfile")
    cv2.imshow("img_win_name", img)
    cv2.waitKey(0) # 无限期等待输入

    cv2.imwrite("write_file_name", img)

    注意,opencv打开图片的格式为:
    height×width×channels
    height×width×channels

    即通道数在最后面
    其中33个通道的顺序分别是BB、GG、RR。
    分离方法为:
    b, g, r = cv2.split(img)

    1. scikit-image包
      scikit-image的像素值在−1,1之间,存储的时候转换到0,255,show的时候转换到[0,255]

    import skimage.io as io
    import matplotlib.pyplot as plt

    img = io.imread("a.jpg")
    io.imshow(img)
    plt.show()

    注意skimage读取图片也是height×width×channelsheight×width×channels
    通道顺序是R,G,BR,G,B

    1. matplotlib包
      matplotlib的像素值在[-1,1]之间,存储的时候转换到0,255,show的时候转换到[0,255]

    import matplotlib.pyplot as plt

    img = plt.imread("img_name")
    plt.imshow(img)

    matplotlib读取图片也是height×widht×channelsheight×widht×channels,也是R,G,BR,G,B

    1. tifffile包
    2. tifffile as tiff

    将图片的像素值放缩到[0,1]之间

    def scale_percentile(matrix):

    w, h, d = matrix.shape
    matrix = np.reshape(matrix, [w * h, d]).astype(np.float64)
    # Get 2nd and 98th percentile
    mins = np.percentile(matrix, 1, axis=0)
    maxs = np.percentile(matrix, 99, axis=0) - mins
    matrix = (matrix - mins[None, :]) / maxs[None, :]
    matrix = np.reshape(matrix, [w, h, d])
    matrix = matrix.clip(0, 1)
    return matrix
    

    img = tiff.imread("file_name")
    tiff.imshow(scale_percentile(img))

    注意tifffile的图片的读取顺序height×width×channelsheight×width×channels。R,G,BR,G,B按照波段来获取。

    2019-07-17 22:19:56
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载