LoginSignup
1
3

More than 5 years have passed since last update.

OpenCV等のPythonコードの覚書

Posted at

前提

  • Python3初心者なので、忘れてしまう書き方のメモ
  • できるだけPEP8準拠

標準

無限ループ

python
while True:
  break

if文ノットイコール

python
if not test == 0:
  break

OpenCV

VideoCapture関連

総フレーム数の取得

python
# noinspection PyArgumentList
video = cv2.VideoCapture('test.mp4')
count = video.get(cv2.CAP_PROP_FRAME_COUNT)
print(count)

GUI関連

ウィンドウサイズ可変

python
cv2.namedWindow("Frame", cv2.WINDOW_GUI_EXPANDED | cv2.WINDOW_KEEPRATIO)
cv2.resizeWindow("Frame", 640, 530)

閉じるボタン押下の判定

python
if cv2.getWindowProperty("Test", cv2.WND_PROP_VISIBLE) == 0:
    break

画像関連

create black image

python
import numpy as np
image = np.zeros((480, 640, 3), np.uint8)

参考

https://qiita.com/takahiro_itazuri/items/e740f0a1b5165fafa6ac
https://algorithm.joho.info/programming/python/opencv-videocapture-mp4-movie-py/
https://docs.opencv.org/3.0.0/d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b
https://stackoverflow.com/questions/35003476/opencv-python-how-to-detect-if-a-window-is-closed/37881722#37881722

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3