### 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_lena = cv2.imread(r'D:\image\lena.png')
img_lena.shape
(512, 512, 3)
### Grayscale
img_lena_gray = cv2.cvtColor(img_lena, cv2.COLOR_BGR2GRAY)
img_lena_gray.shape
(512, 512)
### 히스토그램 - grayscale
plt.hist(img_lena_gray.flatten(), 256, [0,256])
plt.show()
### 히스토그램 - grayscale
img_hist = cv2.calcHist([img_lena_gray], [0], None, [256], [0, 256])
plt.plot(img_hist)
plt.show()
### 히스토그램 - color
bgr = cv2.split(img_lena)
colors = ['b', 'g', 'r']
for ch, col in zip(bgr, colors):
img_hist = cv2.calcHist([ch], [0], None, [256], [0, 256])
plt.plot(img_hist, color=col)
plt.show()
### 히스토그램 스트레칭 - grayscale
pixel_min = np.min(img_lena_gray)
pixel_max = np.max(img_lena_gray)
img_hist_stretching = np.array((img_lena_gray - pixel_min) /
(pixel_max - pixel_min) * 255).astype('uint8')
### 히스토그램 출력
plt.subplot(2, 1 , 1), plt.hist(img_lena_gray.flatten(), 256, [0,256])
plt.subplot(2, 1 , 2), plt.hist(img_hist_stretching.flatten(), 256, [0,256])
plt.show()
### 영상 출력
titles = ["원래 영상", "히스토그램 스트레칭"]
images = [img_lena_gray, img_hist_stretching]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, 2, i+1), plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### Histograms Equalization in OpenCV
img_hist_equal = cv2.equalizeHist(img_lena_gray)
### 히스토그램 출력
plt.subplot(2, 1 , 1), plt.hist(img_lena_gray.flatten(), 256, [0,256])
plt.subplot(2, 1 , 2), plt.hist(img_hist_equal.flatten(), 256, [0,256])
plt.show()
### 영상 출력
titles = ["원래 영상", "히스토그램 평활화"]
images = [img_lena_gray, img_hist_equal]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, 2, i+1), plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 영상 읽기
img_lena = cv2.imread(r'D:\image\lena.png')
img_lena.shape
(512, 512, 3)
### BGR → YCrCb 변환
img_lena_ycc = cv2.cvtColor(img_lena, cv2.COLOR_BGR2YCrCb)
### Histograms Equalization in OpenCV
img_lena_ycc[:,:,0] = cv2.equalizeHist(img_lena_ycc[:,:,0])
### YCrCb → BGR 변환
img_bgr_he = cv2.cvtColor(img_lena_ycc, cv2.COLOR_YCrCb2BGR)
### RGB 영상으로 출력
titles = ["원래 영상", "히스토그램 평활화"]
images = [img_lena, img_bgr_he]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1,2,i+1),plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
개념
과정
### Package install
#!pip install skimage
### Package
from skimage import exposure
### 영상 읽기 - lena.png
img_lena_gray = cv2.imread(r'D:\image\lena.png', 0)
img_lena_gray.shape
(512, 512)
### 영상 읽기 - airplane.png
img_air_gray = cv2.imread(r'D:\image\airplane.png', 0)
img_air_gray.shape
(512, 512)
### Histogram matching with OpenCV, scikit-image
img_hist_match = exposure.match_histograms(img_lena_gray, img_air_gray).astype('uint8')
### 히스토그램 출력
plt.subplot(3, 1 , 1), plt.hist(img_lena_gray.flatten(), 256, [0,256])
plt.subplot(3, 1 , 2), plt.hist(img_air_gray.flatten(), 256, [0,256])
plt.subplot(3, 1 , 3), plt.hist(img_hist_match.flatten(), 256, [0,256])
plt.show()
### 영상 출력
titles = ["lena", "airplane", "Histogram Matching"]
images = [img_lena_gray, img_air_gray, img_hist_match]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, 3, i+1), plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 영상 읽기
img_lena = cv2.imread(r'D:\image\lena_color.png')
img_air = cv2.imread(r'D:\image\airplane.png')
### Histogram matching with OpenCV, scikit-image
img_hist_match = exposure.match_histograms(img_lena, img_air).astype('uint8')
### 영상 출력
titles = ["lena", "airplane", "Histogram Matching"]
images = [img_lena, img_air, img_hist_match]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, 3, i+1), plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()
### 영상 읽기
img_ES1 = cv2.imread(r'D:\image\Empire_State_1.jpg')
img_ES2 = cv2.imread(r'D:\image\Empire_State_3.jpg')
### Histogram matching with OpenCV, scikit-image
img_hist_match = exposure.match_histograms(img_ES1, img_ES2).astype('uint8')
### 영상 출력
titles = ["Empire_State_1", "Empire_State_2", "Histogram Matching"]
images = [img_ES1, img_ES2, img_hist_match]
for i in range(len(images)):
img_rgb = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, 3, i+1), plt.imshow(img_rgb)
plt.title(titles[i])
plt.axis('off')
plt.show()