
μμ νννκΈ° μν λ€μν λ°©λ²μ μλ―Έ, μ¬λ¬ λ°©λ²μ΄ μμΌλ©° κ°κ° λͺ©μ μ λ°λΌ μ€κ³λμ΄ κ³ μ ν νΉμ±μ κ°μ§λ€.
RGBλ λμ§νΈ μ΄λ―Έμ§μμ κ°μ₯ λ리 μ¬μ©λλ μ 곡κ°μ λλ€.

HSVλ μμμ μ’ λ₯, κ°λ, λ°κΈ°λ₯Ό λΆλ¦¬νμ¬ νννλ μ 곡κ°μ λλ€.

LAB μ 곡κ°μ μΈκ°μ μκ° μ²΄κ³λ₯Ό λ°μνμ¬ μ€κ³λμμ΅λλ€.

Grayscaleμ νλ°± μ΄λ―Έμ§λ₯Ό νννκΈ° μν μ 곡κ°μ λλ€.

λ°μ΄ν°μ λΆν¬λ₯Ό κ·Έλν ννλ‘ ννν κ²μΌλ‘, μ΄λ―Έμ§ μ²λ¦¬μμμ νμ€ν κ·Έλ¨μ μ£Όλ‘ μ΄λ―Έμ§μ λ°κΈ° κ°μ λνλ΄λ©°, κ° λ°κΈ° κ°μ ν΄λΉνλ ν½μ μ μλ₯Ό μΈλ‘μΆμΌλ‘ νμνλ€.
μ΄λ―Έμ§μ λͺ μ λΆν¬λ₯Ό μ½κ² νμ ν μ μμ
μ΄λ―Έμ§μ μ λ°μ μΈ λλΉμ λ°κΈ° μμ€μ νκ°νκ±°λ μ‘°μ νλλ° λμμ μ€

μΆ : μ΄λ―Έμ§μ λ°κΈ°(0~255)
μΆ : λ±μ₯ λΉλ
ν½μ μ κ°μ 0λΆν° 255κΉμ§μ λμ μΉκ° μ 체 μμμμ κ³ λ₯΄κ² λΆν¬λλλ‘ λ§λλ κΈ°λ²μΌλ‘ μ΄λ―Έμ§κ° μ 체μ μΌλ‘ μ΄λ‘κ±°λ κ³ λ£¨ λ°μ νΉμ§μ΄ λμ λμ§ μκ±°λ λΆλ¦¬κ° μ΄λ €μΈ λ μμ£Ό μ°μ λλ€.
μ μ΄λ―Έμ§λ₯Ό 보μλ©΄ λͺ¨μμ κ·Έλ¦Όμμ μ¬μ± νΌλΆμμ λλΉκ° μ»€μ§ κ²μ νμΈ ν μ μμ΅λλ€.
μ¦, μ΄λμ΄ κ²μ λμ± μ΄λ‘κ², λ°μ κ²μ λμ± λ°μμ‘μ΅λλ€. μ΄λ μλ³Έ μ΄λ―Έμ§μ λΆν¬κ° μ€κ°λ°κΈ°μ λ°μ§λμκΈ° λλ¬Έμ
λλ€.
νμ€ν κ·Έλ¨ λ§€μΉμ ν μ΄λ―Έμ§μ νμ€ν κ·Έλ¨μ λ€λ₯Έ μ΄λ―Έμ§μ νμ€ν κ·Έλ¨κ³Ό μ μ¬νκ² μ‘°μ νλ κ³Όμ μ λλ€. λ€μν νκ²½μμ 촬μλ μ΄λ―Έμ§μ λ°κΈ° λ° λλΉλ₯Ό μΌκ΄λκ² λ§λλλ° μ¬μ©ν μ μμ΅λλ€.
μ΄λ―Έμ§ λ³νμ μ΄λ―Έμ§μ ν½μ μμΉλ κ°μ λ³κ²½νμ¬ μλ³Έ μ΄λ―Έμ§λ₯Ό λ€λ₯Έ ννλ λͺ¨μμΌλ‘ λ³ννλ κ²μ μλ―Έν©λλ€. κΈ°ννμ ꡬ쑰λ₯Ό λ³κ²½νκ±°λ μ΄λ―Έμ§μ λ°κΈ°, μμμ μ‘°μ ν μ μμΌλ©° μ΄λ―Έμ§ νΈμ§, κ°μ , λΆμ λ± λ€μν μ²λ¦¬ μμ μ μ¬μ©λ©λλ€.
κΈ°ννμ λ³νμ μ΄λ―Έμ§μ λͺ¨μμ΄λ μμΉλ₯Ό λ³ννλ κ²μ λλ€.
π» μμ
import cv2
import numpy as np
import matplotlib.pyplot as plt
# ν¬μΈνΈμ μμ κ·Έλ €μ£Όλ ν¨μ
def draw_points(image, points, color, border_color, size=3):
for point in points:
center = (int(point[0]), int(point[1]))
cv2.circle(image, center, size, border_color, -1)
cv2.circle(image, center, size-1, color, -1)
# μ΄λ―Έμ§ λΆλ¬μ€κΈ°
img = cv2.imread('lenna.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# μν λ³ν
rows, cols, ch = img.shape
src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
dst_points = np.float32([[cols * 0.1, rows * 0.1], [cols * 0.9, rows * 0.2], [cols * 0.1, rows * 0.9]])
affine_matrix = cv2.getAffineTransform(src_points, dst_points)
affine_transformed = cv2.warpAffine(img, affine_matrix, (cols, rows))
# μλ³Έ μ΄λ―Έμ§μ μν λ³νλ μ΄λ―Έμ§μ ν¬μΈνΈ 그리기
draw_points(img, src_points, (255, 255, 255), (0, 0, 0))
draw_points(affine_transformed, dst_points, (255, 255, 255), (0, 0, 0))
# κ²°κ³Ό μ΄λ―Έμ§ νμ
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(img)
axes[0].set_title("Original Image")
axes[1].imshow(affine_transformed)
axes[1].set_title("Affine Transformed")
plt.tight_layout()
plt.show()

π» μμ
# μ΄λ―Έμ§ λΆλ¬μ€κΈ°
img = cv2.imread('lenna.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rows, cols, _ = img.shape
# μκ·Ό λ³ν
src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1], [cols - 1, rows - 1]])
dst_points = np.float32([[cols * 0.3, rows * 0.1], [cols * 0.7, rows * 0.1], [cols * 0.1, rows * 0.9], [cols * 0.9, rows * 0.9]])
perspective_matrix = cv2.getPerspectiveTransform(src_points, dst_points)
perspective_transformed = cv2.warpPerspective(img, perspective_matrix, (cols, rows))
# μλ³Έ μ΄λ―Έμ§μ μν λ³νλ μ΄λ―Έμ§μ ν¬μΈνΈ 그리기
draw_points(img, src_points, (255, 255, 255), (0, 0, 0))
draw_points(perspective_transformed, dst_points, (255, 255, 255), (0, 0, 0))
# κ²°κ³Ό μ΄λ―Έμ§ νμ
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(img)
axes[0].set_title("Original Image")
axes[1].imshow(perspective_transformed)
axes[1].set_title("Perspective Transformed")
plt.tight_layout()
plt.show()

κ°λ λ³νμ μ΄λ―Έμ§μ ν½μ κ° μ체λ₯Ό λ³ν νλ κ²μ λλ€.
π» λΌμ΄λΈλ¬λ¦¬ λ° μ΄λ―Έμ§ νΈμΆ
import cv2
import numpy as np
import matplotlib.pyplot as plt
# μ΄λ―Έμ§ λΆλ¬μ€κΈ°
image = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
π» νμ€ν κ·Έλ¨ Plot
# νμ€ν κ·Έλ¨ κ³μ°
hist = cv2.calcHist([image], [0], None, [256], [0,256])
plt.figure()
plt.title("Lenna Image Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()

π» νμ€ν κ·Έλ¨ ννν
# μ΄λ―Έμ§ λΆλ¬μ€κΈ°
image = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
# νμ€ν κ·Έλ¨ ννν
equalized_image = cv2.equalizeHist(image)
fig, axes = plt.subplots(1, 2, figsize=(5, 10))
axes[0].imshow(image, cmap='gray')
axes[1].imshow(equalized_image, cmap='gray')
plt.show()

π» νμ€ν κ·Έλ¨ λ§€μΉ
def histogram_matching(source, reference):
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True, return_counts=True)
r_values, r_counts = np.unique(reference, return_counts=True)
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
r_quantiles = np.cumsum(r_counts).astype(np.float64)
r_quantiles /= r_quantiles[-1]
interp_r_values = np.interp(s_quantiles, r_quantiles, r_values)
matched = interp_r_values[bin_idx].reshape(source.shape)
return matched
# μλ³Έ μ΄λ―Έμ§μ μ°Έμ‘° μ΄λ―Έμ§ λΆλ¬μ€κΈ°
source = cv2.imread('lenna.jpg', cv2.IMREAD_GRAYSCALE)
reference = cv2.imread('ref.jpg', cv2.IMREAD_GRAYSCALE)
matched_image = histogram_matching(source, reference)
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
# Images
axes[0, 0].imshow(source, cmap='gray')
axes[0, 0].set_title("Source Image")
axes[0, 1].imshow(reference, cmap='gray')
axes[0, 1].set_title("Reference Image")
axes[0, 2].imshow(matched_image, cmap='gray')
axes[0, 2].set_title("Matched Image")
# Histograms
axes[1, 0].hist(source.ravel(), 256, [0,256], color='black')
axes[1, 0].set_title("Source Histogram")
axes[1, 1].hist(reference.ravel(), 256, [0,256], color='black')
axes[1, 1].set_title("Reference Histogram")
axes[1, 2].hist(matched_image.ravel(), 256, [0,256], color='black')
axes[1, 2].set_title("Matched Histogram")
plt.tight_layout()
plt.show()
