Learn OpenCV 4 by Building Projects
eBook - ePub

Learn OpenCV 4 by Building Projects

Build real-world computer vision and image processing applications with OpenCV and C++, 2nd Edition

  1. 310 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn OpenCV 4 by Building Projects

Build real-world computer vision and image processing applications with OpenCV and C++, 2nd Edition

Book details
Book preview
Table of contents
Citations

About This Book

Explore OpenCV 4 to create visually appealing cross-platform computer vision applications

Key Features

  • Understand basic OpenCV 4 concepts and algorithms
  • Grasp advanced OpenCV techniques such as 3D reconstruction, machine learning, and artificial neural networks
  • Work with Tesseract OCR, an open-source library to recognize text in images

Book Description

OpenCV is one of the best open source libraries available, and can help you focus on constructing complete projects on image processing, motion detection, and image segmentation. Whether you're completely new to computer vision, or have a basic understanding of its concepts, Learn OpenCV 4 by Building Projects – Second edition will be your guide to understanding OpenCV concepts and algorithms through real-world examples and projects.

You'll begin with the installation of OpenCV and the basics of image processing. Then, you'll cover user interfaces and get deeper into image processing. As you progress through the book, you'll learn complex computer vision algorithms and explore machine learning and face detection. The book then guides you in creating optical flow video analysis and background subtraction in complex scenes. In the concluding chapters, you'll also learn about text segmentation and recognition and understand the basics of the new and improved deep learning module.

By the end of this book, you'll be familiar with the basics of Open CV, such as matrix operations, filters, and histograms, and you'll have mastered commonly used computer vision techniques to build OpenCV projects from scratch.

What you will learn

  • Install OpenCV 4 on your operating system
  • Create CMake scripts to compile your C++ application
  • Understand basic image matrix formats and filters
  • Explore segmentation and feature extraction techniques
  • Remove backgrounds from static scenes to identify moving objects for surveillance
  • Employ various techniques to track objects in a live video
  • Work with new OpenCV functions for text detection and recognition with Tesseract
  • Get acquainted with important deep learning tools for image classification

Who this book is for

If you are a software developer with a basic understanding of computer vision and image processing and want to develop interesting computer vision applications with OpenCV, Learn OpenCV 4 by Building Projects for you. Prior knowledge of C++ will help you understand the concepts covered in this book.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Learn OpenCV 4 by Building Projects by David Millán Escrivá, Vinícius G. Mendonça, Prateek Joshi in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Vision & Pattern Recognition. We have over one million books available in our catalogue for you to explore.

Information

An Introduction to the Basics of OpenCV

After covering OpenCV installation on different operating systems in Chapter 1, Getting Started with OpenCV, we are going to introduce the basics of OpenCV development in this chapter. It begins with showing how to create our project using CMake. We are going to introduce the basic image data structures and matrices, along with other structures that are required to work in our projects. We are going to introduce how to save our variables and data into files using the XML/YAML persistence OpenCV functions.
In this chapter, we will cover the following topics:
  • Configuring projects with CMake
  • Reading/writing images from/to disk
  • Reading videos and accessing camera devices
  • The main image structures (for example, matrices)
  • Other important and basic structures (for example, vectors and scalars)
  • An introduction to basic matrix operations
  • File storage operations with XML/YAML persistence OpenCV API

Technical requirements

This chapter requires familiarity with the basic C++ programming language. All the code used in this chapter can be downloaded from the following GitHub link: https://github.com/PacktPublishing/Learn-OpenCV-4-By-Building-Projects-Second-Edition/tree/master/Chapter_02. The code can be executed on any operating system, though it is only tested on Ubuntu.
Check out the following video to see the Code in Action:
http://bit.ly/2QxhNBa

Basic CMake configuration file

To configure and check all the requisite dependencies for our project, we are going to use CMake, but it is not the only way that this can be done; we can configure our project in any other tool or IDE, such as Makefiles or Visual Studio, but CMake is a more portable way to configure multiplatform C++ projects.
CMake uses configuration files called CMakeLists.txt, where the compilation and dependencies process is defined. For a basic project based on an executable built from a single source code file, a CMakeLists.txt file comprising three lines is all that is required. The file looks as follows:
cmake_minimum_required (VERSION 3.0) project (CMakeTest) add_executable(${PROJECT_NAME} main.cpp) 
The first line defines the minimum version of CMake required. This line is mandatory in our CMakeLists.txt file and allows us to use the functionality of CMake defined from a specific version; in our case, we require a minimum of CMake 3.0. The second line defines the project name. This name is saved in a variable called PROJECT_NAME.
The last line creates an executable command (add_executable()) from the main.cpp file, gives it the same name as our project (${PROJECT_NAME}), and compiles our source code into an executable called CMakeTest which is the name that we set up as a project name. The ${} expression allows access to any variable defined in our environment. Then, we can use the ${PROJECT_NAME} variable as an executable output name.

Creating a library

CMake allows us to create libraries used by the OpenCV build system. Factorizing shared code among multiple applications is a common and useful practice in software development. In big applications, or common code shared in multiple applications, this practice is very useful. In this case, we do not create a binary executable, but instead we create a compiled file that includes all the functions, classes, and so on. We can then share this library file with other applications without sharing our source code.
CMake includes the add_library function to this end:
# Create our hello library add_library(Hello hello.cpp hello.h) # Create our application that uses our new library add_executable(executable main.cpp) # Link our executable with the new library target_link_libraries(executable Hello) 
The lines starting with # add comments and are ignored by CMake. The add_library (Hello hello.cpp hello.h) command defines the source files of our library and its name, where Hello is the library name and hello.cpp and hello.h are the source files. We add the header file too to allow IDEs such as Visual Studio to link to the header files. This line is going to generate a shared (.so for Mac OS X, and Unix or .dll for Windows) or static library (.a for Mac OS X, and Unix or .lib for Windows) file, depending on whether we add a SHARED or STATIC word between library name and source files. target_link_libraries(executable Hello) is the function that links our executable to the desired library, in our case, the Hello library.

Managing dependencies

CMake has the ability to search our dependencies and external libraries, giving us the ability to build complex projects, depending on the external components in our projects, and add some requirements.
In this book, the most important dependency is, of course, OpenCV, and we will add it to all of our projects:
 cmake_minimum_required (VERSION 3.0) PROJECT(Chapter2) # Requires OpenCV FIND_PACKAGE( OpenCV 4.0.0 REQUIRED ) # Show a message with the opencv version detected MESSAGE("OpenCV version : ${OpenCV_VERSION}") 
# Add the paths to the include directories/to the header files include_directories(${OpenCV_INCLUDE_DIRS})
# Add the paths to the compiled libraries/objects link_directories(${OpenCV_LIB_DIR}) # Create a variable called SRC SET(SRC main.cpp) # Create our executable ADD_EXECUTABLE(${PROJECT_NAME} ${SRC}) # Link our library TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})
Now, let's understand the working of the script from the following:
cmake_minimum_required (VERSION 3.0) cmake_policy(SET CMP0012 NEW) PROJECT(Chapter2) 
The first line defines the minimum CMake version, and the second line tells CMake to use the new behavior of CMake to facilitate recognition of the correct numbers and Boolean constants without dereferencing variables with such names; this policy was introduced in CMake 2.8.0, and CMake warns when the policy is not set from version 3.0.2. Finally, the last line defines the project title. After defining the project name, we have to define the requirements, libraries, and dependencies:
# Requires OpenCV FIND_PACKAGE( OpenCV 4.0.0 REQUIRED ) # Show a message with the opencv version detected MESSAGE("OpenCV version : ${OpenCV_VERSION}") include_directories(${OpenCV_INCLUDE_DIRS}) link_directories(${OpenCV_LIB_DIR})
Here is where we search for our OpenCV dependency. FIND_PACKAGE is the function that allows us to ...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. About Packt
  4. Contributors
  5. Preface
  6. Getting Started with OpenCV
  7. An Introduction to the Basics of OpenCV
  8. Learning Graphical User Interfaces
  9. Delving into Histogram and Filters
  10. Automated Optical Inspection, Object Segmentation, and Detection
  11. Learning Object Classification
  12. Detecting Face Parts and Overlaying Masks
  13. Video Surveillance, Background Modeling, and Morphological Operations
  14. Learning Object Tracking
  15. Developing Segmentation Algorithms for Text Recognition
  16. Text Recognition with Tesseract
  17. Deep Learning with OpenCV
  18. Other Books You May Enjoy