Opencv With Python Blueprints Pdf Download

Joseph Howse, webcasting.

Hi! I'm Joseph Howse, an author, software developer, and business owner.

This is the companion website for my books and presentations on OpenCV and other programming topics:

  • Books:
    • Current Editions in English:
      • OpenCV 4 for Secret Agents [help] [order]
      • Learning OpenCV 4 Computer Vision with Python 3 [help] [order]
      • OpenCV 3 Blueprints [help] [order]
      • iOS Application Development with OpenCV 3 [help] [order]
      • Android Application Programming with OpenCV 3 [help] [order]
      • Python Game Programming by Example [help] [order]
    • Current Editions in Simplified Chinese:
      • OpenCV项目开发实战(原书񻋂版) [help] [order]
      • Android OpenCV应用程序设计 [help] [order]
      • OpenCV 3计算机视觉:Python语言实现 [help] [order]
    • Previous Editions in English:
      • Learning OpenCV 3 Computer Vision with Python [help] [order]
      • OpenCV for Secret Agents [help] [order]
      • Android Application Programming with OpenCV [help]
      • OpenCV Computer Vision with Python [help]
    • Previous Editions in Simplified Chinese:
      • OpenCV项目开发实战 [help] [order]
  • Presentations:
    • "Computer Vision: Aplicaciones y Técnicas Utilizando Reconocimiento Visual" [watch] [PDF]
    • "Visualizing the Invisible" [PDF]
    • "Corrected Vision: The Role Of Camera and Lens Parameters in Real-world Measurement" [PDF]
    • "Training Detectors and Recognizers in Python and OpenCV" [PDF]
    • "Training Intelligent Camera Systems with Python and OpenCV" [watch]
  • Author Interview:
    • "PyDev of the Week: Joseph Howse" [read]

Combined, the books' English editions alone have sold more than 22,000 copies, representing a goodly crowd of computer vision students and professionals. If you are already a reader, thank you! If not, please consider becoming one. Together, we are learning to see the world through a new set of lenses.

On this page, you can download installers and source code that will help you complete the OpenCV projects described in the books and presentations. This site is also the right place to check for FAQ, errata, and updates.

If you have any questions, don't hesitate to contact me at josephhowse@nummist.com.

Which Book is Right for You?

The following comparison table may help you choose the next book to read:

Title Platforms Languages Level
OpenCV 4 for Secret Agents Windows, Mac, Linux, Raspberry Pi, Android, Unity Python, Java, C# Intermediate to Advanced
Learning OpenCV 4 Computer Vision with Python 3 Windows, Mac, Linux Python Beginning and Up
OpenCV 3 Blueprints Windows, Mac, Linux, Android C++, Python, Java Advanced
iOS Application Development with OpenCV 3 iOS Objective-C, C++ Beginning and Up
Android Application Programming with OpenCV 3 Android Java, C++ Beginning and Up
Python Game Programming by Example Windows, Mac, Linux Python Beginning and Up

Bear in mind that my books focus on computer vision, application development, and game development, rather than programming languages per se. If you are unfamiliar with a language, you may want to find a general-purpose guide to the language, too.

Back to list of publications

OpenCV for Secret Agents

This book's English edition (January 2015) is titled OpenCV for Secret Agents.

OpenCV项目开发实战 (September 2016) is a Simplified Chinese translation of the book. The English-language downloads and FAQ in this section might also help readers of OpenCV项目开发实战, but for Chinese-language support please visit Tsinghua University Press.

Downloads

  • OpenNI binaries that are compatible with SensorKinect:
    • Windows, 32-bit (x86)
    • Windows, 64-bit (x86-64)
    • Linux, 32-bit (x86)
    • Linux, 64-bit (x86-64)
  • MacPorts-compatible repository for OpenNI, SensorKinect, and OpenCV
  • Debian Wheezy- and Ubuntu-compatible installation script for OpenNI, SensorKinect, and OpenCV
    • For installing OpenNI and SensorKinect, the script supports x86 and x86-64.
    • For installing OpenCV, the script supports any architecture. For example, it works on Raspbian (ARM).
  • Source code for Chapter 2
  • Source code for Chapter 3
    • For OpenCV 3.x, Chapter 3 depends on the face module from opencv_contrib. (Follow the preceding link to find instructions on building OpenCV from source with opencv_contrib. Alternatively, on Mac with MacPorts, install the opencv package with the +contrib variant.) For OpenCV 2.4.x, opencv_contrib is not required.
  • Source code for Chapter 4
  • Source code for Chapter 5
  • Source code for Chapter 6
  • Source code for Chapter 7

FAQ

Q. Which OpenCV versions does the book support?

A. Originally, the book targets OpenCV 2.4.x. The source code downloads have been updated to support both OpenCV 2.4.x and OpenCV 3.x (together in one download per chapter). For the Python projects, a script called CVForwardCompat.py simply creates aliases to give OpenCV 3.x functionality the same names as equivalent OpenCV 2.4.x functionality.

Errata and Updates

Improved cat face cascades with lower false positive rate

I have trained new versions of the cat face cascades with more negative samples and more stages. False positives are much rarer now. If you tailored your code for the cascade's previous version, now you should re-adjust the arguments of CascadeClassifier::detectMultiScale. For example, decrease the value of the minNeighbors argument. The book's source code download for Chapter 3 has been updated to train and use the new cascades.

You do not need to use a human face detector to cross-check the positives anymore. However, the source code download still contains an example of how to do this (in case you want to be extra cautious).

For further information on the new cascades, see the merge request where the cascades have been merged into OpenCV.

Finding the VOC2007 dataset and VOC2007 website

The VOC2007 dataset and website are no longer available at the URLs listed in the book. Instead, use the University of Texas mirror of the VOC2007 dataset. The book's source code download for Chapter 3 has been updated to use this mirror in the cascade_training/download_datasets.sh script.

Alternatively, the University of Oxford Robotics Research group has a mirror of the entire VOC2007 website, where you can find a download of the VOC2007 dataset.

Similarly, Joseph Redmon has a mirror of the VOC2007 and VOC2012 documents and datasets, including a download of the VOC2007 dataset.

Finding the Microsoft Cat Dataset 2008

The Microsoft Cat Dataset 2008 and its website are no longer available at the URLs listed in the book. Instead, use the Internet Archive Wayback Machine's mirror of the site, which includes downloads of Part 1 and Part 2 of the dataset. The book's source code download for Chapter 3 has been updated to use this mirror in the cascade_training/download_datasets.sh script.

Setting and validating the camera's image dimensions

On page 85, the implementation of the cvResizeCapture function has problems with some cameras and some builds of OpenCV. It may fail to return the camera's actual image dimensions. The following implementation is more robust:

def cvResizeCapture(capture, preferredSize):     # Try to set the requested dimensions.     w, h = preferredSize     capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, w)     capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, h)     # Return the actual dimensions.     w = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))     h = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))     return (w, h)

Note that in the updated implementation, we do not check the Boolean returned by capture.set because its value might not accurately reflect success; it may be true even if the requested image dimensions could not be set. Instead, we always check the actual image dimensions using capture.get.

Even the updated implementation is not foolproof. For example, buggy camera drivers could report inaccurate dimensions via capture.get or could capture incomplete images (with fewer rows than normal) at the start of a stream. To be absolutely sure of an image's dimensions, capture the image first and then check its dimensions. Remember, in OpenCV's Python API, the image is a NumPy array and you can check its dimensions via its shape attribute.

The downloadable source code contains the updated implementation.

Update to Bing Search API

Microsoft has discontinued support for Azure Data Market, which includes Bing Search API v2. As a replacement, Microsoft has introduced Cognitive Services, which includes Bing Search API v5. The book's source code download for Chapter 2 has been updated to use Bing Search API v5 via a third-party Python wrapper called py-ms-cognitive. A version of py-ms-cognitive is included with the chapter's code, or you can find the latest version on the py-ms-cognitive GitHub repository.

After you download the chapter's code, edit ImageSearchSession.py to replace the value of bingKey (in the search method) with your own Bing Search API key. You can obtain a free key on the Microsoft Cognitive Services website. Old keys from Azure Data Market are incompatible.

Installing Eclipse as part of TADP

For recent versions of TADP, the Express installation option does not include Eclipse. Select the Custom installation option and ensure that the Eclipse component is checked. Alternatively, select the Complete installation option.

More options when building from source on Windows

Recent versions of OpenCV have good support for Visual Studio 2013 (VS2013). Use the following CMake flag to create a 32-bit VS2013 project:

-G "Visual Studio 12 2013"

Alternatively, use the following CMake flag to create a 64-bit VS2013 project:

-G "Visual Studio 12 2013 Win64"

Refer to the book for more complete instructions on CMake usage.

Mac Homebrew installation steps have changed

On Mac, if we are installing OpenCV via the latest version of Homebrew, the book's instructions are partly outdated. Step 1 on page 18 gives the command to install Homebrew itself, and this command has changed since I wrote the book. If you do not already have Homebrew installed, run the following command instead:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The Homebrew installation command may well change again in the future. For up-to-date instructions, see the Homebrew website.

The remaining steps are still valid.

TADP OpenCV version number

On page 132, the first paragraph should say "OpenCV Library – 2.4.8.2", not "OpenCV Library – 2.8.4.2".

Back to list of publications

Android Application Programming with OpenCV 3

This book's 2nd edition (June 2015) is titled Android Application Programming with OpenCV 3. The 1st edition (September 2013) is just titled Android Application Programming with OpenCV (without the "3"). Both editions are covered in this section.

Android OpenCV应用程序设计 (January 2015) is a Simplified Chinese translation of the 1st edition. The English-language downloads and FAQ in this section might also help readers of Android OpenCV应用程序设计, but for Chinese-language support please visit Tsinghua University Press.

Downloads

  • Source code for Chapter 2 [for OpenCV 3.x] [for OpenCV 2.x]
  • Source code for Chapter 3 [for OpenCV 3.x] [for OpenCV 2.x]
  • Source code for Chapter 4 [for OpenCV 3.x] [for OpenCV 2.x]
  • Source code for Chapter 5 [for OpenCV 3.x] [for OpenCV 2.x]
  • Source code for Chapter 6 [for OpenCV 3.x] [for OpenCV 2.x]
    • Chapter 6 is new in the book's 2nd edition. It covers OpenCV's C++ interface and the use of Java Native Interface (JNI) to integrate C++ code into an Android Java application.

FAQ

Q. Which OpenCV versions does the book support?

A. The book's 1st edition targets OpenCV 2.4.x. The 2nd edition targets OpenCV 3.x with notes on backward compatibility for OpenCV 2.4.x. The source code downloads have been updated to support both OpenCV 2.4.x and OpenCV 3.x (separately in two downloads per chapter).

Errata and Updates (2nd Edition)

mSceneCorners should initially be empty

On page 87, mSceneCorners should be initialized as an empty matrix. This way, the draw method (on page 92) will not draw any corners in the initial state. The downloadable source code contains the correct initialization.

Calculating focal length

On page 108, focal length should be calculated according to the following formula:

focalLengthPx = 0.5 * diagonalPx /         sqrt((tan(0.5 * fovX))^2 + (tan(0.5 * fovY))^2)

The downloadable source code has been updated to implement this formula.

Proof

We know that FOV, image size, and focal length have the following relationship:

diagonalFOV = 2 * atan(0.5 * diagonalPx / focalLengthPx)

Solving for focal length:

focalLengthPx = 0.5 * diagonalPx / tan(0.5 * diagonalFOV)

Note that tan(0.5 * diagonalFOV) is the hypotenuse of tan(0.5 * fovX) and tan(0.5 * fovY). Thus:

focalLengthPx = 0.5 * diagonalPx /         sqrt((tan(0.5 * fovX))^2 + (tan(0.5 * fovY))^2)

Q.E.D.

Errata and Updates (1st Edition)

New features including configurable image size and v7 appcompat

The source code downloads have been updated to include the following new features:

  • Support for selecting an image size via the menu.
  • Better backward compatibility. The v7 appcompat library (part of the Android Support Libraries) is required. Import it into your workspace as a project (not a JAR because the JAR does not include the required resources). The library's root folder is <android_sdk>/extras/android/support/v7/appcompat (where <android_sdk> is the root of the Android SDK). Edit the application project's properties (right-click -> Properties -> Android -> Add…) to add a reference to the v7 appcompat library project, as well as the OpenCV library project.

Library projects may need to be on same drive as application project

On some systems, Eclipse may have difficulty linking to the library projects (OpenCV and v7 appcompat) if the application project is located on another drive. This may result in unresolved imports or unresolved variables, including appcompat resource and style variables. Putting the library projects and application project on the same drive is recommended.

Installing Eclipse as part of TADP

For recent versions of TADP, the Express installation option does not include Eclipse. Select the Custom installation option and ensure that the Eclipse component is checked. Alternatively, select the Complete installation option.

NativeCameraView broken on some Android versions and some devices

OpenCV's NativeCameraView is prone to device-specific bugs and it also tends to break when new Android OS versions come out. Symptoms of NativeCameraView problems can include an entirely black camera view or a crash after the app is running for about 30 seconds. One of the following errors might be logged during the crash:

@@@ ABORTING: HEAP MEMORY CORRUPTION IN tmalloc_small
Channel is unrecoverably broken and will be disposed!

The OpenCV team continually works to patch these incompatibilities. Try upgrading OpenCV via OpenCV Manager. If you still encounter problems, replace all occurrences of NativeCameraView with JavaCameraView as a workaround. JavaCameraView may be less optimized but it is more stable. The downloadable source code now uses JavaCameraView.

If you do narrow down the problem to NativeCameraView, please help the OpenCV community by filing a bug report on OpenCV DevZone (unless someone else has already filed a relevant bug report for your Android version and device).

Choice of feature detection, descriptor extraction, and descriptor matching algorithms

On pages 80-81, FeatureDetector.STAR, DescriptorExtractor.FREAK, and DescriptorMatcher.BRUTEFORCE_HAMMING were recommended. Based on further testing in recent versions of OpenCV, the following alternatives are actually faster and more reliable: FeatureDetector.ORB, DescriptorExtractor.ORB, and DescriptorMatcher.BRUTEFORCE_HAMMINGLUT. The downloadable source code uses the latter options.

For other authors' evaluations of the algorithms and their implementations in OpenCV, see the following sources:

  • "A Comparative Evaluation of Well-known Feature Detectors and Descriptors" by Şahin Işık and Kemal Özkan
  • "Comparison of OpenCV's feature detection algorithms – II" by Ievgen Khvedchenya
  • "Comparison of feature descriptors" by Ievgen Khvedchenya

mSceneCorners should initially be empty

On page 81, mSceneCorners should be initialized as an empty matrix. This way, the draw method (on page 86) will not draw any corners in the initial state. The downloadable source code contains the correct initialization.

Local variable akbarHunting must be defined

On pages 88-89, the definition of the local variable akbarHunting was omitted. It must be defined in much the same way as the local variable starryNight on page 88. The downloadable source code contains the correct definitions.

Video size and aspect ratio in CameraProjectionAdapter

On pages 96-99, there was an unstated assumption that the size, aspect ratio, and vertical FOV of the camera's current video mode are the same as the size, aspect ratio, and vertical FOV that are implied by:

  • CameraParameters.getPictureSize; or
  • The combination of CameraParameters.getHorizontalViewAngle and CameraParemeters.getVerticalViewAngle

This assumption is unsound. The downloadable source code contains a better implementation of CameraProjectionAdapter, which takes the video size as an additional argument to the constructor. For some formulae, this implementation calculates an aspect ratio (and then vertical FOV) based on the size of the current video mode. Horizontal FOV is assumed to be a constant (whose value is given by CameraParameters.getHorizontalViewAngle) because most cameras crop from the top and bottom ("letterboxing") to obtain video modes with a different aspect ratio than the sensor's physical aspect ratio.

For the calculation of focal length, the uncropped horizontal FOV, vertical FOV, and aspect ratio are still used.

Calculating focal length

On page 99, focal length should be calculated according to the following formula:

focalLengthPx = 0.5 * diagonalPx /         sqrt((tan(0.5 * fovX))^2 + (tan(0.5 * fovY)^2)))

The downloadable source code has been updated to implement this formula.

Proof

We know that FOV, image size, and focal length have the following relationship:

diagonalFOV = 2 * atan(0.5 * diagonalPx / focalLengthPx)

Solving for focal length:

focalLengthPx = 0.5 * diagonalPx / tan(0.5 * diagonalFOV)

Note that tan(0.5 * diagonalFOV) is the hypotenuse of tan(0.5 * fovX) and tan(0.5 * fovY). Thus:

focalLengthPx = 0.5 * diagonalPx /         sqrt((tan(0.5 * fovX))^2 + (tan(0.5 * fovY)^2)))

Q.E.D.

Method is named findCorners, not findHomography

On page 100, "Let's rename our findHomography method to findPose," should instead read, "Let's rename our findCorners method to findPose." Similarly, on page 101, "After finding keypoints, the implementation of findPose starts to differ from the old findHomography method," should instead read, "After finding keypoints, the implementation of findPose starts to differ from the old findCorners method."

Converting position and rotation from OpenCV to OpenGL

On pages 101-102, there were errors in converting the tracked object's rotation and position from OpenCV's formats to OpenGL's format. The following are the correct steps:

  1. Negate the x rotation (before constructing the rotation matrix using OpenCV's Calib3d.Rodrigues method).
  2. Negate the y position.
  3. Negate the z position.
  4. Transpose the rotation matrix.

The following are the underlying differences between OpenCV's and OpenGL's coordinate systems:

  • Positive y is up in OpenGL, down in OpenCV.
  • Positive z is backward in OpenGL, forward in OpenCV.
  • Positive angles are counter-clockwise in OpenGL, clockwise in OpenCV.
  • Due to the preceding differences, x angles are negated but y and z angles are double-negated (that is, unchanged).
  • Transformation matrices are column-major in OpenGL, row-major in OpenCV.

The downloadable source code performs the correct conversions.

Back to list of publications

Python Game Programming by Example

This section covers Chapter 7 of the book. For support of other chapters, please contact Alejandro Rodas de Paz at alexrdp90@gmail.com.

Downloads

  • Source code for Chapter 7

Back to list of publications

OpenCV Computer Vision with Python

This book's 2nd edition (September 2015) is titled Learning OpenCV 3 Computer Vision with Python. See Joe Minichino's support site for the 2nd edition. The 1st edition (September 2013) is just titled OpenCV Computer Vision with Python and it is covered in this section.

Downloads

  • OpenNI binaries that are compatible with SensorKinect:
    • Windows, 32-bit (x86)
    • Windows, 64-bit (x86-64)
    • Linux, 32-bit (x86)
    • Linux, 64-bit (x86-64)
  • MacPorts-compatible repository for OpenNI, SensorKinect, and OpenCV
  • Debian Wheezy- and Ubuntu-compatible installation script for OpenNI, SensorKinect, and OpenCV
    • For installing OpenNI and SensorKinect, the script supports x86 and x86-64.
    • For installing OpenCV, the script supports any architecture. For example, it works on Raspbian (ARM).
  • Source code for Chapter 2
  • Source code for Chapter 3
  • Source code for Chapter 4
  • Source code for Chapter 5
  • Source code for Appendix A

FAQ

Q. Which OpenCV versions does the book support?

A. The book's 1st edition and this site's source code downloads target OpenCV 2.4.x. The book's 2nd edition and the source code hosted on Joe Minichino's support site target OpenCV 3.x.

Q. Which OpenCV Python module does the book use: cv or cv2?

A. The book uses the cv2 module, which is what you want for new projects. See this comparison.

Errata and Updates

More Options When Building from Source on Windows

Recent versions of OpenCV have good support for 64-bit Python and Visual Studio 2013 (VS2013). For example, I use the following commands to create a 64-bit VS2013 project:

mkdir C:\OpenCV\x64 C: cd C:\OpenCV\x64 cmake -DCMAKE_BUILD_TYPE=RELEASE ^       -DWITH_OPENGL=ON ^       -DWITH_TBB=ON ^       -DPYTHON2_LIBRARY=C:/Python27/libs/python27.lib ^       -DPYTHON2_INCLUDE_DIR=C:/Python27/include ^       -G "Visual Studio 12 2013 Win64" ^       Z:/SDKs/OpenCV/fork > cmake.log Z:

Here, my source folder is Z:\SDKs\OpenCV\fork and my build folder is C:\OpenCV\x64. Your paths may vary.

OpenNI Binaries for Use with SensorKinect on Windows

Since openni.org is defunct, binaries can no longer be downloaded from that site. Instead, get them from this book's Downloads section, above.

Captured Frame may be None

The book's sample code assumes that frames in a camera capture session are reliably non-None (until the session terminates). This assumption is wrong. For some cameras, the first few frames of a session may be None, yet subsequent frames may be non-None. The source code downloads, above, are corrected so that the program's main loop continues even if a given frame is None.

Reported Frame Rate may be Negative

The book's sample code assumes that OpenCV always reports a non-negative value for the capture frame rate. On some platforms, including Ubuntu, this assumption is wrong. When the assumption is violated, the sample code may produce the following error message, which is misleading:

OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open

(The problem actually has nothing to do with codecs.)

When OpenCV reports a negative frame rate, we should measure the actual frame rate instead, just as we do when OpenCV reports a frame rate of 0.0. The fix is to modify the following conditional statement in managers.py:

if fps == 0.0:

Here is the corrected version:

if fps <= 0.0:

This change is incorporated into the source code downloads, above.

Kinect Model 1473 Unsupported on Mac OS X Mountain Lion

The sample code for Chapter 5 is tested successfully with Kinect Model 1414. On some platforms, other Kinect models may be unsupported by the SensorKinect driver. Specifically, the driver is known to fail on Mac OS X Mountain Lion for Kinect Model 1473. (It crashes after about 20 seconds of input.)

The model number should be printed on the bottom of the Kinect device.

Mac Homebrew Installation Steps Have Changed

On Mac, if we are installing OpenCV via the latest version of Homebrew, the book's instructions are partly outdated. Step 1 on page 16 gives the command to install Homebrew itself, and this command has changed since I wrote the book. If you do not already have Homebrew installed, run the following command instead:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The Homebrew installation command may well change again in the future. For up-to-date instructions, see the Homebrew website.

Also ignore step 8 on page 17 of the book and instead run the following commands:

$ brew tap homebrew/science $ brew install opencv

(We are accessing an optional repository called homebrew/science, which contains the latest opencv package.)

Next, edit your ~/.profile file to add the following line:

export PYTHONPATH=/usr/local/Cellar/opencv/2.4.6.1/lib/python2.7/site-packages:$PYTHONPATH

(We are telling Python where to find Homebrew's installation of OpenCV.)

Now, OpenCV should be ready to use.

Function is named createFlatView, not flatView

On page 50, the following lines of code are incorrect:

srcFlatView = utils.flatView(src) dstFlatView = utils.flatView(dst)

Replace them with the following lines instead:

srcFlatView = utils.createFlatView(src) dstFlatView = utils.createFlatView(dst)

Back to list of publications

Posted by: montemontefrosse0267229.blogspot.com

Source: https://nummist.com/opencv/

Post a Comment

Previous Post Next Post