1

Using the documentation here here, the code

import time
import picamera
import picamera.array
import cv2

with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(2)
    with picamera.array.PiRGBArray(camera) as stream:
        camera.capture(stream, format='bgr')
        # At this point the image is available as stream.array
        image = stream.array

As i understand it, this returns a numpy array to be read in openCV correct? My question is, how do I then open/access it to perform image processing on it in openCV?

1

On the last line of that script, image is a numpy array with shape (rows, cols, color-plane) with the color planes in BGR order - which is precisely how OpenCV represents image data. In other words, you can just pass that array straight to OpenCV functions:

import time
import picamera
import picamera.array
import cv2

with picamera.PiCamera() as camera:
    time.sleep(2)
    with picamera.array.PiRGBArray(camera) as stream:
        camera.capture(stream, format='bgr')
        # At this point the image is available as stream.array
        image = stream.array
        image = cv2.dilate(image, None)
        image = cv2.erode(image, None)
        image = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY)
        r, image = cv2.threshold(image, 127, 255, 1)
        cv2.namedWindow('win')
        cv2.imshow('win', image)
        cv2.waitKey(0)
  • Hi,Thanks for your answer. However, I used this code and I simply get a constant video feed from my webcam which fills my whole screen. How do i fix this? – Kingdavidek Aug 5 '15 at 19:17
  • Sorry, that's just the preview - comment out the start_preview line (I was testing it over SSH so I didn't notice that) – Dave Jones Aug 5 '15 at 19:18
  • Hi, I have tried this but all it gives me is an empty windows named 'win' :/ Could it be I am missing some software or something? I am running opencv 2.4 – Kingdavidek Aug 7 '15 at 9:43
  • That little demo script just takes an image, performs dilation and erosion, thresholds it and displays the result. Depending on the image it might come out completely black, completely white, or show the shape of something (it shows my hand quite nicely when I stick it in front of the camera but it'll depend very much on the lighting). Comment out the threshold line if you want to see the black'n'white results of the dilation and erosion. Still, it's demonstrating that it's working happily with OpenCV otherwise you'd get an exception... – Dave Jones Aug 7 '15 at 12:16
  • Hi,yes totally, I see it now when I have something with more distinct boundaries in front :) Thanks for your help and thanks so much for your work! One more question, this captures streams of images right? Would it be possible to do this on a video capture frame by frame, using opencv? – Kingdavidek Aug 8 '15 at 15:49

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.