소스 뷰어
%%time
# 평균 변환, 광도 변환, 회색조 변환에 따른 이미지 비교
import numpy as np
from matplotlib import pyplot as plt
# 컬러 영상 읽기 
img = plt.imread( 'messi5.jpg' )

# 컬러 영상 채널 분리 
red  = img[:, :, 0]  # blue channel data
green = img[:, :, 1]  # green channel data
blue   = img[:, :, 2]  # red channel data

# 서브 챠트 생성 
idx = 0 
fig, charts = plt.subplots( 1, 3, figsize=(18, 17) )
charts = charts.flatten()

# 평균 회색조 변환
average_gray =  red/3 + green/3 + blue/3 

chart = charts[ idx ]
chart.imshow( average_gray, cmap='gray' )
chart.set_title( 'Average' )
idx += 1 

# Luminosity 회색조 변환
#luminosity_gray =  red*0.21 + green*0.72 + blue*0.07
luminosity_gray =  0.21*red + 0.72*green + 0.07*blue 

chart = charts[ idx ]
chart.imshow( luminosity_gray, cmap='gray' )
chart.set_title( 'Luminosity' )
idx += 1 

# Lightness 회색조 변환
lightness_gray = np.min( img, axis=2 )/2 + np.max( img, axis=2 )/2

chart = charts[ idx ]
chart.imshow( lightness_gray, cmap='gray' )
chart.set_title( 'Lightness' )
idx += 1 

# 챠트 출력 
plt.show()
No description has been provided for this image
Wall time: 432 ms
%%time
# 평균 변환, 광도 변환, 회색조 변환에 따른 영상 값의 차이 분석 
import numpy as np
from matplotlib import pyplot as plt
# 컬러 영상 읽기 
img = plt.imread( 'messi5.jpg' )

# 컬러 영상 채널 분리 
red  = img[:, :, 0]  # blue channel data
green = img[:, :, 1]  # green channel data
blue   = img[:, :, 2]  # red channel data

# 서브 챠트 생성 
idx = 0 
chart_rows = 3
chart_cols = 2
fig, charts = plt.subplots( chart_rows, chart_cols, figsize=(18, 17) )
charts = charts.flatten()

# 평균 회색조 변환
average_gray =  red/3 + green/3 + blue/3 

# Luminosity 회색조 변환
luminosity_gray =  0.21*red + 0.72*green + 0.07*blue 

# Lightness 회색조 변환
lightness_gray = np.min( img, axis=2 )/2 + np.max( img, axis=2 )/2

average_luminosity = average_gray - luminosity_gray
chart = charts[ idx ]
chart.imshow( average_luminosity, cmap='gray' )
chart.set_title( 'Average - Luminosity' )
idx += 1 

# 0 ~ 255 사이의 값으로 변환 
average_luminosity_norm = average_luminosity - np.min( average_luminosity )
chart = charts[ idx ]
chart.imshow( average_luminosity_norm, cmap='gray' )
chart.set_title( 'Average - Luminosity Normalize' )
idx += 1

luminosity_lightness = luminosity_gray - lightness_gray
chart = charts[ idx ]
chart.imshow( luminosity_lightness, cmap='gray' )
chart.set_title( 'Luminosity - Lightness' )
idx += 1 

luminosity_lightness_norm = luminosity_lightness - np.min( luminosity_lightness )
chart = charts[ idx ]
chart.imshow( luminosity_lightness_norm, cmap='gray' )
chart.set_title( 'Luminosity - Lightness Normalize' )
idx += 1 

lightness_average = lightness_gray - average_gray
chart = charts[ idx ]
chart.imshow( lightness_average, cmap='gray' )
chart.set_title( 'Lightness - Average' )
idx += 1 

lightness_average_norm = lightness_average - np.min( lightness_average )
chart = charts[ idx ]
chart.imshow( lightness_average_norm, cmap='gray' )
chart.set_title( 'Lightness - Average Normalize' )
idx += 1 

# 챠트 출력 
plt.show()
No description has been provided for this image
Wall time: 862 ms