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 Answer 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– Kingdavidek2015年08月05日 19:17:01 +00:00Commented Aug 5, 2015 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– Dave Jones2015年08月05日 19:18:05 +00:00Commented Aug 5, 2015 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.4Kingdavidek– Kingdavidek2015年08月07日 09:43:53 +00:00Commented Aug 7, 2015 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– Dave Jones2015年08月07日 12:16:05 +00:00Commented Aug 7, 2015 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– Kingdavidek2015年08月08日 15:49:19 +00:00Commented Aug 8, 2015 at 15:49