OpenCV Read image or video

Ref: https://docs.opencv.org/master/

Read image

image = cv.imread(fileName, flag=cv.IMREAD_COLOR)
  • @brief ์ด๋ฏธ์ง€ ์ฝ๊ธฐ
  • @param fileName: string ํŒŒ์ผ ๊ฒฝ๋กœ
  • @param flag
    • cv.IMREAD_COLOR : BGR
    • cv.IMREAD_GRAYSCALE
    • cv.IMREAD_UNCHANGED
  • @return numpy.ndarray (y์ถ•, x์ถ•, pixel ์›์†Œ ์ˆ˜) 3 ์ฐจ์› ๋ฐฐ์—ด
cv.imshow(title, image)
key_value = cv.waitKey(delay=0) & 0xFF
cv.destroyWindows(title)
cv.destroyAllWindows()
  • @brief ์ด๋ฏธ์ง€ ์ฐฝ์— ํ‘œ์‹œ, title์„ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•œ ํ›„, image๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋™์˜์ƒ์ฒ˜๋Ÿผ ๋ณด์ž„
  • @param title: string
  • @param image: numpy.ndarray ์ด๋ฏธ์ง€ 3 ์ฐจ์› ๋ฐฐ์—ด
cv.imwrite(fileName, image)
  • @brief ์ด๋ฏธ์ง€ ์ €์žฅ
  • @param fileName: string ํŒŒ์ผ ๊ฒฝ๋กœ
  • @param image: numpy.ndarray ์ด๋ฏธ์ง€ 3 ์ฐจ์› ๋ฐฐ์—ด

Read video, cam

cv.VideoCapture

class cv.VideoCapture(index|filename, apiPreference=cv.CAP_ANY)
  • @brief VideoCapture ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
  • @param index|filename: ์บ ์˜ ์ธ๋ฑ์Šค ๋˜๋Š” ๋™์˜์ƒ ํŒŒ์ผ ๊ฒฝ๋กœ
  • @param apiPreference: ์˜์ƒ์„ ์ฒ˜๋ฆฌํ•  api ์„ ํƒ
  • @return cv.VideoCapture
info

Linux(debian)์—์„œ ๋น„๋””์˜ค์˜ ์ธ๋ฑ์Šค๋Š” /dev/video ๋’ค์˜ ์ˆซ์ž์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์บ  ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(/dev/video[์ธ๋ฑ์Šค]) v4l2-ctl --all --list-formats-ext -d /dev/video0

is_success = cv.VideoCapture.isOpened()
  • @brief ์บ  ๋˜๋Š” ํŒŒ์ผ์ด ์ •์ƒ์ ์œผ๋กœ ์—ด๋ ธ๋Š”์ง€ ํ™•์ธ
value = cv.VideoCapture.get(propid)
is_success = cv.VideoCapture.set(propid, value)
is_success, image = cv.VideoCapture.read()
  • @brief ๋‹ค์Œ ํ”„๋ ˆ์ž„์„ ์ฝ์–ด ์ด๋ฏธ์ง€๋กœ ๋ฐ˜ํ™˜
  • @return image (BGR)
cv.VideoCapture.release()
  • @brief ํŒŒ์ผ ๋˜๋Š” ์บ  ๋‹ซ๊ธฐ

cv.VideoWriter

class cv.VideoWriter(filename, apiPreference=cv.CAP_ANY, fourcc, fps, frameSize, isColor=True)
  • @brief VideoWriter ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
  • @param apiPreference: ์˜์ƒ์„ ์ฒ˜๋ฆฌํ•  api ์„ ํƒ
  • @param fourcc: 4-character code of codec, cv.VideoWriter_fourcc()๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ •
  • @param fps
  • @param frameSize: tuple (๊ฐ€๋กœ, ์„ธ๋กœ)
  • @param isColor
  • @return cv.VideoWriter
fourcc_code = cv.VideoWriter_fourcc(c1, c2, c3, c4)
is_success = cv.VideoWriter.isOpened()
value = cv.VideoWriter.get(propid)
is_success = cv.VideoWriter.set(propid, value)
cv.VideoWriter.release()
  • @brief cv.VideoCapture์™€ ๋™์ผ
cv.VideoWriter.write(image)
  • @brief ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์ถ”๊ฐ€

Examples

C++ compilation

g++ [input.cpp] -o [output] $(pkg-config opencv4 --libs --cflags)

Read image

import cv2 as cv
# image.jpg ํŒŒ์ผ์„ ์ฝ๊ณ , img์— ํ• ๋‹น.
img = cv.imread("image.jpg")
# image ์ฐฝ์— img ํ‘œ์‹œ.
cv.imshow("image", img)
# ํ‚ค ์ž…๋ ฅ์ด ์žˆ์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ.
cv.waitKey()
cv.destroyAllWindows()

Read video

import cv2 as cv
# video.mp4 ํŒŒ์ผ ์—ด๊ธฐ.
cap = cv.VideoCapture("video.mp4")
# frame per second ๊ฐ’ ์–ป๊ธฐ.
fps = cap.get(cv.CAP_PROP_FPS)
if cap.isOpened():
while True:
# ํ”„๋ ˆ์ž„ ์ฝ๊ณ , frame ๋ณ€์ˆ˜์— ํ• ๋‹น.
is_success, frame = cap.read()
if not is_success:
break
# video ์ฐฝ์— frame ํ‘œ์‹œ.
cv.imshow("video", frame)
if cv.waitKey(int(1000 / fps)) & 0xFF != 255:
"""
1000/fps ms ๋™์•ˆ ํ‚ค ์ž…๋ ฅ ๊ธฐ๋‹ค๋ฆผ.
ํ‚ค ์ž…๋ ฅ์ด ์žˆ์œผ๋ฉด ๋ฃจํ”„ ์ข…๋ฃŒ.
"""
break
cap.release()
cv.destroyAllWindows()

Read cam

import cv2 as cv
OUTPUT_VIDEO_WIDTH = 640
OUTPUT_VIDEO_HEIGHT = 480
# 0 ๋ฒˆ ์บ  (/dev/video0) ์—ด๊ธฐ.
cap = cv.VideoCapture(0, cv.CAP_V4L2)
# ํ”ฝ์…€ ๋‹จ์œ„๋กœ ๊ฐ€๋กœ, ์„ธ๋กœ ์„ค์ •.
cap.set(cv.CAP_PROP_FRAME_WIDTH, OUTPUT_VIDEO_WIDTH)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, OUTPUT_VIDEO_HEIGHT)
# ํ”ฝ์…€ ํฌ๋งท ์„ค์ •.
cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*"MJPG"))
"""
๋…นํ™” ์„ค์ •.
record.avi๋กœ ์ €์žฅ, FFmpeg ์‚ฌ์šฉ.
"""
fourcc = cv.VideoWriter_fourcc(*"FMP4")
out = cv.VideoWriter(
"record.avi", fourcc, 25.0, (OUTPUT_VIDEO_WIDTH, OUTPUT_VIDEO_HEIGHT)
)
if cap.isOpened() and out.isOpened():
while True:
try:
# ํ”„๋ ˆ์ž„ ์ฝ๊ณ , frame ๋ณ€์ˆ˜์— ํ• ๋‹น.
is_success, frame = cap.read()
except cv.error as e:
continue
if not is_success:
break
# record.avi์— frame ์“ฐ๊ธฐ.
out.write(frame)
# video ์ฐฝ์— frame ํ‘œ์‹œ.
cv.imshow("video", frame)
if cv.waitKey(1) & 0xFF != 255:
"""
1 ms ๋™์•ˆ ํ‚ค ์ž…๋ ฅ ๊ธฐ๋‹ค๋ฆผ.
ํ‚ค ์ž…๋ ฅ์ด ์žˆ์œผ๋ฉด ๋ฃจํ”„ ์ข…๋ฃŒ.
"""
break
cap.release()
out.release()
cv.destroyAllWindows()
Last updated on