오늘 수행하려고 했던 과제는 총 4가지였습니다.
- 사진 읽어오기
- 사진을 회색으로 변환시키기
- 회색 사진의 선명도를 바꿔보기 (실패)
- histogram과 cumsum 그려보기
1. 사진 읽어오기
import PIL.Image as pilimg
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# 사진 읽기
im = pilimg.open('IMG_3514.jpg').convert('L') # 수정 사진
image = pilimg.open('IMG_3514.jpg') #원본 사진
#이미지 픽셀을 numpy array로 변환
pix = np.array(im) # 수정 사진
pix1 = np.array(image) # 원본 사진
plt.imshow(pix, cmap='gray') # 수정 사진 그리기
plt.show()
처음에는 계속 원본사진만 출력되었습니다. 아무리 cmap을 해도 되지 않았습니다. 그래서 convert('L')을 붙여줬더니 cmap이 수행되었습니다. 이유는 저도 잘 모르겠지만 좀 더 이 부분에 대해서는 알아봐야겠습니다.
2. 원본사진과 흑백사진 비교
min = np.min(pix)
max = np.max(pix)
fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(1,2,1)
im1 = ax1.imshow(pix, cmap=cm.gray, vmin= min, vmax = max) # 수정 사진
plt.colorbar(im1, ax=ax1)
ax2 = fig.add_subplot(1,2,2)
im2 = ax2.imshow(pix1) # 원본 사진
plt.colorbar(im2, ax=ax2)
3. histogram과 cumsum 그리기
span = max - min
histogram, x = np.histogram(pix, bins=np.arange(min,max, span/256))
chistogram = np.cumsum(histogram)
histogram = histogram / np.max(histogram)
chistogram = chistogram / np.max(chistogram)
fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(1,2,1)
im1 = ax1.imshow(pix, cmap=cm.gray)
plt.colorbar(im1, ax=ax1)
ax2 = fig.add_subplot(1,2,2)
im2 = ax2.bar(x[:-1], histogram, width=span/256, color='gray') #histogram
im3 = ax2.plot(x[:-1], chistogram, color="red") #cumsum
plt.show()