### 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
### 영상 읽기
img_raw = cv2.imread(r'D:\image\lena.png')
### Grayscale
img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
img_gray.shape
(512, 512)
### 영상 출력 - grayscale
plt.imshow(img_gray, 'gray')
plt.show()
### 덧셈
img_1 = cv2.add(img_gray, 50)
img_2 = cv2.add(img_gray, 100)
img_3 = cv2.add(img_gray, 200)
### 영상 출력
titles = ["Original", "+50", "+100", "+200"]
images = [img_gray, img_1, img_2, img_3]
for i in range(len(images)):
plt.subplot(1, 4, i+1)
plt.imshow(images[i], "gray")
plt.title(titles[i])
plt.axis('off')
plt.show()
### 뺄셈
img_1 = cv2.subtract(img_gray, 50) # cv2.add(img_gray, -50)
img_2 = cv2.subtract(img_gray, 100)
img_3 = cv2.subtract(img_gray, 200)
### 영상 출력
titles = ["Original", "-50", "-100", "-200"]
images = [img_gray, img_1, img_2, img_3]
for i in range(len(images)):
plt.subplot(1, 4, i+1)
plt.imshow(images[i], "gray")
plt.title(titles[i])
plt.axis('off')
plt.show()
### 곱셈
img_1 = cv2.multiply(img_gray, 1.3)
img_2 = cv2.multiply(img_gray, 1.5)
img_3 = cv2.multiply(img_gray, 1.7)
### 영상 출력
titles = ["Original", "*1.3", "*1.5", "*1.7"]
images = [img_gray, img_1, img_2, img_3]
for i in range(len(images)):
plt.subplot(1, 4, i+1)
plt.imshow(images[i], "gray")
plt.title(titles[i])
plt.axis('off')
plt.show()
### 나눗셈
img_1 = cv2.divide(img_gray, 1.3)
img_2 = cv2.divide(img_gray, 1.5)
img_3 = cv2.divide(img_gray, 1.7)
### 영상 출력
titles = ["Original", "/1.3", "/1.5", "/1.7"]
images = [img_gray, img_1, img_2, img_3]
for i in range(len(images)):
plt.subplot(1, 4, i+1)
plt.imshow(images[i], "gray")
plt.title(titles[i])
plt.axis('off')
plt.show()
### 밝게 - alpha = 1, beta = 32
img_1 = cv2.convertScaleAbs(img_gray, alpha = 1, beta = 50)
### 어둡게 - alpha = 1, beta = -32
img_2 = cv2.convertScaleAbs(img_gray, alpha = 1, beta = -50)
### 반전 - alpha = -1, beta = 255
img_3 = cv2.convertScaleAbs(img_gray, alpha = -1, beta = 255)
### 영상 출력
titles = ["Original", "밝게", "어둡게", "반전"]
images = [img_gray, img_1, img_2, img_3]
for i in range(len(images)):
plt.subplot(1, 4, i+1)
plt.imshow(images[i], "gray")
plt.title(titles[i])
plt.axis('off')
plt.show()
### dtype=uint8
a = np.array(110, dtype='uint8')
b = np.array(200, dtype='uint8')
print('{0:08b}'.format(a))
print('{0:08b}'.format(b))
01101110 11001000
### AND
c = np.bitwise_and(a , b)
print('{0:08b}'.format(c))
print(c)
01001000 72
### OR
c = np.bitwise_or(a , b)
print('{0:08b}'.format(c))
print(c)
11101110 238
### XOR
c = np.bitwise_xor(a , b)
print('{0:08b}'.format(c))
print(c)
10100110 166
### NOT
c = np.invert(a)
print('{0:08b}'.format(c))
print(c)
10010001 145
### 영상 읽기
img_gray = cv2.imread(r'D:\image\lena.png', 0)
img_gray.shape
(512, 512)
### Mask
img_mask = np.zeros((512, 512), dtype='uint8')
img_mask[128:385, 128:385] = 255
### 논리연산
img_and = cv2.bitwise_and(img_gray, img_mask)
img_or = cv2.bitwise_or(img_gray, img_mask)
img_xor = cv2.bitwise_xor(img_gray, img_mask)
img_not = cv2.bitwise_not(img_gray)
### 영상 출력
titles = ["원래 영상", "mask", "bitwise_and", "bitwise_or", "bitwise_xor", "bitwise_not"]
images = [img_gray, img_mask, img_and, img_or, img_xor, img_not]
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\lena.png')
img_raw.shape
(512, 512, 3)
### Mask
img_mask = np.zeros((512, 512, 3), dtype='uint8')
img_mask[128:385, 128:385, :] = 255
### 논리연산
img_and = cv2.bitwise_and(img_raw, img_mask)
img_or = cv2.bitwise_or(img_raw, img_mask)
img_xor = cv2.bitwise_xor(img_raw, img_mask)
img_not = cv2.bitwise_not(img_raw)
### 영상 출력
titles = ["원래 영상", "mask", "bitwise_and", "bitwise_or", "bitwise_xor", "bitwise_not"]
images = [img_raw, img_mask, img_and, img_or, img_xor, img_not]
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()