OpenCV_1

주제무·2022년 6월 10일
0

openCV

목록 보기
1/2

OpenCV

import cv2 as cv
import numpy as np

Video

VideoCapture Class

path_video = 'rsc/video.mp4'
capture = cv.VideoCapture(path_video)

path_video can be following values.

  1. name of video file (eg. video.avi)
  2. image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
  3. URL of video stream (eg. protocol://host:port/script_name?script_params|auth)
  4. GStreamer pipeline string in gst-launch tool format in case if GStreamer is used as backend Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.

VideoCapture.read()

# retval is boolean, image is a frame of video
retval, image = capture.read()

The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call.

cv.resize()

# dsize = (width, height)
# fx, fy = dst image's scale of src.size
# interpolation has default; INTER_LINEAR
image = cv.resize(src, dsize, dst, fx, fy, interpolation)

Resizes an image.

To shrink an image, it will generally look best with INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with INTER_CUBIC (slow) or INTER_LINEAR (faster but still looks OK).

cv.imshow()

# window's name; winname = 'Video'
cv.imshow('Video', image)

Displays an image in the specified window.

cv.namedWindow()

# flags has deflaut; WINDOW_AUTOSIZE
cv.namedWindow(winname, flags)

Creates a window.

cv.WindowFlags

WINDOW_NORMAL

the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.

WINDOW_AUTOSIZE

the user cannot resize the window, the size is constrainted by the image displayed.

Composite Result

import cv2 as cv
import numpy as np


# Define function but it is nonessential
def rescale_frame(frame, scale=0.75):
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimension = width, height

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


# reading dog video
video_path = 'opencv-course/Resources/Videos/dog.mp4'
capture = cv.VideoCapture(video_path)
ret, image = capture.read()

while ret:
    ret, image = capture.read()
    cv.imshow('Video', rescale_frame(frame))

    # keyboard binding; to break out of playing, press 'd'
    if cv.waitKey(20) & 0xFF == ord('d'):
        break

capture.release()
cv.destroyAllWindows()

Reference

https://www.geeksforgeeks.org/python-opencv-namedwindow-function/#:~:text=Python%20OpenCV%20namedWindow()%20method,it%20to%20fit%20our%20screen.

https://docs.opencv.org/4.x/

https://youtu.be/oXlwWbU8l2o

0개의 댓글