numpy array에서 image 겹치기 및 에러 해결
검은색 배경에 이미지를 겹치기 위해 np.zero를 통해 검은색 배경을 만들어준다
# 세로가 더 길 경우
if img.shape[0] > img.shape[1]:
# numpy로 검은색 배경을 생성
background = np.zeros((img.shape[0],img.shape[0], 3),dtype=np.uint8)
# 이미지를 가운데로 겹치도록 설정
x_offset = y_offset = img.shape[0] // 2 - (img.shape[1] // 2)
# 해당 위치에 이미지를 겹침
background[:, x_offset:x_offset + img.shape[1]] = img
else:
# 가로가 더 길 경우
background = np.zeros((img.shape[1], img.shape[1], 3),dtype=np.uint8)
x_offset = y_offset = img.shape[1] // 2 - (img.shape[0] // 2)
background[y_offset:y_offset + img.shape[0],:] = img
dtype을 uint8로 설정했는데 uint32로 할 경우 cv2.write을 할 때
img data type = 6 is not supported
라는 에러가 뜨기 때문에 uint6으로 해야한다
또한 float32나 int32로 설정하면 이미지를 겹칠 때
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers
라는 에러가 뜨면서 이미지 값들이 모두 1로 변환되서 합쳐진다