import cv2
import numpy as np
# 세로 480 크기 가로 640, 3채널(BGR)에 해당하는 스케치북 만들기
img = np.zeros((480,640,3), dtype=np.uint8)
img[:] = (255,255,255) #전체 공간을 흰색으로 채움
#print(img)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
img = np.zeros((480,640,3), dtype=np.uint8)
img[100:200, 200:300] = (255,255,255) #세로로 100에서 200까지, 가로로 200에서 300까지 흰색
#[세로영역:세로영역, 가로영역:가로영역]
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
직선
- 직선의 종류(line.type)
- 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)
COLOR2 = (255,0,0)
COLOR3 = (0,0,255)
THICKNESS = 3 #두께
THICKNESS2 = 8 #두께
THICKNESS3 = 13 #두께
cv2.line(img, (50,100),(400,50),COLOR, THICKNESS, cv2.LINE_8)
cv2.line(img, (300,10),(200,50),COLOR2, THICKNESS2, cv2.LINE_4)
cv2.line(img, (250,300),(40,120),COLOR3, THICKNESS3, cv2.LINE_AA)
#cv2.line(그릴 위치, 시작점, 끝점, 색, 두께, 선 종류)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
img = np.zeros((480,640,3),dtype=np.uint8)
COLOR = (0,255,255)
RADIUS = 50
THICKNESS = 9 #두께
cv2.circle(img, (200,100),RADIUS, COLOR, THICKNESS, cv2.LINE_8)# 속이 비어있는 원
cv2.circle(img, (350,100),RADIUS, COLOR, cv2.FILLED, cv2.LINE_8)# 속이 차있는 원
#cv2.circle(그릴 위치, 시작점, 반지름, 색, 두께, 선 종류)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
img = np.zeros((480,640,3),dtype=np.uint8)
COLOR = (0,255,255)
THICKNESS = 4 #두께
cv2.rectangle(img, (200,100),(30,300), COLOR, THICKNESS)# 속 빈 네모
cv2.rectangle(img, (300,200),(60,400), COLOR, cv2.FILLED)# 속 찬 네모
#cv2.rectangle(그릴 위치, 시작점, 끝점, 색, 두께)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
img = np.zeros((480,640,3),dtype=np.uint8)
COLOR = (0,255,255)
THICKNESS = 4 #두께
pts1 = np.array([[100,100],[200,100],[300,300]])
pts2 = np.array([[200,100],[300,100],[300,200]])
#cv2.polylines(img,[pts1], False, COLOR, THICKNESS, cv2.LINE_AA)
#cv2.polylines(img,[pts1,pts2], True, COLOR, THICKNESS, cv2.LINE_AA)
#cv2.polylines(그릴 위치, 그려질 좌표, 닫힘여부, 색, 두께, 선 종류) 속 빈 다각형
#그려질 좌표는 리스트 형태로 받음
#True 는 닫음(선이 완성이 안되어있으면 이어서 닫아서 완성시킴)
#False 는 열음 닫음 행동을 하지 않음
pts3 = np.array([[[100,100],[200,100],[300,200]],[[300,200],[100,100],[200,300]]])
cv2.fillPoly(img, pts3, COLOR, cv2.LINE_AA)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
opencv에서 사용하는 글꼴 종류
- cv2.FONT_HERSHEY_SIMPLEX : 보통 크기의 산 세리프 글꼴
- cv2.FONT_HERSHEY_PLAIN : 작은 크기의 산 세리프 글꼴
- cv2.FONT_HERSHEY_SCRIPT_SIMPLEX : 필기체 스타일 글꼴
- cv2.FONT_HERSHEY_TRIPLEX : 보통 크기의 세리프 글꼴
- cv2.FONT_ITALIC : 기울임(이탈릭체)/이탈릭체는 다른 글꼴이랑 함께 직성해야함
import cv2
import numpy as np
img = np.zeros((480,640,3),dtype=np.uint8)
COLOR = (255,255,255) #색
THICKNESS = 1 #두께
SCALE = 1 #크기
cv2.putText(img,"learning opencv python",(20,50),cv2.FONT_HERSHEY_SIMPLEX, SCALE,COLOR,THICKNESS)
#cv2.putText(그릴 배경 위치, 텍스트, 좌표, 글꼴, 크기, 색, 두께)
cv2.putText(img,"learning opencv python",(20,100),cv2.FONT_HERSHEY_PLAIN, SCALE,COLOR,THICKNESS)
cv2.putText(img,"learning opencv python",(20,150),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, SCALE,COLOR,THICKNESS)
cv2.putText(img,"learning opencv python",(20,200),cv2.FONT_HERSHEY_TRIPLEX, SCALE,COLOR,THICKNESS)
cv2.putText(img,"learning opencv python",(20,250),cv2.FONT_HERSHEY_TRIPLEX|cv2.FONT_ITALIC, SCALE,COLOR,THICKNESS)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
PIL(Python Image Library)
from PIL import ImageFont, ImageDraw, Image
#한글을 사용하기 위한 우회 방법을 사용하기 위한 라이브러리
import cv2
import numpy as np
def myText(src, text, pos, font_size, font_color):
img_pil = Image.fromarray(src)
draw = ImageDraw.Draw(img_pil)
font = ImageFont.truetype('fonts/gulim.ttc',font_size)
draw.text(pos, text, font=font, fill=font_color)
return np.array(img_pil)
img = np.zeros((480,640,3),dtype=np.uint8)
COLOR = (255,255,255) #색
THICKNESS = 1 #두께
SCALE = 30 #크기
img = myText(img,"김민석 영상처리",(20,50), SCALE,COLOR)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()