OpenCV-Python

OpenCV Python 선, 도형, 문자열 출력

rkftks22 2021. 8. 24. 12:01

직선 : cv2.line(img, pt1, pt2, color [, thickness [, lineType [, shift]]])

img : 그림 그릴 영상

pt1, pt2 : 직선의 시작점, 직선의 끝점 (영상 벗어난 좌표도 괜찮습니다.)

color : 선 색상 또는 밝기(B, G, R) 튜플 또는 정수 값

thickness : 선 두께(기본 값 : 1)

lineType : 선 타입(기본 값 : cv2.LINE_8)

  • cv2.LINE_4
  • cv2.LINE_8
  • cv2.LINE_AA : 부드러움

shift : 그리기 좌표 값 축소비율(기본 값 : 0)


사각형 : cv2.rectangle(img, pt1, pt2, color [, thickness [, lineType [, shift]]])

            cv2.rectangle(img, rec, color [, thickness [, lineType [, shift]]])

pt1, pt2 : 사각형 두 꼭짓점 좌표(x1, y1) 튜플

rec : 사각형 위치 정보, (x, y, w, h) 튜플

thickness : 선 두께(기본 값 : 1), 음수(-1) 지정 시 채우기 효과


원 : cv2.circle(img, center, radius, color [, thickness [, lineType [, shift]]])

center : 원의 중심점 좌표(x1, y1) 튜플

radius : 지름


다각형 : cv.polylines(img, pts, isClosed, color [, thickness [, lineType [, shift]]])

pts : 다각형 외곽점들의 좌표 배열(numpy.ndarray 리스트)

isClosed : 폐곡선 여부(Trus, Flase)


텍스트 : cv2.putText(img, text, org, fontFace, fontScale, color [, thickness [, lineType [, bottomLeftOrigin]]])

text : 출력할 텍스트 문자열

org : 영상에 출력할 텍스트 문자열의 왼쪽 아래 모서리 좌표

fontFace : 글꼴 유형

FontScale : 글꼴 크기(배율)

bottomLeftOrigin : True일 시 데이터 원점이 왼쪽 하단 모서리, 아니면 왼쪽 상단 모서리


import cv2
import numpy as np

img = np.full((500, 500, 3), 255, np.uint8)

# 선
cv2.line(img, (60, 50), (170, 50), (0, 0, 255), 5)

cv2.line(img, (10, 90), (210, 130), (0, 0, 128), 5)

cv2.line(img, (10, 130), (210, 170), (255, 0, 0), 5, cv2.LINE_AA)

# 사각형
cv2.rectangle(img, (150, 250), (200, 300), (0, 255, 0), 3)

cv2.rectangle(img, (50, 300, 50, 50), (0, 255, 0), 3)

cv2.rectangle(img, (350, 100, 30, 50), (0, 255, 0), -1)

# 원
cv2.circle(img, (300, 100), 30, (100, 100, 255), -1, cv2.LINE_AA)

cv2.circle(img, (300, 100), 60, (255, 100, 255), 3, cv2.LINE_AA)

# 다각형
pts = np.array([[200, 200], [220, 300], [350, 350], [390, 340]])
cv2.polylines(img, [pts], True, (0, 0, 0), 2)

# 텍스트
cv2.putText(img, "drawing test", (50, 400), cv2.FONT_HERSHEY_SIMPLEX,
            2, (0, 0, 255), 2, cv2.LINE_AA)


cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()

 

출력 결과