소스 뷰어
# 합성곱 예제
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal
img = plt.imread('messi5.jpg')
img = np.sum( img*[.2126, .7152, .0722], axis=2 ).astype('uint8')
img_org = img
# edge convolution
img_conv = signal.convolve( img, [ [-1,-1,-1], [-1, 8,-1], [-1,-1,-1] ] )
# 임계치 처리
avg = np.average( img_conv )/8
img_thresh = np.where( img_conv < avg, 0, 1 )
row_cnt = 1; col_cnt=3
fig, charts = plt.subplots( row_cnt, col_cnt,figsize=(5*col_cnt, 5*row_cnt) )
charts = charts.flatten() if row_cnt*col_cnt >= 2 else [ charts ]
idx = 0
chart = charts[idx]
chart.set_title( "\nOriginal\n" );
chart.imshow( img_org, cmap='gray' ); idx += 1
chart = charts[idx]
chart.set_title( "\nEdge Convolution\n" );
chart.imshow( img_conv, cmap='gray' ); idx += 1
chart = charts[idx]
chart.set_title( "\nThreshold\n" );
chart.imshow( img_thresh, cmap='gray' ); idx += 1
plt.tight_layout(); plt.show()