본문 바로가기

Back/Python

[Python] OpenCV

0.설치 

www.lfd.uci.edu/~gohlke/pythonlibs/

 

Python Extension Packages for Windows - Christoph Gohlke

by Christoph Gohlke, Laboratory for Fluorescence Dynamics, University of California, Irvine. Updated on 3 January 2021 at 07:17 UTC. This page provides 32- and 64-bit Windows binaries of many scientific open-source extension packages for the official CPyth

www.lfd.uci.edu

위에서 OpenCV.whl찾아서 pip 설치

 

1. 이미지 읽기, 저장

import cv2
import numpy as np

def img_show():
    file = 'img/cute-cat.jpg'
    img = cv2.imread(file, cv2.IMREAD_COLOR)
    #흑백으로
    #img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
    #알파채널 값 유지
    #img = cv2.imread(file, cv2.IMREAD_UNCHANGED)


    # 크기 조절 가능한 윈도우 설정
    cv2.namedWindow('cute-cat', cv2.WINDOW_NORMAL)
    #화면에 이미지 표시, 1번쨰는 윈도우, 2번째는 이미지
    cv2.imshow('cute-cat',img)

    #입력 전까지 대기, 누르는 버튼 값 리턴
    s=cv2.waitKey(0)
    if s == 27:
        #모든 창 닫기(esc)
        cv2.destroyAllWindows()

    elif s == ord('c'):
        cv2.imwrite('img/copy.jpg',img)
        cv2.destroyAllWindows()

 

2.웹캠으로 부터 이미지 읽기

def video_show():
    try:
        print("카메라 작동")
        capture = cv2.VideoCapture(0)
    except:
        print("작동실패")
        return

    capture.set(3, 480)
    capture.set(4, 320)

    while True:
        ret, frame = capture.read()

        if not ret:
            print("error")
            break

        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        cv2.imshow('video',gray)

        s = cv2.waitKey(0)
        if s == 27:
            break

    capture.release()
    cv2.destroyAllWindows()

비디오속성 설정

docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get

 

Reading and Writing Images and Video — OpenCV 2.4.13.7 documentation

propId – Property identifier. It can be one of the following: CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next. CV_CAP_PROP_P

docs.opencv.org

 

3. 비디오 저장

def write_video():
    try:
        print("카메라 작동")
        capture = cv2.VideoCapture(0)
    except:
        print("작동실패")
        return

    fps = 20.0
    width = int(capture.get(3))
    height = int(capture.get(4))
    fcc = cv2.VideoWriter_fourcc('D','I','V','X')

    out = cv2.VideoWriter('cam.avi',fcc, fps,(width,height))
    print('start')

    while True:
        ret, frame = capture.read()

        if not ret:
            print("error")
            break

        cv2.imshow('video', frame)
        out.write(frame)

        s = cv2.waitKey(1)
        if s == 27:
            break

    capture.release()
    out.release()
    cv2.destroyAllWindows()

 

4.도형그리기 

def drawing():
    img = np.zeros((512,512,3), np.uint8)

    cv2.line(img, (0,0), (511,511), (100,100,100), 5)
    cv2.rectangle(img, (384,0), (510, 150), (255,0,0), 3)
    cv2.circle(img, (255,255), 100, (0, 255, 0), -1)
    cv2.ellipse(img,(200,200), (100,50), 0,0,180,(0,0,255),-1)

    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, 'test', (10,500), font, 4, (255,255,255),2)

    cv2.imshow('drawing', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

drawing()

각도형의 속성은 IDE를 통해 확인.

 

5. 이미지 리사이징, 회전, 변환

blog.naver.com/samsjang/220504966397

 

[11편] 이미지 변환 - 리사이징, 이동, 회전, 원근효과

이미지 프로세싱 & 컴퓨터 비전 OpenCV-Python 강좌 11편 : 이미지 변환 - 리사이징, 이동, 회전, ...

blog.naver.com

 

6. 가늘게, 굵게, 훼손 보정

blog.naver.com/samsjang/220505815055

 

[13편] 이미지 Erosion과 Dilation

이미지 프로세싱 & 컴퓨터 비전OpenCV-Python 강좌 13편 : 이미지 형태 변환(Morphological Tran...

blog.naver.com