본문 바로가기
Python/Open CV

[Open CV] 직선 및 사각형 그리기

by IT두잇 2022. 5. 23.

1) cv 라이브러리 import

import numpy as np
import cv2

 

2) 기본 변수 정의 (색깔, 이미지크기, 좌표값 설정)

blue , green, red = (255,0,0), (0,255,0), (0,0,255)
white, black = (255,255,255), (0,0,0)
image = np.full((300,500,3), white, np.uint8)

center = (image.shape[1]//2, image.shape[0]//2) ## 중심좌표 구하기
pt1, pt2 = (300,50), (100,220)
shade = (pt2[0] +2, pt2[1] +2) #그림자 좌표

 

3) 원 그리기

: 3번째 원에는 색깔 채우기 추가 (-1로 채우기 설정)

cv2.circle(image, center, 100, blue)
cv2.circle(image, pt1, 50, green, 2)
cv2.circle(image, pt2, 70, red, -1) #원 내부 채움

 

4) 텍스트 옵션 넣기

font = cv2.FONT_HERSHEY_COMPLEX

cv2.putText(image, 'center_blue', center, font, 1.0, blue)
cv2.putText(image, 'pt1_green', pt1, font, 0.8, green)
cv2.putText(image, 'pt2_red', shade, font, 1.2, black, 2) #그림자 효과
cv2.putText(image, 'pt2_red', pt2, font, 1.2, red, 1)

 

5) imshow를 통해 윈도우 생성

cv2.imshow('Draw circles', image)
cv2.waitKey(0)

 

출력내용:

 

 

 

'Python > Open CV' 카테고리의 다른 글

[Open CV] 글자 쓰기  (0) 2022.05.18