해당 글은 제로베이스데이터스쿨 학습자료를 참고하여 작성되었습니다
OpenCV 그리기 함수
그리기 함수 사용 시 주의할 점
cv2.line(img, pt1, pt2, color, thickness=1, lineType=cv2.LINE_8, shift=0)
v2.LINE_4
, cv2.LINE_8(Default)
, cv2.LINE_AA
cv2.rectangle(img, pt1, pt2, color, thickness=1, lineType=cv2.LINE_8, shift=0)
cv2.rectangle(img, rec, color, thickness=1, lineType=cv2.LINE_8, shift=0)
v2.LINE_4
, cv2.LINE_8(Default)
, cv2.LINE_AA
cv2.circle(img, center, radius, color, thickness=1, lineType=cv2.LINE_8, shift=0
v2.LINE_4
, cv2.LINE_8(Default)
, cv2.LINE_AA(추천)
cv2.polylines(img, pts, isClosed, color, thickness=1, lineType=cv2.LINE_8, shift=0)
v2.LINE_4
, cv2.LINE_8(Default)
, cv2.LINE_AA
cv2.putText(img, text, org, fontFace, fontScale, color, thickness=1, lineType=cv2.LINE_8, bottomLeftOrigin=False)
import numpy as np
import cv2
img = np.full((400, 400, 3), 255, np.uint8)
#img = cv2.imread('cat.bmp')
cv2.line(img, (50, 50), (200, 50), (0, 0, 255), 5)
cv2.line(img, (50, 60), (150, 160), (0, 0, 128))
cv2.rectangle(img, (50, 200, 150, 100), (0, 255, 0), 2)
cv2.rectangle(img, (70, 220), (180, 280), (0, 128, 0), -1)
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)
text = 'Hello? OpenCV ' + cv2.__version__
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()
cv2.VideoCapture(index, apiPreference=AUTO)
cv2.VideoCapture.open(index, apiPreference=AUTO)
cv2.VideoCapture.isOpened()
cv2.VideoCapture.read(image=one)
import sys
import cv2
# 카메라 열기
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Camera open Failed!")
sys.exit()
else:
print("Camera Opened!")
while True:
ret, frame = cap.read()
if not ret:
break
# 흑백처리
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 1은 좌우반전, 0은 상하반전
color_flip = cv2.flip(frame, 1)
gray_flip = cv2.flip(gray, 1)
cv2.imshow('frame', frame)
cv2.imshow('gray', gray)
cv2.imshow('color_flip', color_flip)
cv2.imshow('gray_flip', gray_flip)
if cv2.waitKey(1) == 27: # ESC
break
cap.release()
cv2.destroyAllWindows()
cv2.VideoCapture(filename, apiPreference=AUTO)
cv2.VideoCapture.open(filename, apiPreference=AUTO)
cv2.VideoCapture.get(propId)
cv2.VideoCapture.set(propId, value)
import sys
import cv2
# 비디오 파일 열기
cap = cv2.VideoCapture('./data/video1.mp4')
if not cap.isOpened():
print("Video open failed!")
sys.exit()
# 비디오 프레임 크기, 전체 프레임수, FPS 등 출력
print('Frame width:', int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
print('Frame height:', int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('Frame count:', int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))
fps = cap.get(cv2.CAP_PROP_FPS)
print('FPS:', fps)
delay = round(1000 / fps)
# 비디오 매 프레임 처리
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
cv2.imshow('gray', gray)
if cv2.waitKey(delay) == 27: # delay 값이 들어가면 정상속도
break
cap.release()
cv2.destroyAllWindows()
---------------------------------------------------------------------
Frame width: 1280
Frame height: 720
Frame count: 85
FPS: 24.0
cv2.VideoWriter(filename, fourcc, fps, frameSize, isColor=None)
cv2.VideoWriter.open(filename, fourcc, fps, frameSize, isColor=None)
cv2.VideoWriter.isOpened()
cv2.VideoWriter.write(image=one)
import sys
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Camera open Failed!")
sys.exit()
w = round(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*"DIVX") # *'DIVX' == 'D', 'I', 'V', 'X'
delay = round(1000 / fps)
out = cv2.VideoWriter('./data/output.avi', fourcc, fps, (w, h))
if not out.isOpened():
print('File open Failed!')
cap.release()
sys.exit()
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 흑백
edge = cv2.Canny(gray, 100, 255) # 외곽선
gray_color = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) # 저장을 위해 BGR로 변환
out.write(gray_color) # 흑백 저장
cv2.imshow('frame', frame)
cv2.imshow('gray', gray)
cv2.imshow('gray_color', gray_color)
cv2.imshow('edge', edge)
if cv2.waitKey(delay) == 27:
break
cap.release()
out.release()
cv2.destroyAllWindows()