[computer-vision] 크로마 키 합성
특정 색상만 추출
image = cv.imread('balloon.webp')
HSV에서 H가 40-80 사이, SV는 50-255 사이인 부분만 추출
lower = np.array([40, 50, 50])
upper = np.array([80, 255, 255])
mask = cv.inRange(hsv, lower, upper)
Image.fromarray(mask)
원래 이미지에서 mask 부분만 보기
cv.bitwise_and(image, image, mask=mask)
크로마 키를 이용한 합성
foreground = cv.imread('chroma-key.jpg')
background = cv.imread('desert.jpg')
# 배경 크기 리사이즈
height, width = foreground.shape[:2]
background = cv.resize(background, (width, height))
# 인물 이미지를 HSV로 변환
hsv = cv.cvtColor(foreground, cv.COLOR_BGR2HSV)
크로마 키 색 범위
lower_green = np.array([40, 100, 100])
upper_green = np.array([60, 255, 255])
마스크 만들기
mask = cv.inRange(hsv, lower_green, upper_green)
mask_inv = cv.bitwise_not(mask)
전경에서 인물만 뽑기
fg_isolated = cv.bitwise_and(foreground, foreground, mask=mask_inv)
배경에서 인물만큼 잘라내기
bg_isolated = cv.bitwise_and(background, background, mask=mask)
합치기
final_output = cv.add(fg_isolated, bg_isolated)