import cv2
img = cv2.imread("./egg/1.png",
cv2.IMREAD_COLOR)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
import matplotlib.pyplot as plt
img = cv2.imread("./egg/21.png",
cv2.IMREAD_GRAYSCALE)
plt.imshow(img,cmap='gray')
plt.show
만약에 컬러로 변경하려면 IMREAD_GRAYSCALE를 COLOR_BGR2RGB로 변경한다.동영상 로딩 시키기(예외 처리를 위해 try~except문 사용)
try :
print("영상로딩을 시작합니다..")
video_cap = cv2.VideoCapture("./jung.mp4")
except :
print("영상로딩 실패")
영상 frame 읽어오기
ret, frame = video_cap.read()
if ret :
cv2.imshow("video",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
video_cap.release()
video_cap.release() : 비디오 객체를 메모리에서 해제시키는 기능
프레임 단위로 영상을 출력해야되기 때문에 while문을 사용한다.
try :
print("영상로딩을 시작합니다..")
video_cap = cv2.VideoCapture("./jung.mp4")
while True :
ret, frame = video_cap.read()
if ret :
gray_frame = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
cv2.imshow("video",gray_frame)
k = cv2.waitKey(51)#초당 프레임수를 맞추기 위함 : 1000/24
else :
break;
if k == 27: #27번은 ecs
break;
cv2.destroyAllWindows()
video_cap.release()
except :
print("영상로딩 실패")