SAM2로 segmentation를 해보고 있었다.
기존에는 RGB, 3채널 이미지를 다루고 있었는데
흑백 이미지로도 진행해보고 싶어서 바꾸어보았는데 문제가 발생했다.
기존 코드
from PIL import Image
image = Image.open("image/path.jpg")
image = image.convert("RGB") # 생략가능
image = np.array(image)
masks = mask_generator.generate(image)
바꾼 부분
image.convert("RGB") -> image.convert("L")
에러 메세지
흑백 이미지를 처리하려니 채널이 맞지 않아 아래와 같은 오류가 출력되었다.
IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
shape 확인
입력되어야 할 이미지 shape: [3, 1024, 1024]
흑백으로 변환된 이미지 shape: [1, 1024, 1024]
문제 해결
이미지 크기와 흑백 상태는 그대로 유지한 채
채널만 3으로 늘려준다.
image = Image.open('image/path.jpg')
image = image.convert("L")
image = np.array(image)
image = np.stack([image] * 3, axis=-1)
masks = mask_generator.generate(image)
'공부 > Python' 카테고리의 다른 글
[torchvision] 분류 모델 종류/파라미터 수 - 가벼운 모델을 찾아보자! (0) | 2025.03.04 |
---|---|
[Python] 파이썬 import 한 라이브러리/모듈/패키지 경로 확인하는 법 (0) | 2024.11.15 |
[Python] requests 사용 POST json 포맷 데이터 전송하기 (+ Status Code 400 409 오류 해결) (1) | 2024.10.31 |
[Python] virtualenv 가상환경 설치/실행/삭제 (0) | 2024.10.23 |
[Python] TypeError: 'numpy._DTypeMeta' object is not subscriptable 해결 방법 (0) | 2023.10.17 |