이미지 위에 정보 표시하기

정강민·2022년 1월 3일
0

OpenCV

목록 보기
4/9
post-thumbnail

선 그리기

# 검정 바탕 이미지에 빨간 선분 그리기
start_point, end_point = (0, 0), (img_black.shape[1], img_black.shape[0])
color, line_thickeness, line_type = (0, 0, 255), 1, cv2.LINE_AA
img_black = cv2.line(img_black, start_point, end_point, color, line_thickness, line_type)

cv2.line(image, start_point, end_point, color, line_thickness, line_type)

인자

  • start_point, end_point : 튜플로 구성된 x, y 좌표로 행렬 좌표가 아님
  • color : 선의 색상을 지정. (B, G, R)로 구성되어 있으며 0~ 255 사이 값을 사용
  • line_thickness : 선의 굵기
  • line_type : 선의 종류

원 그리기

# 검정 바탕 이미지 중점에 반지름이 30인 파란 원 그리기
center_point = (img_black.shape[1] // 2, img_black.shape[0] // 2)
radius, color, line_thickeness, line_type = 30, (255, 0, 0), 1, cv2.LINE_AA
img_black = cv2.circle(img_black, center_point, radius, color, line_thickeness, line_type)

cv2.circle(image, center_point, radius, color, line_thickness, line_type)

인자

  • center_point : 원의 중심점
  • radius : 반지름 길이

사각형 그리기

# 검정 바탕 이미지 중점에 20 x 20 초록 사각형 그리기
width, height = 20, 20
top_left = center_point[0] - (width // 2), center_point[1] - (height // 2)
bottom_right = center_point[0] + (width // 2), center_point[1] + (height // 2)
color, line_thickeness, line_type = (0, 255, 0), 1, cv2.LINE_AA
img_black = cv2.rectangle(img_black, top_left, bottom_right, color, line_thickeness, line_type)

cv2.rectangle(image, top_left, bottom_right, color, line_thickness, line_type)

인자

  • top_left : 사각형이 이미지에 위치할 좌상단 모서리 좌표
  • bottom_right: 사각형이 이미지에 위치할 우하단 모서리 좌표

텍스트 쓰기

# 검정 바탕 이미지에 (30, 30)위치에 하얀 글씨 쓰기
text_bottom_left = (30, 30)
font_type, font_scale = cv2.FONT_HERSHEY_COMPLEX, 0.4
color, line_thickeness, line_type = (255, 255, 255), 1, cv2.LINE_AA
img_black = cv2.putText(img_black, "OpenCV", text_bottom_left, font_type, font_scale, color, line_thickeness, line_type)

cv2.putText(image, string, text_bottom_left, font_type, font_scale, color, line_thickeness, line_type)

  • string : 이미지에 쓸 문자열로 영문만 쓸 수 있음
  • text_bottom_left : 문자열이 시작되는 좌측 하단 좌표
profile
DA/DA/AE

0개의 댓글