Python opencv 설치와 이미지 출력 및 카메라 영상 캡쳐 예제 코드

◼ python환경 opencv 설치

anaconda

conda install -c conda-forge opencv

https://anaconda.org/conda-forge/opencv


pip

pip install opencv-python


설치 버전 확인

import cv2 as cv
print(cv.__version__)

4.1.0


◼ opencv 동작 예제 코드

OpenCV-Python Tutorials 페이지에 python에서 opencv를 사용하는 예제 코드들이 수록되어 있다.

위 예제 중 간단한 몇 가지를 실행해 봤다.


이미지 디스플레이 예제

참조 : https://docs.opencv.org/master/db/deb/tutorial_display_image.html


import cv2 as cv

img = cv.imread('14793554227.jpg')

if img is not None:
    cv.imshow('display', img)

cv.waitKey(0)

cv.destroyAllWindows()



카메라 영상 캡쳐 예제

참조 : https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html


import cv2 as cv

cap = cv.VideoCapture(0)

while cap.isOpened():
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    # Display the resulting frame
    cv.imshow('video', frame)
    
    if cv.waitKey(1) == ord('q'):
        break
    
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()


cv.VideoCapture의 파라메터 '0'은 디폴트 카메라 디바이스를 의미하며 0대신 동영상 파일 위치를 입력하면 동영상 파일의 비디오를 캡처하여 화면에 출력할 수 있다.

댓글

이 블로그의 인기 게시물

간단한 cfar 알고리즘에 대해

windows에서 간단하게 크롬캐스트(Chromecast)를 통해 윈도우 화면 미러링 방법

간단한 칼만 필터(Kalman Filter) 소스 코드와 사용 예제

안드로이드(android) 전체 화면 시계 앱(clock app) 예제 코드

mkfs.fat Device or resource busy 에러 해결법