0

I'm struggling to get opencv2 to work with my NOIR camera board.

I'm following this guide to make the pi camera work with the opencv2, but don't fully understand what it's doing, and can not get it working with a python file below.

#!/usr/bin/env python

import cv2

# Camera 0 is the integrated web cam on my netbook
camera_port = 0

#Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 60

# Now we can initialize the camera capture object with the cv2.VideoCapture class.
# All it needs is the index to a camera port.
camera = cv2.VideoCapture(camera_port)

# Captures a single image from the camera and returns it in PIL format
def get_image():
 # read is the easiest way to get a full image out of a VideoCapture object.
 retval, im = camera.read()
 return im

# Ramp the camera - these frames will be discarded and are only used to allow v4l2
# to adjust light levels, if necessary
for i in xrange(ramp_frames):
 temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
file = "test_image_1_from_open_cv2.jpg"
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)

# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del(camera)

When run, the python file doesn't throw up any errors, but returns an empty jpg. The python code works on my PC, but not on my PI.

This leads me to think its how opencv links to the camera board, that's the problem.

As far as I understand it, the above guide instructs us to search through "C files that opencv might use, like raspistill" then re-compiles them to work with the camera board.

It does this by searching, and replacing, using the command sed -i ‘s/if (DEFINED CMAKE_TOOLCHAIN_FILE)/if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)/g’ makefiles/cmake/arm-linux.cmake

The search criteria has been kindly explained here, but could someone please explain

1) What I need to do with the compiled versions of say raspistill. it's currently in /opt/vc/build/bin

2) If there are 2 raspistills on my PI how does the PI know which one to use?

  • I managed to get it kinda working ish by stopping motion sudo /etc/init.d/motion stop then running the above script from the directory the script is located /usr/src/scripts and by running using the command sudo ./videocaprure_opencv2.py BUT the resultant image is tiny! Why? – reggie Feb 23 '15 at 10:21
0

1) stop any use of camera board by anything else, in my case motion: sudo /etc/init.d/motion stop

2) load the driver so the camera board works with opencv: sudo modprobe bcm2835-v4l2

3) run the modified script: sudo /usr/src/scripts/videocap_opencv2_1.py with the lines included: camera.set(3,800) camera.set(4,600)

videocap_opencv2_1.py

#!/usr/bin/env python

import cv2

# Camera 0 is the integrated web cam on my netbook
camera_port = 0

#Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 60

# Now we can initialize the camera capture object with the cv2.VideoCapture class.
# All it needs is the index to a camera port.
camera = cv2.VideoCapture(camera_port)


camera.set(3,800)
camera.set(4,600)

# Captures a single image from the camera and returns it in PIL format
def get_image():
 # read is the easiest way to get a full image out of a VideoCapture object.
 retval, im = camera.read()
 return im

# Ramp the camera - these frames will be discarded and are only used to allow v4l2
# to adjust light levels, if necessary
for i in xrange(ramp_frames):
 temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()



file = "/usr/src/scripts/test_image_1_from_open_cv2.jpg"
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)





# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del(camera)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.