import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
print(img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
[[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
...
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]]

img[:] = (255,255,255)
[[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
...
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]
[[255 255 255]
[255 255 255]
[255 255 255]
...
[255 255 255]
[255 255 255]
[255 255 255]]]

영역 색칠
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
img[100:200, 200:300] = (255,255,255)
print(img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

직선
cv2.LINE_4
: 상하좌우 4방향으로 연결된 선
cv2.LINE_8
: 대각선을 포함한 8방향으로 연결된 선(기본값)
cv2.LINE_AA
: 부드러운 선(anti-aliasing)
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
COLOR = (0,255,255)
THICKNESS = 3
cv2.line(img, (50,100),(400,50), COLOR, THICKNESS, cv2.LINE_8)
cv2.line(img, (50,200),(400,150), COLOR, THICKNESS, cv2.LINE_4)
cv2.line(img, (50,300),(400,250), COLOR, THICKNESS, cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

원
cv2.circle(img, (300,200),RADIUS, COLOR, THICKNESS, cv2.LINE_AA)
:
그릴 위치, 원의 중심점, 반지름, 색, 두께, 선 종류
- 원을 채울 경우
THICKNESS
부분을 cv2.FILLED
로 바꿈.
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
COLOR = (255,255,0)
RADIUS = 50
THICKNESS = 10
cv2.circle(img, (300,200),RADIUS, COLOR, THICKNESS, cv2.LINE_AA)
cv2.circle(img, (500,200),RADIUS, COLOR, cv2.FILLED, cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

사각형
cv2.rectangle(img, (100,100),(200,200), COLOR, THICKNESS)
: 속이 빈 사각형
- 원과 마찬가지로 속을 채우고 싶다면
THICKNESS
부분을 cv2.FILLED
로 바꿈.
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
COLOR = (0,255,0)
THICKNESS = 3
cv2.rectangle(img, (100,100),(200,200), COLOR, THICKNESS)
cv2.rectangle(img, (300,100),(400,200), COLOR, cv2.FILLED)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

다각형
cv2.polylines(img, [pts1], True, COLOR, THICKNESS, cv2.LINE_AA)
: 그릴 위치, 그릴 좌표들, 닫힘 여부, 색, 두께, 선 종류
cv2.fillPoly(img, pts3, COLOR, cv2.LINE_AA)
: 채우기
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
COLOR = (0,0,255)
THICKNESS = 3
pts1 = np.array([[100,100],[200,100],[100,200]])
cv2.polylines(img, [pts1], True, COLOR, THICKNESS, cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
- 닫힘 여부가 True일 때

- 닫힘 여부가 False일 때

다각형 두 개 이어서
pts1 = np.array([[100,100],[200,100],[100,200]])
pts2 = np.array([[200,100],[300,100],[300,200]])
cv2.polylines(img, [pts1,pts2], True, COLOR, THICKNESS, cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

- 속 채운 다각형
pts3=np.array([[[100,300],[200,300],[100,400]],[[200,300],[300,300],[300,400]]])
cv2.fillPoly(img, pts3, COLOR, cv2.LINE_AA)
