소스 뷰어
%%time
import numpy as np
from matplotlib import pyplot as plt
img = plt.imread( 'messi5.jpg' )

# 회색조 변환
img = np.sum( [1/3]*3*img, axis=2 ).astype(np.uint8)
#img = np.sum( [1/3, 1/3, 1/3]*img, axis=2 ).astype(np.uint8)

# 히스토그램 추출 
xarr = np.arange( 0, 256, 1 )
yarr = np.zeros( 256 )

for x in xarr :
    yarr[ x ] = np.count_nonzero( img == x )
pass

# 차트 출력 
fig, charts = plt.subplots( 1, 2, figsize=(16, 8) )
charts = charts.flatten() ; idx = 0 

chart = charts[idx]
chart.imshow( img, cmap='gray' ) ; 
chart.set_title( "\nGrayscale Image\n" )
idx +=1

chart = charts[idx]
chart.plot( xarr, yarr )
chart.set_title( "\nHistogram\n" )
chart.set_xlabel( "Grayscale" ); chart.set_ylabel( "Count" )

plt.tight_layout()
plt.show()
uint8 (720, 1280)
No description has been provided for this image
Wall time: 560 ms
%%time
import numpy as np
from matplotlib import pyplot as plt
from skimage import data

# 예제 이미지 데이터
imgs =  [ 
         data.astronaut(), data.camera(), data.brick(),
         data.rocket(), data.coffee(), #data.cat(),
         data.coins(), data.clock(), data.moon(),
         data.grass(), data.checkerboard(),
         # data.text(), data.page(),
         # data.retina(), data.colorwheel(),
         # data.gravel(), data.microaneurysms(), data.shepp_logan_phantom(), data.cell(),
        ]

# 차트 출력 
fig, charts = plt.subplots( len(imgs), 2, figsize=(12, 35) ) 
charts = charts.flatten()
idx = 0 

for img in imgs :
    # 회색조 변환 
    if len( img.shape ) > 2 :
        img = np.sum( [1/3]*3*img, axis=2 ).astype(np.uint8)
    
    chart = charts[ idx ]
    chart.set_title( "Grayscale")
    chart.imshow( img , cmap= [None, 'gray'][ len(img.shape) == 2 ] )
    idx += 1
    
    # 히스토그램 추출 
    xarr = np.arange( 0, 256, 1 )
    yarr = np.zeros( 256 )

    for x in xarr :
        yarr[ x ] = np.count_nonzero( img == x )
    pass

    chart = charts[idx]
    chart.plot( xarr, yarr )
    chart.set_title( "Histogramn" )
    chart.set_xlabel( "Grayscale" ); chart.set_ylabel( "Count" )
    idx +=1
pass

plt.tight_layout(); plt.show()
No description has been provided for this image
Wall time: 5.57 s