Great job completing Chapter 3! You now understand how to load images from disk, display them to your screen, and write the images back to disk using OpenCV and Python.
However, if you want more practice with these basics, be sure to refer to the first section on this page. I also detail how to use matplotlib rather than OpenCV to display images.
Finally, don’t forget to take the quiz at the bottom of this page to test your skills!
Want more practice loading images?
If you need a little more experience practicing loading images and displaying them to your screen, I would suggest reading this tutorial on the PyImageSearch blog:
http://www.pyimagesearch.com/2014/06/02/opencv-load-image/
In all reality, it may not be the usage of cv2.imread
, cv2.imshow
, or cv2.waitKey
that is tripping you up — it might be the command line arguments.
When going through Chapter 3, if you struggled with either:
- Understanding the
ArgumentParser
code. - Executing the
load_display_save.py
script from the command line.
Then you should spend some more time understanding what command line arguments are and how to use them.
Hands down, my favorite tutorial for understanding command line arguments and how to use them with Python can be found here:
http://www.cyberciti.biz/faq/python-command-line-arguments-argv-example/
If you find yourself struggling with command line arguments, be sure to give the above tutorial a read.
Display images using matplotlib
If you already have prior experience with the matplotlib package, then you might be more comfortable using the plt.imshow
function versus cv2.imshow
.
In that case, you can absolutely use matplotlib’s plt.imshow
function to display images to your screen. The following tutorial demonstrates how to display an image loaded via cv2.imread
using matplotlib:
http://www.pyimagesearch.com/2014/11/03/display-matplotlib-rgb-image/
If you’re comfortable with matplotlib, certainly give it a try. However, keep in mind that this only works for images. Using plt.imshow
is not suitable for displaying frames from a video stream or video file — be sure to keep this in mind or you’ll be scratching your head quite a bit once we reach the Case Studies portion of the book and start working with video.
If you decide to give matplotlib a try, just be sure not to neglect the OpenCV GUI functions detailed in this chapter!
Common matplotlib issues
If you’re using Python virtual environments (specifically under a Linux OS) and your matplotlib figures are not displaying, then you likely have an issue with how the “backend” rendering system is configured.
Luckily, this issue is easy to resolve:
http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/
The solution boils down to uninstalling your current matplotlib install, downloading the matplotlib source, and then installing from source. It sounds complicated, but as the above blog post demonstrates, it’s only five commands that you need to execute.
Another common matplotlib error (especially on OSX) is related to the Python framework. If you try to execute a script on OSX that imports matplotlib
and see the following error message, then you know you are working with a framework issue:
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
This particular error message is due to an incompatibility with the latest release of matplotlib and Python virtual environments.
To get around this issue, you can install an older version of matplotlib:
$ pip uninstall matplotlib $ pip install matplotlib==1.4.3
Essentially, the newer versions of matplotlib will not compile against non-system versions of Python, hence why we have to use an older version.