3

As a whole I am hoping to access ip camera using OpenCV, crop and adjust their image properties (saturation, contrast, brightness) and then output the result as a new stream.

I have very little knowledge of python/opencv and am doing my best to piece this together from what I can find.

I have been able to access the mjpeg stream however every way of cropping I have found seems to fail. The code below seems the most promising but I am open to alternative methods.

I have achieved the result i'm after using Max MSP and Syphon however my hope is that using OpenCV i will be able to make this completely web based and accessible.

I am hoping to avoid splitting the stream into individual jpegs but if that is the only way to achieve what i'm after please let me know.

Any and all guidance is greatly appreciated.


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
Michael Sandford
  • 35
  • 1
  • 1
  • 5
  • Your code seems bound to fail since you process the image and discard the result **after** displaying it. How were you hoping to crop it exactly? – Mark Setchell Aug 05 '19 at 08:32
  • Apologies i am showing my ignorance, I have changed the code though I could still be completely off the mark. I am hoping to crop a rectangular frame from any given stream (specifically the sky). – Michael Sandford Aug 05 '19 at 09:05
  • Do you mean you want to extract and use the sky part, or you want to crop off and discard the sky part? Do you have/know the exact coordinates you want or are you planning to detect the sky dynamically? – Mark Setchell Aug 05 '19 at 09:07
  • I am planning to manually define the crop coordinates for each stream that I use. I am aiming to extract the part of the sky, adjust its saturation etc. and send it out as a new stream. In an ideal world I would create a gradient based on the average colour values of the cropped image but I am under the impression that this may not be possible with opencv – Michael Sandford Aug 05 '19 at 09:12

1 Answers1

8

Too much for a comment, but try this to get started:

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)
Dvir Berebi
  • 1,406
  • 14
  • 25
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432