2021년도 1학기 기계학습기반 영상처리 9주차 출석용 과제물입니다.
Hisgogram Equalization을 활용한 color balance를 구현한 코드입니다.

코드

"""
제   목 : 기계학습기반영상처리 9주차 출석용 과제물
기   능 : Histogram Equalization 을 활용한 color balance 구현
파일이름 : 기계학습기반영상처리_9주차출석용과제물_201622821_김영진
수정날짜 : 2021-05-19
작 성 자 :소프트웨어미디어융합전공 4학년 201622821 김영진
"""
import numpy as np, cv2
## 이미지를 COLOR로 불러온다.
image = cv2.imread("C:/Users/Yeongjin/Desktop/Practice/pythonProject/chap06/images/gomduri.png", cv2.IMREAD_COLOR)
if image is None: raise Exception("영상 파일 읽기 오류 발생")
## B, G, R 로 split
B, G, R = cv2.split(image)

zeros = np.zeros((image.shape[0], image.shape[1]), dtype="uint8")
B = cv2.merge([B, zeros, zeros])
G = cv2.merge([zeros, G, zeros])
R = cv2.merge([zeros, zeros, R])
## B, G, R 을 images에 저장
cv2.imwrite('images/R.png', R)
cv2.imwrite('images/B.png', B)
cv2.imwrite('images/G.png', G)
## B, G, R, Color 출력
cv2.imshow("RGB",image)
cv2.imshow("B", B)
cv2.imshow("G", G)
cv2.imshow("R", R)

bins, ranges = [256], [0, 256]
################### R #######################
R_image = cv2.imread("images/R.png", cv2.IMREAD_GRAYSCALE) # 영상 읽기
if R_image is None: raise Exception("영상 파일 읽기 오류")
R_hist = cv2.calcHist([R_image], [0], None, bins, ranges)    # 히스토그램 계산
R_accum_hist = np.zeros(R_hist.shape[:2], np.float32)
R_accum_hist[0] = R_hist[0]
for i in range(1, R_hist.shape[0]):
    R_accum_hist[i] = R_accum_hist[i - 1] + R_hist[i]
R_accum_hist = (R_accum_hist / sum(R_hist)) * 255                 # 누적합의 정규화
R_dst1 = [[R_accum_hist[val] for val in row] for row in R_image] # 화소값 할당
R_dst1 = np.array(R_dst1, np.uint8)
R_dst2 = cv2.equalizeHist(R_image)                # OpenCV 히스토그램 평활화
R_hist1 = cv2.calcHist([R_dst1], [0], None, bins, ranges)   # 히스토그램 계산
R_hist2 = cv2.calcHist([R_dst2], [0], None, bins, ranges)   # 히스토그램 계산

################### G #######################
G_image = cv2.imread("images/G.png", cv2.IMREAD_GRAYSCALE)
if G_image is None: raise Exception("영상 파일 읽기 오류")
G_hist = cv2.calcHist([G_image], [0], None, bins, ranges)
G_accum_hist = np.zeros(G_hist.shape[:2], np.float32)
G_accum_hist[0] = G_hist[0]
for i in range(1, G_hist.shape[0]):
    G_accum_hist[i] = G_accum_hist[i - 1] + G_hist[i]
G_accum_hist = (G_accum_hist / sum(G_hist)) * 255
G_dst1 = [[G_accum_hist[val] for val in row] for row in G_image]
G_dst1 = np.array(G_dst1, np.uint8)
G_dst2 = cv2.equalizeHist(G_image)
G_hist1 = cv2.calcHist([G_dst1], [0], None, bins, ranges)
G_hist2 = cv2.calcHist([G_dst2], [0], None, bins, ranges)

################### B #######################
B_image = cv2.imread("images/B.png", cv2.IMREAD_GRAYSCALE)
if B_image is None: raise Exception("영상 파일 읽기 오류")
B_hist = cv2.calcHist([G_image], [0], None, bins, ranges)
B_accum_hist = np.zeros(B_hist.shape[:2], np.float32)
B_accum_hist[0] = B_hist[0]
for i in range(1, B_hist.shape[0]):
    B_accum_hist[i] = B_accum_hist[i - 1] + B_hist[i]
B_accum_hist = (B_accum_hist / sum(B_hist)) * 255
B_dst1 = [[B_accum_hist[val] for val in row] for row in B_image]
B_dst1 = np.array(B_dst1, np.uint8)
B_dst2 = cv2.equalizeHist(B_image)
B_hist1 = cv2.calcHist([B_dst1], [0], None, bins, ranges)
B_hist2 = cv2.calcHist([B_dst2], [0], None, bins, ranges)

## 출력
cv2.imshow("R_image", R_image)
cv2.imshow("R_dst1_User", R_dst1)
cv2.imshow("R_dst2_OpenCV", R_dst2)

cv2.imshow("G_image", G_image)
cv2.imshow("G_dst1_User", G_dst1)
cv2.imshow("G_dst2_OpenCV", G_dst2)

cv2.imshow("B_image", B_image)
cv2.imshow("B_dst1_User", B_dst1)
cv2.imshow("B_dst2_OpenCV", B_dst2)

cv2.waitKey(0)
cv2.destroyAllWindows()

실행결과

스크린샷(151)

댓글남기기