⚙️ 이미지 처리
📌 라이브러리 및 이미지 로드
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
import numpy as np
import cv2
!gdown 1cHcUDb4ziI6UKr4Xq1aLRqS8JCNM-aHf
img_path = "/content/sohn.jpg"
image = cv2.imread(img_path)
imgRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- cv2.imread()를 통해 이미지를 불러오면, 기본적으로 BGR 형태로 가져온다.
- 따라서 BGR2RGB를 사용한 색상 변환을 거쳐야 원본 이미지를 가져온다.
from google.colab.patches import cv2_imshow
img = cv2.imread(img_path)
- from google.colab.patches import cv2_imshow를 사용하면 바로 원본을 가져온다.
📌 이미지 채널 분리 & 병합
# 채널을 각각 분리
r = cv2_imshow(imgRGB[:, :, 0])
g = cv2_imshow(imgRGB[:, :, 1])
b = cv2_imshow(imgRGB[:, :, 2])
# 채널을 자동으로 분리
r, g, b = cv2.split(imgRGB)
- 이미지의 채널을 지정해서 분리해줄 수 있다.
- cv2.split()을 통해 자동으로 분리하는 것도 가능하다.
temp = cv2.merge([b, g, r])
- 분리된 채널들을 cv2.merge()를 통해 원본으로 복구할 수 있다.
📌 이미지 색상 공간 변환
g = cv2.cvtColor(imgRGB, cv2.COLOR_RGB2GRAY)
- cv2.cvtColor()를 통해 이미지를 흑백, 또는 다양한 색상으로 변환할 수 있다.
📌 이미지에 대한 어파인 변환
# 평행 이동
tx = 600
ty = 800
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
translated_image = cv2.warpAffine(imgRGB, translation_matrix, (image.shape[1], image.shape[0]))
# 회전
angle = 45
height, width = imgRGB.shape[:2]
center = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(imgRGB, rotation_matrix, (width, height))
- cv2.warpAffine을 통해 이미지에 대한 평행 이동, 회전 등을 수행할 수 있다.
- 이러한 변환을 '어파인 변환'이라고 한다.
📌 이미지 해상도 조절
scale_width = 0.05
scale_height = 0.05
new_width = int(imgRGB.shape[1] * scale_width)
new_height = int(imgRGB.shape[0] * scale_height)
scaled_image = cv2.resize(imgRGB, (new_width, new_height))
- cv2.resize() 옵션을 통해 이미지 해상도를 조절할 수 있다.
⚙️ 이미지 처리
📌 라이브러리 및 이미지 로드
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
import numpy as np
import cv2
!gdown 1cHcUDb4ziI6UKr4Xq1aLRqS8JCNM-aHf
img_path = "/content/sohn.jpg"
image = cv2.imread(img_path)
imgRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- cv2.imread()를 통해 이미지를 불러오면, 기본적으로 BGR 형태로 가져온다.
- 따라서 BGR2RGB를 사용한 색상 변환을 거쳐야 원본 이미지를 가져온다.
from google.colab.patches import cv2_imshow
img = cv2.imread(img_path)
- from google.colab.patches import cv2_imshow를 사용하면 바로 원본을 가져온다.
📌 이미지 채널 분리 & 병합
# 채널을 각각 분리
r = cv2_imshow(imgRGB[:, :, 0])
g = cv2_imshow(imgRGB[:, :, 1])
b = cv2_imshow(imgRGB[:, :, 2])
# 채널을 자동으로 분리
r, g, b = cv2.split(imgRGB)
- 이미지의 채널을 지정해서 분리해줄 수 있다.
- cv2.split()을 통해 자동으로 분리하는 것도 가능하다.
temp = cv2.merge([b, g, r])
- 분리된 채널들을 cv2.merge()를 통해 원본으로 복구할 수 있다.
📌 이미지 색상 공간 변환
g = cv2.cvtColor(imgRGB, cv2.COLOR_RGB2GRAY)
- cv2.cvtColor()를 통해 이미지를 흑백, 또는 다양한 색상으로 변환할 수 있다.
📌 이미지에 대한 어파인 변환
# 평행 이동
tx = 600
ty = 800
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
translated_image = cv2.warpAffine(imgRGB, translation_matrix, (image.shape[1], image.shape[0]))
# 회전
angle = 45
height, width = imgRGB.shape[:2]
center = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(imgRGB, rotation_matrix, (width, height))
- cv2.warpAffine을 통해 이미지에 대한 평행 이동, 회전 등을 수행할 수 있다.
- 이러한 변환을 '어파인 변환'이라고 한다.
📌 이미지 해상도 조절
scale_width = 0.05
scale_height = 0.05
new_width = int(imgRGB.shape[1] * scale_width)
new_height = int(imgRGB.shape[0] * scale_height)
scaled_image = cv2.resize(imgRGB, (new_width, new_height))
- cv2.resize() 옵션을 통해 이미지 해상도를 조절할 수 있다.