Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
OpenCV Computer Vision Application Programming Cookbook Second Edition

You're reading from  OpenCV Computer Vision Application Programming Cookbook Second Edition

Product type Book
Published in Aug 2014
Publisher Packt
ISBN-13 9781782161486
Pages 374 pages
Edition 1st Edition
Languages
Author (1):
Robert Laganiere Robert Laganiere
Profile icon Robert Laganiere

Table of Contents (18) Chapters

OpenCV Computer Vision Application Programming Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Playing with Images Manipulating Pixels Processing Color Images with Classes Counting the Pixels with Histograms Transforming Images with Morphological Operations Filtering the Images Extracting Lines, Contours, and Components Detecting Interest Points Describing and Matching Interest Points Estimating Projective Relations in Images Processing Video Sequences Index

Chapter 3. Processing Color Images with Classes

In this chapter, we will cover the following recipes:

  • Using the Strategy pattern in an algorithm design

  • Using a Controller design pattern to communicate with processing modules

  • Converting color representations

  • Representing colors with hue, saturation, and brightness

Introduction


Good computer vision programs begin with good programming practices. Building a bug-free application is just the beginning. What you really want is an application that you and the programmers working with you will be able to easily adapt and evolve as new requirements come in. This chapter will show you how to make the best use of some of the object-oriented programming principles in order to build good quality software programs. In particular, we will introduce a few important design patterns that will help you build applications with components that are easy to test, maintain, and reuse.

Design patterns are a well-known concept in software engineering. Basically, a design pattern is a sound, reusable solution to a generic problem that occurs frequently in software designing. Many software patterns have been introduced and well documented. Good programmers should build a working knowledge of these existing patterns.

This chapter also has a secondary objective. It will teach you...

Using the Strategy pattern in an algorithm design


The objective of the Strategy design pattern is to encapsulate an algorithm in a class. This way, it becomes easier to replace a given algorithm by another one or to chain several algorithms together in order to build a more complex process. In addition, this pattern facilitates the deployment of an algorithm by hiding as much of its complexity as possible behind an intuitive programming interface.

Getting ready

Let's say we want to build a simple algorithm that will identify all of the pixels in an image that have a given color. For this, the algorithm has to accept an image and a color as input and will return a binary image showing the pixels that have the specified color. The tolerance with which we want to accept a color will be another parameter to be specified before running the algorithm.

How to do it…

Once an algorithm has been encapsulated in a class using the Strategy design pattern, it can be deployed by creating an instance of this...

Using a Controller design pattern to communicate with processing modules


As you build more complex applications, you will need to create multiple algorithms that can be combined together in order to accomplish some advanced tasks. Consequently, to properly set up the application and have all the classes communicate together will become more and more complex. It then becomes advantageous to centralize the control of the application in a single class. This is the idea behind the Controller design pattern. A Controller is a particular object that plays a central role in an application, and we will explore this in this recipe.

Getting ready

Using your favorite IDE, create a simple dialog-based application with two buttons; one button to select an image, and another button to start the processing, shown as follows:

Here, we use the ColorDetector class of the previous recipe.

How to do it…

The role of the Controller class is to first create the classes required to execute the application. Here, there...

Converting color representations


The earlier recipes taught you how to encapsulate an algorithm into a class. This way, the algorithm becomes easier to use through a simplified interface. Encapsulation also permits you to modify an algorithm's implementation without impacting the classes that use it. This principle is illustrated in the next recipe, where we will modify the ColorDetector class algorithm in order to use another color space. Therefore, this recipe will also be an opportunity to introduce color conversions with OpenCV.

Getting ready

The RGB color space is based on the use of the red, green, and blue additive primary colors. These have been selected because when they are combined together, they can produce a wide gamut of different colors. In fact, the human visual system is also based on the trichromatic perception of colors, with cone cell sensitivity located around the red, green, and blue spectrum. It is often the default color space in digital imagery because that is the...

Representing colors with hue, saturation, and brightness


In this chapter, we played with image colors. We used different color spaces and tried to identify image areas that have a specific color. The RGB color space, for instance, was considered, and although it is an effective representation for the capture and display of colors in electronic imaging systems, this representation is not very intuitive. This is not the way humans think about colors. We talk about colors in terms of their tint, brightness, or colorfulness (that is, whether it is a vivid or pastel color). The phenomenal color spaces based on the concept of hue, saturation, and brightness were introduced to help users to specify the colors using properties that are more intuitive to them. In this recipe, we will explore the concepts of hue, saturation, and brightness as a means to describe colors.

How to do it…

The conversion of a BGR image into a phenomenal color space is done using the cv::cvtColor function that was explored...

lock icon The rest of the chapter is locked
You have been reading a chapter from
OpenCV Computer Vision Application Programming Cookbook Second Edition
Published in: Aug 2014 Publisher: Packt ISBN-13: 9781782161486
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}