树莓派拍照

https://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

Step 5: Accessing a single image of your Raspberry Pi using Python and OpenCV.

Alright, now we can finally start writing some code!

Open up a new file, name it test_image.py , and insert the following code:

We’ll start by importing our necessary packages on Lines 2-5.

From there, we initialize our PiCamera object on Line 8 and grab a reference to the raw capture component on Line 9. This rawCapture  object is especially useful since it (1) gives us direct access to the camera stream and (2) avoids the expensive compression to JPEG format, which we would then have to take and decode to OpenCV format anyway. I highly recommend that you use PiRGBArray  whenever you need to access the Raspberry Pi camera — the performance gains are well worth it.

From there, we sleep for a tenth of a second on Line 12 — this allows the camera sensor to warm up.

Finally, we grab the actual photo from the rawCapture  object on Line 15 where we take special care to ensure our image is in BGR format rather than RGB. OpenCV represents images as NumPy arrays in BGR order rather than RGB — this little nuisance is subtle, but very important to remember as it can lead to some confusing bugs in your code down the line.

Finally, we display our image to screen on Lines 19 and 20.

To execute this example, open up a terminal, navigate to your test_image.py  file, and issue the following command:

If all goes as expected you should have an image displayed on your screen:

Figure 6: Grabbing a single image from the Raspberry Pi camera and displaying it on screen.

Figure 6: Grabbing a single image from the Raspberry Pi camera and displaying it on screen.

Note: I decided to add this section of the blog post after I had finished up the rest of the article, so I did not have my camera setup facing the couch (I was actually playing with some custom home surveillance software I was working on). Sorry for any confusion, but rest assured, everything will work as advertised provided you have followed the instructions in the article!

猜你喜欢

转载自blog.csdn.net/popppig/article/details/80376755