본문 바로가기

인공지능/Python

gray scale image to RGB image (using opencv)

Problem

image에서 egde를 추출할 일이 있었는데, edge는 gray scale이라 1channel만 가지고 있었다. 원본 image에 edge를 추가할 목적이라 1channel의 edge를 3channel로 맞춰주는 작업이 필요했다.

 

Solution

# original image read
img = cv2.imread('/path/to/image.jpg')
img.shape
# (680,720,3)

# Canny Edge Detection
edges = cv2.Canny(image=img, threshold1=50, threshold2=200) 
edges.shape 
# (680,720)

# 1channel image to 3channel image
img2 = cv2.merge((edges,edges,edges))
img2.shape
# (680,720,3)

cv2.merge를 이용해서 간단하게 스택을 쌓을 수 있다.