영상처리 Chap04_영상향상
- 색상히스토그램
12345678910111213141516from PIL import Imageimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im2 = np.array(im)print(im2.shape)im2 = im2.reshape(-1)print(im2.shape)plt.figure(figsize=(10, 5))plt.subplot(1,2,1), plt.imshow(im, cmap='gray')plt.subplot(1,2,2), plt.hist(im2, bins=64, range=(0,256), color='gray', edgecolor='black', alpha=0.5)plt.show()
- 색상 값 이동
1234567891011121314from PIL import Imageimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im2 = np.array(im, dtype='float') - 50im2 = np.clip(im2, 0, 255)plt.figure(figsize=(10, 5))plt.subplot(1,2,1), plt.imshow(im2, cmap='gray')plt.subplot(1,2,2), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5)plt.show()
- 파워-로우 변환
12345678910111213141516171819from PIL import Imageimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im = np.array(im, dtype='float')im2 = im ** 0.3im3 = im ** 3plt.figure(figsize=(15, 10))plt.subplot(2,3,1), plt.imshow(im, cmap='gray')plt.subplot(2,3,2), plt.imshow(im2, cmap='gray')plt.subplot(2,3,3), plt.imshow(im3, cmap='gray')plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5)plt.show()
- 콘트라스트 스트레칭
1234567891011121314151617181920from PIL import Imagefrom skimage import img_as_ubyte, img_as_floatimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im = np.array(im, dtype='float')im2 = (255 * im - 22950) / 48; im2 = np.clip(im2, 0, 255) # (90 ~ 138)im3 = (255 * im - 12750) / 48; im3 = np.clip(im3, 0, 255) # (50 ~ 98)plt.figure(figsize=(15, 10))plt.subplot(2,3,1), plt.imshow(im, cmap='gray')plt.subplot(2,3,2), plt.imshow(im2, cmap='gray')plt.subplot(2,3,3), plt.imshow(im3, cmap='gray')plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5)plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5)plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5)plt.show()
- 임계화(thresholding)
1234567891011121314151617181920from PIL import Imagefrom skimage import img_as_ubyteimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im = np.array(im, dtype='float')im2 = np.copy(im); im2[im2 <= 100] = 0; im2[im2 > 100] = 1 # thres = 100im3 = np.copy(im); im3[im3 <= 150] = 0; im3[im3 > 150] = 1 # thres = 150plt.figure(figsize=(15, 10))plt.subplot(2,3,1), plt.imshow(im, cmap='gray')plt.subplot(2,3,2), plt.imshow(im2, cmap='gray')plt.subplot(2,3,3), plt.imshow(im3, cmap='gray')plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5)plt.show()
- 히스토그램 평활화
1234567891011121314151617181920from PIL import Imagefrom skimage import exposureimport matplotlib.pyplot as pltimport numpy as npim = Image.open('./images/parrot.png')im = im.convert('L')im = np.array(im, dtype='float') / 255.0im2 = exposure.equalize_hist(im) #전역 평활화 ( 전역 연산 )im3 = exposure.equalize_adapthist(im, clip_limit=0.03) #지역 평활화 ( 적응 연산 )plt.figure(figsize=(15, 10))plt.subplot(2,3,1), plt.imshow(im, cmap='gray')plt.subplot(2,3,2), plt.imshow(im2, cmap='gray')plt.subplot(2,3,3), plt.imshow(im3, cmap='gray')plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5)plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5)plt.show()