Python/OpenCV

그림 그리기

ddingz 2022. 2. 16. 15:02

직선 그리기

  • cv2.line(img, start, end, color [, thickness, lineType]) : 직선 그리기
    • img : 그림 그릴 대상 이미지, NumPy 배열
    • start : 선 시작 지점 좌표(x, y)
    • end : 선 끝 지점 좌표(x, y)
    • color : 선 색상, (Blue, Green, Red), 0~255
    • thickness = 1 : 선 두께
    • lineType : 선 그리기 형식
      • cv2.LINE_4 : 4 연결 선 알고리즘
      • cv2.LINE_8 : 8 연결 선 알고리즘
      • cv2.LINE_AA : 안티에일리어싱(antialiasing, 계단 현상 없는 선)

사각형 그리기

  • cv2.rectangle(img, start, end, color [, thickness, lineType]) : 사각형 그리기
    • img : 그림 그릴 대상 이미지, NumPy 배열
    • start : 사각형 시작 꼭짓점(x, y)
    • end : 사각형 끝 꼭짓점(x, y)
    • color : 색상(Blue, Green, Red)
    • thickness : 선 두께
      • -1 : 채우기
    • lineType : 선 타입, cv2.line()과 동일

다각형 그리기

  • cv2.polylines(img, points, isClosed, color [, thickness, lineType]) : 다각형 그리기
    • img : 그림 그릴 대상 이미지
    • points : 꼭짓점 좌표, NumPy 배열 리스트
    • isClosed : 닫힌 도형 여부, True/False
    • color : 색상(Blue, Green, Red)
    • thickness : 선 두께
    • lineType : 선 타입, cv2.line()과 동일

원, 타원, 호 그리기

  • cv2.circle(img, center, radius, color [, thickness, lineType]) : 원 그리기 함수
    • img : 그림 대상 이미지
    • center : 원점 좌표(x, y)
    • radius : 원의 반지름
    • color : 색상(Blue, Green, Red)
    • thickness : 선 두께(-1 : 채우기)
    • lineType : 선 타입, cv2.line()과 동일
  • cv2.ellipse(img, center, axes, angle, from, to, color [, thickness, lineType]) : 호나 타원 그리기 함수
    • img : 그림 대상 이미지
    • center : 원점 좌표(x, y)
    • axes : 기준 축 길이
    • angle : 기준 축 회전 각도
    • from, to : 호를 그릴 시작 각도와 끝 각도

글씨 그리기

  • cv2.putText(img, text, point, fontFace, fontSize, color [, thickness, lineType])
    • img : 글씨를 표시할 이미지
    • text : 표시할 문자열
    • point : 글씨를 표시할 좌표(좌측 하단 기준)(x, y)
    • fontFace : 글꼴
      • cv2.FONT_HERSHEY_PLAIN : 산세리프체 작은 글꼴
      • cv2.FONT_HERSHEY_SIMPLEX : 산세리프체 일반 글꼴
      • cv2.FONT_HERSHEY_DUPLEX : 산세리프체 진한 글꼴
      • cv2.FONT_HERSHEY_COMPLEX_SMALL : 세리프체 작은 글꼴
      • cv2.FONT_HERSHEY_COMPLEX : 세리프체 일반 글꼴
      • cv2.FONT_HERSHEY_TRIPLEX : 세리프체 진한 글꼴
      • cv2.FONT_HERSHEY_SCRIPT_SIMPLEX : 필기체 산세리프 글꼴
      • cv2.FONT_HERSHEY_SCRIPT_COMPLEX : 필기체 세리프 글꼴
      • cv2.FONT_ITALIC : 이탤릭체 플래그
    • fontSize : 글꼴 크기
    • color, thickness, lineType : cv2.retangle()과 동일