Language - Python(Opencv)

Python Opencv - #6 Draw 함수

KimTory 2021. 11. 21. 15:33

▶ Opencv 뿐만 아니라 다양한 Machine Vision 라이브러리에서 Labeling 할 때, 기본적인 개념을

   이 포스팅에서 알아두면 좋을 거 같습니다.

→ Opencv 에서 Draw 함수 사용 시, 주의해야 할 점은 gray 영상에선 사용이 안되며 cvtColor 함수로

    Color 영상으로 이미지 변환 후, 그리기 함수를 호출해야 됨

 

■ Draw 함수 종류들

  1. line : cv2.line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None) -> img
  2. Rectangle : cv2.rectangle(img, rec, color, thickness=None, lineType=None, shift=None) -> img
  3. Circle : cv2.circle(img, center, radius, color, thickness=None, lineType=None, shift=None) -> img
  4. Polylines : cv2.polyines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None) -> img (polygon)

 

-. C# OpencvShape 글 참고

C# OpencvSharp - Blob Labeling Example (tistory.com)


import numpy as np
import cv2

# full((x_size, y_size), channel, dtype)
img = np.full((400, 400, 3), 255, np.uint8)

# line
cv2.line(img, (50, 50), (200, 50), (0, 0, 255), 5)
cv2.line(img, (50, 60), (150, 160), (0, 0, 128))

# rectangle
cv2.rectangle(img, (50, 200, 150, 100), (0, 255, 0), 2)
cv2.rectangle(img, (70, 220), (180, 280), (0, 128, 0), -1)

# circle
cv2.circle(img, (300, 100), 30, (255, 255, 0), -1, cv2.LINE_AA)
cv2.circle(img, (300, 100), 60, (255, 0, 0), 3, cv2.LINE_AA)

# polylines
pts = np.array([[250, 200], [300, 200], [350, 300], [250, 300]])
cv2.polylines(img, [pts], True, (255, 0, 255), 2)

text = 'Machine Python Opencv '
# image 내, test 삽입
cv2.putText(img, text, (50, 350), cv2.FONT_HERSHEY_SIMPLEX, 0.8, 
            (0, 0, 255), 1, cv2.LINE_AA)

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

 

 

Result Image

 

▶ 이후 Machine Vision 또는 ML/DL 시, 라벨링에 필수임 (object detection, 측정 data 등...)