Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 코딩테스트
- Generative Models
- 과제형 코딩테스트
- colorization
- KT
- kt인적성
- 논문 리뷰
- controlNet
- diffusion models
- 포스코 코딩테스트
- DDPM
- Image Generation
- classifier-free guidance
- dp
- ip-adapter
- stable diffusion
- ddim
- 포스코 채용
- manganinja
- posco 채용
- 프로그래머스
Archives
- Today
- Total
Paul's Grit
[OpenCV] OpenCV로 도형 그리기 본문
OpenCV를 사용하여 도형 그리기¶
In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
1. 검정색 배경 이미지를 그리기¶
- np.zeros()
In [2]:
blank_img = np.zeros((512, 512, 3), dtype=np.int16)
blank_img.shape
Out[2]:
(512, 512, 3)
In [3]:
plt.imshow(blank_img)
Out[3]:
<matplotlib.image.AxesImage at 0x7efefb8d57e0>
2. 사각형 그리기¶
cv2.rectangle(img, start, end, color, thickness, lineType)
- img – 그림을 그릴 이미지
- start – 시작 좌표(ex; (0,0))
- end – 종료 좌표(ex; (500. 500))
- color – BGR형태의 Color(ex; (255, 0, 0) -> Blue)
- thickness (int) – 선의 두께. pixel
- lineType – 선 타입. cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA 중 선택 가능
다음과 같이 (x, y, w, h)를 사용해서 그릴 수도 있다
cv2.rectangle(img, rec, color, thickness, lineType)
- rec: 사각형 위치 정보 (x, y, w, h)
In [4]:
cv2.rectangle(blank_img, pt1=(384, 0), pt2=(510, 150), color=(0, 255,0), thickness=10)
plt.imshow(blank_img);
In [5]:
cv2.rectangle(blank_img, pt1=(200, 200), pt2=(300, 300), color=(0, 0, 255), thickness=20)
plt.imshow(blank_img);
3. 원 그리기¶
cv2.circle(img, center, radian, color, thickness)
- img – 그림을 그릴 이미지
- center – 원의 중심 좌표(x, y)
- radian – 반지름
- color – BGR형태의 Color
- thickness – 선의 두께, -1 이면 원 안쪽을 채움
In [6]:
cv2.circle(blank_img, center=(100, 100), radius=50, color=(255, 0, 0), thickness=8)
plt.imshow(blank_img);
In [7]:
cv2.circle(blank_img, center=(400, 400), radius=50, color=(255, 0, 0), thickness=-1)
plt.imshow(blank_img);
4. 직선¶
cv2.line(img, start, end, color, thickness)
- img – 그림을 그릴 이미지 파일
- start – 시작 좌표(ex; (0,0))
- end – 종료 좌표(ex; (500. 500))
- color – BGR형태의 Color(ex; (255, 0, 0) -> Blue)
- thickness (int) – 선의 두께. pixel
In [8]:
cv2.line(blank_img, pt1=(0, 0), pt2=(512, 512), color=(102, 255, 255), thickness=5)
plt.imshow(blank_img)
Out[8]:
<matplotlib.image.AxesImage at 0x7efef93b9c90>
5. text 넣기¶
cv2.putText(img, text, org, font, fontSacle, color)
- img – image
- text – 표시할 문자열
- org – 문자열이 표시될 위치. 문자열의 bottom-left corner점
- font – font type. CV2.FONT_XXX
- fontSacle – Font Size
- color – fond color
In [9]:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img, text='Hello', org=(10, 500),
fontFace=font, fontScale=4, color= (255,255,255),
thickness=3, lineType=cv2.LINE_AA)
plt.imshow(blank_img)
Out[9]:
<matplotlib.image.AxesImage at 0x7efef94f2cb0>
6. 다각형 그리기¶
cv2.polylines(img, pts, isClosed, color, thickness)
- img – image
- pts (array) – 연결할 꼭지점 좌표
- isClosed – 닫힌 도형 여부
- color – Color
- thickness – 선 두께
In [24]:
blank_img = np.zeros((512, 512, 3), dtype=np.int32)
vertices = np.array([[100, 300],
[200, 200],
[400, 300],
[200, 400]], dtype=np.int32)
vertices
Out[24]:
array([[100, 300], [200, 200], [400, 300], [200, 400]], dtype=int32)
In [25]:
vertices.shape
Out[25]:
(4, 2)
In [26]:
pts= vertices.reshape((-1, 1, 2))
pts.shape
Out[26]:
(4, 1, 2)
In [27]:
cv2.polylines(blank_img, [pts], isClosed=True, color=(255, 0, 0), thickness=5)
plt.imshow(blank_img);
7. 연습¶
In [34]:
# 선 그리기
img = np.full((400, 400, 3), 255, np.uint8)
cv2.line(img, (50, 50), (200, 50), (0, 0, 255), 5)
cv2.line(img, (50, 60), (150, 160), (0, 0, 128))
plt.imshow(img)
Out[34]:
<matplotlib.image.AxesImage at 0x7efef92d1fc0>
In [35]:
# 사각형 그리기
cv2.rectangle(img, (50, 200, 150, 100), (0, 255, 0), 2)
cv2.rectangle(img, (70, 220), (180, 280), (0, 255, 0), -1)
plt.imshow(img);
In [37]:
# 원 & 다각형 그리기
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)
pts = np.array([[250, 200],
[300, 200],
[350, 300],
[250, 300]])
cv2.polylines(img, [pts], True, (255, 0, 255), 2)
plt.imshow(img);