cv2 bbox 그리기
2023. 11. 16. 01:15ㆍpython
OpenCV (cv2)를 사용하여 이미지에 bounding box (bbox)를 그리고 저장하는 Python 코드:
import cv2
def draw_bbox(image_path, bbox, output_path):
# 이미지를 읽어옵니다.
image = cv2.imread(image_path)
# bounding box 좌표를 추출합니다.
x, y, w, h = bbox
# bounding box를 그립니다.
color = (0, 255, 0) # 초록색
thickness = 2
cv2.rectangle(image, (x, y), (x + w, y + h), color, thickness)
# bounding box가 그려진 이미지를 저장합니다.
cv2.imwrite(output_path, image)
if __name__ == "__main__":
# 이미지 파일 경로
input_image_path = "input_image.jpg"
# bounding box 좌표 (x, y, width, height)
bounding_box = (100, 50, 200, 150)
# bounding box가 그려진 이미지를 저장할 경로
output_image_path = "output_image_with_bbox.jpg"
# 함수 호출
draw_bbox(input_image_path, bounding_box, output_image_path)
위 코드에서 input_image_path
는 원본 이미지 파일 경로, bounding_box
는 bounding box의 좌표를 나타내며 (x, y, width, height) 형식입니다. output_image_path
는 bounding box가 그려진 이미지를 저장할 파일 경로입니다.
이 코드는 초록색으로 bounding box를 그립니다. 필요에 따라 색상을 변경하거나 두께 등을 조절할 수 있습니다. 코드를 수정하여 원하는 bounding box 스타일로 그릴 수 있습니다.
'python' 카테고리의 다른 글
다각형 겹친 부분의 넓이 구하기 (Python w/ shapely) (0) | 2024.01.08 |
---|---|
헝가리안 알고리즘 (0) | 2023.11.17 |
python opencv 이미지 합치기 (1) | 2023.11.01 |
Python import 위치 알기 (0) | 2023.10.10 |
python 데이터 저장 읽기 / pickle / 리스트, 딕셔너리 저장 (0) | 2023.08.10 |