글모음

OpenCV - python 튜토리얼 공부 정리 (1) 본문

프로그래밍 공부/OpenCV

OpenCV - python 튜토리얼 공부 정리 (1)

Nova_61 2021. 4. 17. 23:34
728x90
반응형

프로젝트를 하는데 OpenCV가 필요해서 개발을 위해 정리하는 중

freeCodeCamp.org 라는 유투브 계정의 튜토리얼 영상을 보면서 공부중이다.

3시간 41분짜리 영상이고 한글자막은 없지만 영어가 그렇게 어렵지는 않다.

 

Github 라이브러리는 여기

github.com/jasmcaus/opencv-course

 

jasmcaus/opencv-course

Learn OpenCV in 4 Hours - Code used in my Python and OpenCV course on freeCodeCamp. - jasmcaus/opencv-course

github.com

[ 목차 ]

 

1. read.py ( 사진, 영상 불러오기 )

폴더에 있는 사진을 불러오고, 웹캠을 통해 실시간으로 영상 받아오기

# read.py
import cv2 as cv

# 사진 읽기
img = cv.imread('photos/cat.png')
cv.imshow('Cat',img)
cv.waitKey(0)

# 동영상 불러오기(웹캠)
capture = cv.VideoCapture(0)

# Frame을 만들고 무한 반복문을 돌려서 웹캠을 통해 영상 프레임을 실시간으로 받아온다
while True:
    isTrue, frame = capture.read()
    cv.imshow('Video', frame)

    # d 누르면 영상 종료
    if cv.waitKey(20) & 0xFF==ord('d'):
        break
capture.release()
cv.destroyAllWindows()
cv.waitKey(0)

* 사진 읽을 때는 중요한 점이 사진의 height와 width가 너무 크면 잘리기 때문에 resize 과정이 필요하다.

 

2. rescale.py

Frmae resizing과 Frame Resolution 변경하는 코드

import cv2 as cv

# Frame 크기 변경(resizing)
# Default scale은 75% (0.75 = 75%), '.2'는 2%와 같다.
# rescaleFrame 함수는 image, Live video, video 모두에서 사용가능
def rescaleFrame(frame, scale = 0.75): 
    width = int(frame.shape[1] * scale) 
    height = int(frame.shape[0] * scale)
    
    dimensions = (width, height) # tuple형식으로 저장

    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)

# 변환 전 이미지 프레임
img = cv.imread('Photos/cat.png')
cv.imshow('Cat', img)

# 변환 후 이미지 프레임
resized_image = rescaleFrame(img)
cv.imshow('Image', resized_image)

# 비디오 resolution 변경
# 실시간 영상에서만 동작하고 image와 그냥 video에서는 안된다고 한다.
# 밝기도 변경가능

def changeRes(width, height):
    capture.set(3, width)
    capture.set(4, height)

capture = cv.VideoCapture(0)

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

    # Frame 크기 변경
    # scale 변수 추가해주면 rescaleFrame에서 변경된 frame에서 추가로 변경된다.
    frame_resized = rescaleFrame(frame) 
    # frame_resized = rescaleFrame(frame, scale=.2) 

    cv.imshow('Video', frame)
    cv.imshow('Video_resiezed', frame_resized)

    if cv.waitKey(20) & 0xFF == ord('d'):
        break
capture.release()
cv.destoryAllWindows()

 

3. 도형 그리기

draw.py

 

4. OpenCV에서 많이 쓰이는 Basic function

 

import cv2 as cv

img = cv.imread('Photos/city.png')
cv.imshow('city', img)

# convert RGB image to gray image
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)

# Blur
# 1. GaussianBlur
blur = cv.GaussianBlur(img, (7,7), cv.BORDER_DEFAULT)
cv.imshow('Blur',blur)

# Edge Cascade
canny = cv.Canny(img, 125, 175)
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)

# Dilating the image
# apply canny image
dilated = cv.dilate(canny, (3,3), iterations=2)
cv.imshow('Dilated', dilated)

# Eroding
# apply dilated image
eroded = cv.erode(dilated, (3,3), iterations=2)
cv.imshow('Eroded', eroded)

# image Resize
resized = cv.resize(img, (300, 400))
cv.imshow('Resided', resized)

# Cropping
cropped = img[100:300, 200:300]
cv.imshow('cropped', cropped)

cv.waitKey(0)

 

(1) 이미지 회색으로 converting

 

(2) Blur - 이미지 흐리게 하기

 

 

 

(3) Canny, Dilated

Dilate는 Canny화 된 이미지를 사용한다.

Canny에 비해 Dilated를 시키면 선이 더 굵어진다.

iterations의 수를 더 올리면 더 굵어진다.

 

 

crop - 이미지 슬라이싱

 

 

이미지 shifting, 회전, Flipping

 

Transformations : 

 

728x90
반응형
Comments