在 Python (Mac) 中使用 OpenCV 裁剪视频

Cropping video with OpenCV in Python (Mac)

总的来说,我希望使用 OpenCV 访问网络摄像头,裁剪和调整它们的图像属性(饱和度、对比度、亮度),然后将结果作为新流输出。

我对 python/opencv 知之甚少,正在尽我所能将我能找到的内容拼凑起来。

我已经能够访问 mjpeg 流,但是我发现的每种裁剪方式似乎都失败了。下面的代码似乎是最有前途的,但我愿意接受其他方法。

我在使用 Max MSP 和 Siphon 后取得了我想要的结果,但是我希望使用 OpenCV 我能够使它完全基于 Web 并可访问。

我希望避免将流拆分成单独的 jpeg,但如果这是实现我所追求目标的唯一方法,请告诉我。

非常感谢任何和所有指导。


import cv2
import numpy as np

cap = cv2.VideoCapture('http://89.29.108.38:80/mjpg/video.mjpg')

(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 20)
roi = frame[y:y+h, x:x+w]

while True:
  ret, frame = cap.read()
  cv2.imshow('Video', frame)

  if cv2.waitKey(1) == 27:
    exit(0)

Traceback (most recent call last):
  File "mjpeg-crop.py", line 6, in <module>
    (x, y, w, h) = cv2.boundingRect(c)
NameError: name 'c' is not defined

评论太多了,但试试这个开始:

import cv2
import numpy as np

cap = cv2.VideoCapture('http://89.29.108.38:80/mjpg/video.mjpg')

# (x, y, w, h) = cv2.boundingRect(c)
# cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 20)
# roi = frame[y:y+h, x:x+w]

while True:
    ret, frame = cap.read()
    # (height, width) = frame.shape[:2]
    sky = frame[0:100, 0:200]
    cv2.imshow('Video', sky)

    if cv2.waitKey(1) == 27:
        exit(0)