### Packages
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
### 출력 영상 크기
plt.rcParams["figure.figsize"] = (16,9)
### 한글 표시
plt.rcParams['font.family'] = "Gulim" # 'AppleGothic' in mac

### 감마보정 값 0.4를 적용
gamma = 0.4
lookUpTable = np.empty((1, 256), np.uint8)
for i in range(256) :
lookUpTable[0, i] = np.clip(pow (i / 255.0, gamma) * 255.0, 0, 255)
lookUpTable
array([[ 0, 27, 36, 43, 48, 52, 56, 60, 63, 66, 69, 72, 75,
77, 79, 82, 84, 86, 88, 90, 92, 93, 95, 97, 99, 100,
102, 103, 105, 106, 108, 109, 111, 112, 113, 115, 116, 117, 119,
120, 121, 122, 123, 125, 126, 127, 128, 129, 130, 131, 132, 133,
134, 136, 137, 138, 139, 140, 141, 141, 142, 143, 144, 145, 146,
147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 157,
158, 159, 160, 161, 161, 162, 163, 164, 165, 165, 166, 167, 168,
168, 169, 170, 171, 171, 172, 173, 173, 174, 175, 176, 176, 177,
178, 178, 179, 180, 180, 181, 182, 182, 183, 184, 184, 185, 186,
186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 192, 193, 194,
194, 195, 195, 196, 197, 197, 198, 198, 199, 200, 200, 201, 201,
202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 207, 208, 208,
209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 215,
216, 216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222,
222, 223, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228, 228,
229, 229, 229, 230, 230, 231, 231, 232, 232, 233, 233, 234, 234,
235, 235, 235, 236, 236, 237, 237, 238, 238, 239, 239, 239, 240,
240, 241, 241, 242, 242, 242, 243, 243, 244, 244, 245, 245, 245,
246, 246, 247, 247, 248, 248, 248, 249, 249, 250, 250, 250, 251,
251, 252, 252, 252, 253, 253, 254, 254, 255]], dtype=uint8)
### 감마보정 함수
def fn_gamma_correction(img, gamma=1):
lookUpTable = np.empty((1, 256), np.uint8)
for i in range(256) :
lookUpTable[0, i] = np.clip(pow (i / 255.0, gamma) * 255.0, 0, 255)
return cv2.LUT(img, lookUpTable)
### 영상 읽기
img_lena = cv2.imread(r'D:\image\lena.png')
img_lena.shape
(512, 512, 3)
### 감마보정
img_gamma_1 = fn_gamma_correction(img_lena, 0.40)
img_gamma_2 = fn_gamma_correction(img_lena, 0.67)
img_gamma_3 = fn_gamma_correction(img_lena, 1.00)
img_gamma_4 = fn_gamma_correction(img_lena, 1.50)
img_gamma_5 = fn_gamma_correction(img_lena, 2.50)
### 영상 출력
titles = ["원래 영상", "$\gamma$=0.40", "$\gamma$=0.67", "$\gamma$=1.00", "$\gamma$=1.50", "$\gamma$=2.50"]
images = [img_lena, img_gamma_1, img_gamma_2, img_gamma_3, img_gamma_4, img_gamma_5]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(2, 3, i+1)
plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 경계 값을 이용한 변환
levels = 5
lookUpTable = np.arange(256, dtype='uint8')
for i in range(levels):
lookUpTable[(lookUpTable >= i*255/levels) & (lookUpTable < (i+1)*255/levels)] = i*255/(levels-1)
lookUpTable
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 191, 191, 191,
191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191,
191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191,
191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191,
191, 191, 191, 191, 191, 191, 191, 191, 191, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8)
### 포스터라이징 함수
def fn_posterizing(img, levels=2):
lookUpTable = np.arange(256, dtype='uint8')
for i in range(levels) :
lookUpTable[(lookUpTable >= i*255/levels) & (lookUpTable < (i+1)*255/levels)] = i*255/(levels-1)
return cv2.LUT(img, lookUpTable)
### 영상 읽기 - grayscale
img_lena_gray = cv2.imread(r'D:\image\lena.png', 0)
img_lena_gray.shape
(512, 512)
### 포스터라이징
img_poster_1 = fn_posterizing(img_lena_gray, 2)
img_poster_2 = fn_posterizing(img_lena_gray, 5)
img_poster_3 = fn_posterizing(img_lena_gray, 10)
img_poster_4 = fn_posterizing(img_lena_gray, 50)
img_poster_5 = fn_posterizing(img_lena_gray, 100)
### 영상 출력
titles = ["원래 영상", "levels=2", "levels=5", "levels=10", "levels=50", "levels=100"]
images = [img_lena_gray, img_poster_1, img_poster_2, img_poster_3, img_poster_4, img_poster_5]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(2, 3, i+1)
plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 범위 강조 함수
def fn_highlight(img, min_value, max_value, out_value):
lookUpTable = np.arange(256, dtype='uint8')
lookUpTable[(lookUpTable >= min_value) & (lookUpTable < max_value)] = out_value
return cv2.LUT(img, lookUpTable)
### 범위 강조
img_highlight_1 = fn_highlight(img_lena_gray, 200, 255, 255)
img_highlight_2 = fn_highlight(img_lena_gray, 0, 150, 0)
img_highlight_3 = fn_highlight(img_lena_gray, 50, 100, 0)
### 영상 출력
titles = ["원래 영상", "200~255→255", "0~150→0", "50~100→0"]
images = [img_lena_gray, img_highlight_1, img_highlight_2, img_highlight_3]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(2, 2, i+1)
plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 이진화 - grayscale
ret, img_thresh_1 = cv2.threshold(img_lena_gray, 127, 255, cv2.THRESH_BINARY)
ret, img_thresh_2 = cv2.threshold(img_lena_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, img_thresh_3 = cv2.threshold(img_lena_gray, 127, 255, cv2.THRESH_TRUNC)
ret, img_thresh_4 = cv2.threshold(img_lena_gray, 127, 255, cv2.THRESH_TOZERO)
ret, img_thresh_5 = cv2.threshold(img_lena_gray, 127, 255, cv2.THRESH_TOZERO_INV)
### 영상 출력
titles = ["원래 영상", "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC", "THRESH_TOZERO", "THRESH_TOZERO_INV"]
images = [img_lena_gray, img_thresh_1, img_thresh_2, img_thresh_3, img_thresh_4, img_thresh_5]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(2, 3, i+1)
plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 영상 읽기
img_raw = cv2.imread(r'D:\image\bp_white_re.jpg')
### 마스크 - 이진영상
img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
ret, img_binary_inv = cv2.threshold(img_gray, 240, 255, cv2.THRESH_BINARY_INV)
### 논리연산 → 영상 추출
img_bp = cv2.bitwise_and(img_raw, cv2.cvtColor(img_binary_inv, cv2.COLOR_GRAY2BGR))
### 영상 읽기
img1 = cv2.imread(r'D:\image\img1.jpg')
### 마스크 - 이진영상
ret, img_binary = cv2.threshold(img_gray, 240, 255, cv2.THRESH_BINARY)
### 논리연산 → 영상 추출
img_bg = cv2.bitwise_and(img1, cv2.cvtColor(img_binary, cv2.COLOR_GRAY2BGR))
### 산술연산 → 영상 합성
img_bp_bg = cv2.add(img_bp, img_bg)
### 영상 출력
images = [img_raw, img_binary_inv, img_bp,
img1, img_binary, img_bg,
img_bp, img_bg, img_bp_bg,]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(3,3,i+1),plt.imshow(img_rgb)
plt.axis('off')
plt.show()