????????以前沒用過matplotlib的讀圖方式,今天在別人的工程里看到這個用法,自己改寫別人工程時,怎么都找不到問題,最后在最初,開始讀圖的時候發現了問題。
目錄
🌷🌷1.對于png格式的3波段uint8圖像
🍀🍀1.1 skimage.io讀圖
🌾🌾???????1.2 matplotlib.image.imread讀圖
🍓🍓1.3 python代碼
?skimage.io代碼
?matplotlib.image.imread代碼
🌷🌷2. 對于tif格式的4波段uint8圖像
🍀🍀2.1skimage.io讀圖
🍓🍓2.2matplotlib.image.imread讀圖
🍓🍓2.3python代碼
?skimage.io代碼
?matplotlib.image.imread代碼
🌷🌷3.總結
整理不易,歡迎一鍵三連!!!
送你們一條美麗的--分割線--
首先說區別:
🌷🌷???????1.對于png格式的3波段uint8圖像
🍀🍀1.1 skimage.io讀圖
????????讀取的就是uint8的[0,255]的3波段
🌾🌾???????1.2 matplotlib.image.imread讀圖
????????讀取的就是float32的[0,1]的3波段,并且讀到的像素值=原始像素值/255。
🍓🍓1.3 python代碼
????????python代碼展示如下,同一張圖不同的方式讀取,結果不同。
?skimage.io代碼
>>> from skimage import io
>>> a = io.imread('./samples/A/1_0.png')
>>> a
array([[[ 40, 61, 68],[ 57, 78, 85],[ 73, 94, 101],...,[ 92, 112, 119],[ 89, 109, 116],[ 75, 95, 102]],[[ 47, 68, 75],[ 62, 83, 90],[ 75, 96, 103],...,[ 91, 111, 118],[ 89, 109, 116],[ 77, 97, 104]],[[ 51, 72, 79],[ 64, 85, 92],[ 74, 95, 102],...,[ 80, 100, 107],[ 83, 103, 110],[ 73, 93, 100]],...,[[ 54, 67, 66],[ 66, 79, 78],[ 86, 96, 94],...,[135, 139, 142],[133, 137, 140],[132, 136, 139]],[[ 57, 70, 69],[ 71, 84, 83],[ 91, 101, 99],...,[132, 136, 138],[130, 134, 136],[129, 133, 135]],[[ 60, 73, 72],[ 77, 90, 89],[ 96, 106, 104],...,[127, 131, 133],[126, 130, 132],[126, 130, 132]]], dtype=uint8)
?matplotlib.image.imread代碼
>>> from matplotlib.image import imread
>>> b = imread('./samples/A/1_0.png')
>>> b
array([[[0.15686275, 0.23921569, 0.26666668],[0.22352941, 0.30588236, 0.33333334],[0.28627452, 0.36862746, 0.39607844],...,[0.36078432, 0.4392157 , 0.46666667],[0.34901962, 0.42745098, 0.45490196],[0.29411766, 0.37254903, 0.4 ]],[[0.18431373, 0.26666668, 0.29411766],[0.24313726, 0.3254902 , 0.3529412 ],[0.29411766, 0.3764706 , 0.40392157],...,[0.35686275, 0.43529412, 0.4627451 ],[0.34901962, 0.42745098, 0.45490196],[0.3019608 , 0.38039216, 0.40784314]],[[0.2 , 0.28235295, 0.30980393],[0.2509804 , 0.33333334, 0.36078432],[0.2901961 , 0.37254903, 0.4 ],...,[0.3137255 , 0.39215687, 0.41960785],[0.3254902 , 0.40392157, 0.43137255],[0.28627452, 0.3647059 , 0.39215687]],...,[[0.21176471, 0.2627451 , 0.25882354],[0.25882354, 0.30980393, 0.30588236],[0.3372549 , 0.3764706 , 0.36862746],...,[0.5294118 , 0.54509807, 0.5568628 ],[0.52156866, 0.5372549 , 0.54901963],[0.5176471 , 0.53333336, 0.54509807]],[[0.22352941, 0.27450982, 0.27058825],[0.2784314 , 0.32941177, 0.3254902 ],[0.35686275, 0.39607844, 0.3882353 ],...,[0.5176471 , 0.53333336, 0.5411765 ],[0.50980395, 0.5254902 , 0.53333336],[0.5058824 , 0.52156866, 0.5294118 ]],[[0.23529412, 0.28627452, 0.28235295],[0.3019608 , 0.3529412 , 0.34901962],[0.3764706 , 0.41568628, 0.40784314],...,[0.49803922, 0.5137255 , 0.52156866],[0.49411765, 0.50980395, 0.5176471 ],[0.49411765, 0.50980395, 0.5176471 ]]], dtype=float32)
🌷🌷2. 對于tif格式的4波段uint8圖像
🍀🍀2.1skimage.io讀圖
????????讀取的就是uint8的[0,255]的4波段
🍓🍓2.2matplotlib.image.imread讀圖
????????讀取的也是uint8的[0,255]的4波段,二者讀圖結果一致。
🍓🍓2.3python代碼
????????python代碼展示如下,同一張圖不同的方式讀取,結果相同。
?skimage.io代碼
>>> from skimage import io
>>> a = io.imread('/home/zhouyuan/mnt/54.15.zhouyuan5/鄂爾多斯/cut_2019.tif')
>>> a.shape
(3179, 4834, 4)>>> import numpy as np
>>> np.unique(a)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,248, 249, 250, 251, 252, 253, 254], dtype=uint8)
?matplotlib.image.imread代碼
>>> from matplotlib.image import imread
>>> b = imread('2019.tif')
>>> b.shape
(3179, 4834, 4)
>>> np.unique(b)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,248, 249, 250, 251, 252, 253, 254], dtype=uint8)
🌷🌷3.總結
????????這是因為matplotlib.image.imread讀圖有一個可選的參數format,用于讀取數據的圖像文件格式。如果沒有給出,則從文件名中推斷格式。如果無法推導出任何結果,則嘗試使用png。
具體的推導情況有以下三種:
format | |
返回: | numpy.array 圖像數據。返回的數組具有形狀
|
---|
?
整理不易,歡迎一鍵三連!!!
送你們一條美麗的--分割線--
🌷🌷🍀🍀🌾🌾🍓🍓🍂🍂🙋🙋🐸🐸🙋🙋💖💖🍌🍌🔔🔔🍉🍉🍭🍭🍋🍋🍇🍇🏆🏆📸📸????🍎🍎👍👍🌷🌷