Search code examples
pythonc++opencvstereo-3d

Video capture using Opencv through single port multi-head (stereo) usb camera giving single output


I have recently bought a stereo camera through Amazon and I want to use it for depth mapping. The problem is that the output that I get from the camera is in the form of a single video with the output of both the cameras.

Camera output

What I want is two seprate outputs from the single usb port if it is possible.I can use cropping but I dont want to use that because i am trying to reduce the processing time and I want the outputs sepratley.

The obove image was generated from the following code

  import numpy as np
  import cv2

  cam = cv2. VideoCapture(1)
  cam.set(cv2.CAP_PROP_FPS, 120)

  cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
  cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

  while(1):
  s,orignal = cam.read()
  cv2.imshow('original',orignal)

  if cv2.waitKey(1) & 0xFF == ord('w'):
  break


  cam.release()
  cv2.destroyAllWindows()

I have also tried other techniques such as:

  import numpy as np
  import cv2


  left = cv2.VideoCapture(1)
  right = cv2.VideoCapture(2)


  left.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
  left.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
  right.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
  right.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

  left.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
  right.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))

  # Grab both frames first, then retrieve to minimize latency between cameras
  while(True):
  _, leftFrame = left.retrieve()
  leftWidth, leftHeight = leftFrame.shape[:2]
  _, rightFrame = right.retrieve()
  rightWidth, rightHeight = rightFrame.shape[:2]

  # TODO: Calibrate the cameras and correct the images

  cv2.imshow('left', leftFrame)
  cv2.imshow('right', rightFrame)
  if cv2.waitKey(1) & 0xFF == ord('q'):
  break

  left.release()
  right.release()
  cv2.destroyAllWindows()

but they are not recognising the 3rd camera any help would be nice.

My openCV version is 3.4

P.S If anyone can present a soloution in c++ it would also work for me


Solution

  • Ok so after analysing the problem I figured that the best way would be to crop the images in half as it saves processing time. If you have two different image sources then your pipeline time is doubled for getting these images. After testing the stereo camera using cropping and without cropping I saw no noticeable change in the FPS. Here is a simple code for cropping the video and displaying it in two different windows.

    import numpy as np
    import cv2
    
    cam = cv2.  VideoCapture(1)
    cam.set(cv2.CAP_PROP_FPS, 120)
    
    cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    s,orignal = cam.read()
    height, width, channels = orignal.shape
    print(width)
    print(height)
    while(1):
        s,orignal = cam.read()
        left=orignal[0:height,0:int(width/2)]
        right=orignal[0:height,int(width/2):(width)]
        cv2.imshow('left',left)
        cv2.imshow('Right',right)
    
        if cv2.waitKey(1) & 0xFF == ord('w'):
    
            break
    
    
    cam.release()
    cv2.destroyAllWindows()
    

    [Left Image

    Right Image