Book Image

OpenCV 3.0 Computer Vision with Java

By : Daniel Lelis Baggio
Book Image

OpenCV 3.0 Computer Vision with Java

By: Daniel Lelis Baggio

Overview of this book

Table of Contents (15 chapters)
OpenCV 3.0 Computer Vision with Java
About the Author
About the Reviewers


Living in times when self-driving vehicles are becoming a reality might trigger curious minds as to how could computers' incipient vision works. Having a face recognized for access control, getting our pictures automatically organized by a subject or person, and having characters automatically recognized from paper scans are tasks that have become common in our lives. All these aforementioned actions have been enlisted in the so-called study area of computer vision.

As a scientific discipline, the theory behind systems that can extract information from images can be described as computer vision, and it has been adopted to extract valuable measurements from medical images, as well as to help humans delineate the boundaries of important image areas in the so-called semi-automatic procedures.

In the context of providing a simple-to-use computer vision infrastructure to help people rapidly build sophisticated vision applications, an open source library was created: OpenCV. It was designed for real-time applications and is written in C++, containing several hundred computer vision algorithms.

Although OpenCV had its debut alpha release back in January 1999, it was only in February 2013 that it officially supported desktop Java through bindings. As this is one of the most popular introductory teaching languages adopted in computer science departments as well as K-12 computer-related courses, it is important to have a good reference for how to build vision apps in a Java environment.

This book covers the basic OpenCV computer vision algorithms and their integration with Java. As the Swing GUI widget toolkit is widely adopted to build GUIs in Java, in this book, you will benefit from the chapters that deal with this topic as well as come to know how to set up your development environment that deals with native code bindings. Besides, operations such as stretching, shrinking, warping, and rotating, as well as finding edges, lines, and circles are all covered through interesting and practical sample projects in this book.

As the Kinect device has become a great tool for background segmentation, we have covered it in this chapter as well.

Another hot topic that is commonly explored with computer vision is machine learning, and in this book, you will find useful information to create your own object tracker and to use OpenCV's built-in face tracker as well.

Since Java has been widely used for web applications, we have covered computer vision applications on the server side as well, explaining the details of image uploading and integration with OpenCV.

By the end of this book, you will have a solid background in how to use Java with OpenCV from setup to server side; a brief explanation of the basic computer vision topics are covered in the book. Also, you'll get the source code of several complete projects from which you can extend and add your own functionality.

What this book covers

Chapter 1, Setting Up OpenCV for Java, covers the setting up of a library and development environment. This chapter covers Eclipse and NetBeans IDEs, as well as explaining the Ant and Maven build tools configuration.

Chapter 2, Handling Matrices, Files, Cameras, and GUIs, shows how to access matrices at the pixel level as well as how to load and display images from files and web cameras. It also covers the Swing widget toolkit support and how to work with OpenCV.

Chapter 3, Image Filters and Morphological Operators, deals with the process of removing noise from images as well as morphological operators. It also explains image pyramids and topics such as flood fill and image thresholding.

Chapter 4, Image Transforms, explains important transformations to find edges, such as the Gradient and Sobel filters. Additionally, it also explains line and circle Hough transforms, which are used to identify not only straight but also radial lines. The Discrete Fourier analysis and some distance transforms are also explained in this chapter.

Chapter 5, Object Detection Using Ada Boost and Haar Cascades, demonstrates how to create your own classifier to find some objects, as well as how to use the famous face detection classifier.

Chapter 6, Detecting Foreground and Background Regions and Depth with a Kinect Device, explores the important problem of extracting your background. Furthermore, it explains how to use a Kinect device to retrieve depth information.

Chapter 7, OpenCV on the Server Side, explains how to set up a web server application with OpenCV.

What you need for this book

If you are a Java developer, student, researcher, or hobbyist wanting to create computer vision applications in Java then this book is for you. If you are an experienced C/C++ developer who is used to working with OpenCV, you will also find this book very useful for migrating your applications to Java.

All you need is basic knowledge of Java, with no prior understanding of computer vision required, as this book will give you clear explanations and examples of the basics.

Who this book is for

If you are a C/C++ developer, student, researcher, or hobbyist wanting to create computer vision applications in Java, then this book is for you. If you are an experienced C/C++ developer who is used to working with OpenCV, you will also find this book very useful to migrate your applications to Java.

All you need is a basic knowledge of Java. No prior understanding of computer vision is required, as this book will give you clear explanations and examples of the basics.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Another way to get the source code is by using the git tool."

A block of code is set as follows:


When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

imageView.addMouseListener(new MouseAdapter()
  public void mousePressed(MouseEvent e)
  {,new Point(e.getX(),e.getY()),20, new Scalar(0,0,255), 4);

Any command-line input or output is written as follows:

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev ant

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: " Go to Window | Preferences, and type classpath variables in the search box."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.