import numpy as np
from skimage import exposure,data
image =data.camera()*1.0
用 numpy 包计算直方图。
hist1=np.histogram(image, bins=2)
用 skimage 计算直方图。
hist2=exposure.histogram(image, nbins=2)
print(hist1)
(array([ 93585, 168559]), array([ 0. , 127.5, 255. ]))
print(hist2)
(array([ 93585, 168559]), array([ 63.75, 191.25]))
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')hist 的参数非常多,但常用的就这几个,只有第一个是必须的,后面其它几个可选。
- arr: 需要计算直方图的一维数组。
- bins: 直方图的柱数,可选项,默认为10。
- normed: 是否将得到的直方图向量归一化,默认为0。
- facecolor: 直方图颜色。
- edgecolor: 直方图边框颜色。
- alpha: 透明度。
- histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’ 。
返回值 :
- n: 直方图向量,是否归一化由参数 normed设定。
- bins: 返回各个 bin的区间范围。
- patches: 返回每个 bin里面包含的数据,是一个list。
%matplotlib inline
from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
<Figure size 640x480 with 0 Axes>
原代码:
# n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')  
n, bins, patches = plt.hist(arr, bins=256,edgecolor='None',facecolor='red')  
plt.show()
from skimage import data
import matplotlib.pyplot as plt
img=data.chelsea()
ar=img[:,:,0].flatten()
# plt.hist(ar, bins=256, normed=1,facecolor='r',edgecolor='r')
plt.hist(ar, bins=256, facecolor='r',edgecolor='r')
ag=img[:,:,1].flatten()
# plt.hist(ag, bins=256, normed=1, facecolor='g',edgecolor='g')
plt.hist(ag, bins=256,  facecolor='g',edgecolor='g')
ab=img[:,:,2].flatten()
# ,host
# plt.hist(ab, bins=256, normed=1, facecolor='b',edgecolor='b')
plt.hist(ab, bins=256, facecolor='b',edgecolor='b')
plt.show()
其中,在 plt.hist(ab, bins=256, normed=1, facecolor='b',edgecolor='b',host=1)
加一个参数 hold=1,表示可以叠加。
from skimage import data,exposure
import matplotlib.pyplot as plt
img=data.moon()
plt.figure("hist",figsize=(8,8))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
原始图像,原始图像直方图:
arr=img.flatten()
plt.subplot(221)
<Axes: >
原始图像。
plt.imshow(img,plt.cm.gray)
plt.subplot(222)
<Axes: >
原始图像直方图。
# plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')
plt.hist(arr, bins=256, edgecolor='None',facecolor='red')
(array([2.4000e+02, 0.0000e+00, 6.0000e+01, 3.6000e+01, 0.0000e+00,
        6.0000e+01, 5.2000e+01, 0.0000e+00, 5.2000e+01, 0.0000e+00,
        6.0000e+01, 8.0000e+01, 0.0000e+00, 4.0000e+00, 4.4000e+01,
        0.0000e+00, 5.6000e+01, 0.0000e+00, 8.8000e+01, 7.2000e+01,
        0.0000e+00, 8.0000e+01, 7.6000e+01, 0.0000e+00, 5.2000e+01,
        6.8000e+01, 0.0000e+00, 6.4000e+01, 0.0000e+00, 8.8000e+01,
        8.0000e+01, 0.0000e+00, 7.2000e+01, 4.0000e+01, 0.0000e+00,
        7.2000e+01, 3.6000e+01, 0.0000e+00, 4.8000e+01, 6.4000e+01,
        0.0000e+00, 6.4000e+01, 4.8000e+01, 0.0000e+00, 2.8000e+01,
        7.6000e+01, 0.0000e+00, 8.4000e+01, 7.6000e+01, 8.8000e+01,
        0.0000e+00, 8.0000e+01, 1.0800e+02, 0.0000e+00, 5.2000e+01,
        7.6000e+01, 9.2000e+01, 0.0000e+00, 8.8000e+01, 1.0000e+02,
        1.0000e+02, 0.0000e+00, 1.0400e+02, 0.0000e+00, 1.3200e+02,
        1.0800e+02, 0.0000e+00, 1.4800e+02, 1.2400e+02, 1.6800e+02,
        1.6000e+02, 1.6000e+02, 0.0000e+00, 2.1200e+02, 2.0800e+02,
        1.8000e+02, 2.0000e+02, 2.6400e+02, 2.1600e+02, 2.2000e+02,
        3.1200e+02, 2.2400e+02, 2.7200e+02, 2.9600e+02, 2.8400e+02,
        3.1600e+02, 2.5200e+02, 5.3600e+02, 3.6800e+02, 4.0800e+02,
        3.7200e+02, 8.2400e+02, 3.5200e+02, 5.9600e+02, 4.5600e+02,
        8.6000e+02, 9.1600e+02, 4.0000e+02, 8.8000e+02, 9.0800e+02,
        5.8000e+02, 1.8600e+03, 1.4160e+03, 2.5640e+03, 2.0360e+03,
        3.3920e+03, 5.9280e+03, 5.7240e+03, 1.1964e+04, 1.1436e+04,
        1.6256e+04, 1.7772e+04, 2.0324e+04, 2.1444e+04, 1.7484e+04,
        2.3296e+04, 1.6144e+04, 1.2096e+04, 1.1748e+04, 9.4080e+03,
        9.0200e+03, 5.0800e+03, 5.3160e+03, 2.7760e+03, 1.3640e+03,
        2.0560e+03, 8.7200e+02, 1.2600e+03, 8.6800e+02, 3.7600e+02,
        5.5200e+02, 1.7600e+02, 4.3200e+02, 1.9200e+02, 1.5600e+02,
        2.5200e+02, 1.1200e+02, 1.3600e+02, 1.1600e+02, 1.0400e+02,
        8.8000e+01, 1.1600e+02, 1.0000e+02, 8.8000e+01, 6.0000e+01,
        1.0800e+02, 7.6000e+01, 1.1600e+02, 8.8000e+01, 7.6000e+01,
        6.8000e+01, 9.2000e+01, 7.2000e+01, 0.0000e+00, 4.4000e+01,
        6.4000e+01, 5.6000e+01, 0.0000e+00, 1.1200e+02, 7.2000e+01,
        7.6000e+01, 0.0000e+00, 9.6000e+01, 5.2000e+01, 0.0000e+00,
        0.0000e+00, 3.2000e+01, 4.4000e+01, 0.0000e+00, 4.8000e+01,
        0.0000e+00, 7.6000e+01, 2.8000e+01, 0.0000e+00, 3.6000e+01,
        3.6000e+01, 0.0000e+00, 4.4000e+01, 0.0000e+00, 2.8000e+01,
        0.0000e+00, 2.0000e+01, 5.2000e+01, 0.0000e+00, 2.0000e+01,
        0.0000e+00, 3.2000e+01, 0.0000e+00, 1.2000e+01, 0.0000e+00,
        2.0000e+01, 0.0000e+00, 1.2000e+01, 0.0000e+00, 1.2000e+01,
        0.0000e+00, 1.2000e+01, 0.0000e+00, 2.0000e+01, 0.0000e+00,
        4.0000e+00, 0.0000e+00, 2.8000e+01, 0.0000e+00, 8.0000e+00,
        0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 1.6000e+01,
        0.0000e+00, 8.0000e+00, 0.0000e+00, 1.2000e+01, 0.0000e+00,
        0.0000e+00, 1.2000e+01, 0.0000e+00, 2.0000e+01, 0.0000e+00,
        2.8000e+01, 0.0000e+00, 0.0000e+00, 2.0000e+01, 0.0000e+00,
        1.2000e+01, 0.0000e+00, 0.0000e+00, 3.6000e+01, 0.0000e+00,
        4.8000e+01, 0.0000e+00, 0.0000e+00, 2.4000e+01, 0.0000e+00,
        2.4000e+01, 0.0000e+00, 3.6000e+01, 0.0000e+00, 0.0000e+00,
        2.4000e+01, 0.0000e+00, 1.6000e+01, 0.0000e+00, 0.0000e+00,
        1.2000e+01, 0.0000e+00, 1.6000e+01, 0.0000e+00, 0.0000e+00,
        4.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00,
        4.0000e+00]),
 array([  0.        ,   0.99609375,   1.9921875 ,   2.98828125,
          3.984375  ,   4.98046875,   5.9765625 ,   6.97265625,
          7.96875   ,   8.96484375,   9.9609375 ,  10.95703125,
         11.953125  ,  12.94921875,  13.9453125 ,  14.94140625,
         15.9375    ,  16.93359375,  17.9296875 ,  18.92578125,
         19.921875  ,  20.91796875,  21.9140625 ,  22.91015625,
         23.90625   ,  24.90234375,  25.8984375 ,  26.89453125,
         27.890625  ,  28.88671875,  29.8828125 ,  30.87890625,
         31.875     ,  32.87109375,  33.8671875 ,  34.86328125,
         35.859375  ,  36.85546875,  37.8515625 ,  38.84765625,
         39.84375   ,  40.83984375,  41.8359375 ,  42.83203125,
         43.828125  ,  44.82421875,  45.8203125 ,  46.81640625,
         47.8125    ,  48.80859375,  49.8046875 ,  50.80078125,
         51.796875  ,  52.79296875,  53.7890625 ,  54.78515625,
         55.78125   ,  56.77734375,  57.7734375 ,  58.76953125,
         59.765625  ,  60.76171875,  61.7578125 ,  62.75390625,
         63.75      ,  64.74609375,  65.7421875 ,  66.73828125,
         67.734375  ,  68.73046875,  69.7265625 ,  70.72265625,
         71.71875   ,  72.71484375,  73.7109375 ,  74.70703125,
         75.703125  ,  76.69921875,  77.6953125 ,  78.69140625,
         79.6875    ,  80.68359375,  81.6796875 ,  82.67578125,
         83.671875  ,  84.66796875,  85.6640625 ,  86.66015625,
         87.65625   ,  88.65234375,  89.6484375 ,  90.64453125,
         91.640625  ,  92.63671875,  93.6328125 ,  94.62890625,
         95.625     ,  96.62109375,  97.6171875 ,  98.61328125,
         99.609375  , 100.60546875, 101.6015625 , 102.59765625,
        103.59375   , 104.58984375, 105.5859375 , 106.58203125,
        107.578125  , 108.57421875, 109.5703125 , 110.56640625,
        111.5625    , 112.55859375, 113.5546875 , 114.55078125,
        115.546875  , 116.54296875, 117.5390625 , 118.53515625,
        119.53125   , 120.52734375, 121.5234375 , 122.51953125,
        123.515625  , 124.51171875, 125.5078125 , 126.50390625,
        127.5       , 128.49609375, 129.4921875 , 130.48828125,
        131.484375  , 132.48046875, 133.4765625 , 134.47265625,
        135.46875   , 136.46484375, 137.4609375 , 138.45703125,
        139.453125  , 140.44921875, 141.4453125 , 142.44140625,
        143.4375    , 144.43359375, 145.4296875 , 146.42578125,
        147.421875  , 148.41796875, 149.4140625 , 150.41015625,
        151.40625   , 152.40234375, 153.3984375 , 154.39453125,
        155.390625  , 156.38671875, 157.3828125 , 158.37890625,
        159.375     , 160.37109375, 161.3671875 , 162.36328125,
        163.359375  , 164.35546875, 165.3515625 , 166.34765625,
        167.34375   , 168.33984375, 169.3359375 , 170.33203125,
        171.328125  , 172.32421875, 173.3203125 , 174.31640625,
        175.3125    , 176.30859375, 177.3046875 , 178.30078125,
        179.296875  , 180.29296875, 181.2890625 , 182.28515625,
        183.28125   , 184.27734375, 185.2734375 , 186.26953125,
        187.265625  , 188.26171875, 189.2578125 , 190.25390625,
        191.25      , 192.24609375, 193.2421875 , 194.23828125,
        195.234375  , 196.23046875, 197.2265625 , 198.22265625,
        199.21875   , 200.21484375, 201.2109375 , 202.20703125,
        203.203125  , 204.19921875, 205.1953125 , 206.19140625,
        207.1875    , 208.18359375, 209.1796875 , 210.17578125,
        211.171875  , 212.16796875, 213.1640625 , 214.16015625,
        215.15625   , 216.15234375, 217.1484375 , 218.14453125,
        219.140625  , 220.13671875, 221.1328125 , 222.12890625,
        223.125     , 224.12109375, 225.1171875 , 226.11328125,
        227.109375  , 228.10546875, 229.1015625 , 230.09765625,
        231.09375   , 232.08984375, 233.0859375 , 234.08203125,
        235.078125  , 236.07421875, 237.0703125 , 238.06640625,
        239.0625    , 240.05859375, 241.0546875 , 242.05078125,
        243.046875  , 244.04296875, 245.0390625 , 246.03515625,
        247.03125   , 248.02734375, 249.0234375 , 250.01953125,
        251.015625  , 252.01171875, 253.0078125 , 254.00390625,
        255.        ]),
 <BarContainer object of 256 artists>)
均衡化图像,均衡化直方图:
img1=exposure.equalize_hist(img)
arr1=img1.flatten()
plt.subplot(223)
<Axes: >
均衡化图像。
plt.imshow(img1,plt.cm.gray) 
plt.subplot(224)
<Axes: >
均衡化直方图。
# plt.hist(arr1, bins=256, normed=1,edgecolor='None',facecolor='red') 
plt.hist(arr1, bins=256, edgecolor='None',facecolor='red') 
plt.show()