Downloading and installing OpenCV – a fully featured vision library

Now that you have your camera connected, you can begin to access some amazing capabilities that have been provided by the open source community. The most popular of these for computer vision is OpenCV. To do this, you'll need to install OpenCV. There are several possible ways of doing this; I'm going to suggest ones that I follow to install it on my systems. Once you have booted the system and opened a terminal window, type the following commands in the following order:

  • sudo apt-get update – If you haven't done this in a while, it is a good idea to do this now before you start. You're going to download a number of new software packages, so it is good to make sure everything is up to date.
  • sudo apt-get install build-essential – We have done this in a previous chapter. In case you skipped that part, you will have to refer to it now, as you need this package.
  • sudo apt-get install libavformat-dev – This library provides a way to code and decode audio and video streams.
  • sudo apt-get install ffmpeg – This library provides a way to transcode audio and video streams.
  • sudo apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 – This command shows the basic OpenCV libraries. Note the number in the command. This will almost certainly change as new versions of OpenCV become available. If 2.3 does not work, either try 2.4 or google for the latest version of OpenCV.
  • sudo apt-get install python-opencv – This is the Python development kit needed for OpenCV, as you are going to use Python.
  • sudo apt-get install opencv-doc – This command will show the documentation for OpenCV just in case we need it.
  • sudo apt-get install libcv-dev – This command shows the header file and static libraries to compile OpenCV.
  • sudo apt-get install libcvaux-dev – This command shows more development tools for compiling OpenCV.
  • sudo apt-get install libhighgui-dev – This is another package that provides header files and static libraries to compile OpenCV.

Make sure you are in your home directory, and then type cp -r /usr/share/doc/opencv-doc/examples. This will copy all the examples to your home directory.

Now you are ready to try out the OpenCV library. I prefer to use Python while programming simple tasks; hence, I'll show the Python examples. If you prefer the C examples, feel free to explore. In order to use the Python examples, you'll need one more library. So type sudo apt-get install python-numpy, as you will need this to manipulate the matrices that OpenCV uses to hold images.

Now that you have these, you can try one of the Python examples. Switch to the directory with the Python examples by typing cd /home/pi/examples/python. In this directory, you will find a number of useful examples; we'll only look at the most basic, which is called camera.py. If camera.py is not created, you can create it by typing in the code shown in the next few pages. You can try running this example; however, to do this you'll either need to have a display connected to Raspberry Pi or you can do this over the vncserver connection. Bring up the LXTerminal window and type python camera.py. You should see something as shown in the following screenshot:

Downloading and installing OpenCV – a fully featured vision library

The camera window is quite large; you can change the resolution of the image to a lower one, which will make the update rate faster and the storage requirement for the image smaller. To do this, edit the camera.py file and add two lines, as shown in the following screenshot:

Downloading and installing OpenCV – a fully featured vision library

Here is an explanation of the Python code:

  • import cv2.cv as cv – This line imports the OpenCV library so you can access its functionality.
  • import time – This line imports the time library so you can access the time functionality.
  • cv.NamedWindow("camera", 1) – This line creates a window that you will use to display your image.
  • capture = cvCaptureFromCAM(0) – This line creates a structure that knows how to capture images from the connected webcam.
  • cv.SetCaptureProperty(capture, 3, 360) – This line sets the image width to 360 pixels.
  • cv.SetCaptureProperty(capture, 4, 240) – This line sets the image height to 240 pixels.
  • while True: – Here you are creating a loop that will capture and display the image over and over until you press the Esc key.
  • img = cv.QueryFrame(capture) – This line captures the image and stores it in the img data structure.
  • cv.ShowImage("camera", img) – This line maps the img variable to the camera window you created previously.
  • If cv.WaitKey(10) == 27: – This if statement checks if a key has been pressed, and if the pressed key is the Esc key, it executes the break, which stops the while loop and the program reach its end and stop. You need this statement in your code because it also signals OpenCV to display the image now.

Now run camera.py, and you should see the following screenshot:

Downloading and installing OpenCV – a fully featured vision library

You may want to play with the resolution to find the optimum settings for your application. Bigger images are great—they give you a more detailed view on the world—but they also take up significantly more processing power. We'll play with this more as we actually ask our system to do some real image processing. Be careful if you are going to use vncserver to understand your system performance, as this will significantly slow down the update rate. An image that is twice the size (width/height) will involve four times more processing.

Your project can now see! You will use this capability to do a number of impressive tasks that will use this vision capability.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.62.34