OpenCV 2 Computer Vision Application Programming Cookbook


OpenCV 2 Computer Vision Application Programming Cookbook
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
$22.94
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
$44.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • Teaches you how to program computer vision applications in C++ using the different features of the OpenCV library
  • Demonstrates the important structures and functions of OpenCV in detail with complete working examples
  • Describes fundamental concepts in computer vision and image processing
  • Gives you advice and tips to create more effective object-oriented computer vision programs
  • Contains examples with source code and shows results obtained on real images with detailed explanations and the required screenshots

Images

Book Details

Language : English
Paperback : 304 pages [ 235mm x 191mm ]
Release Date : May 2011
ISBN : 1849513244
ISBN 13 : 9781849513241
Author(s) : Robert Laganière
Topics and Technologies : All Books, Application Development, Cookbooks, Open Source, Web Graphics & Video

Table of Contents

Preface
Chapter 1: Playing with Images
Chapter 2: Manipulating the Pixels
Chapter 3: Processing Images with Classes
Chapter 4: Counting the Pixels with Histograms
Chapter 5: Transforming Images with Morphological Operations
Chapter 6: Filtering the Images
Chapter 7: Extracting Lines, Contours, and Components
Chapter 8: Detecting and Matching Interest Points
Chapter 9: Estimating Projective Relations in Images
Chapter 10: Processing Video Sequences
Index
  • Chapter 1: Playing with Images
    • Introduction
    • Installing the OpenCV library
    • Creating an OpenCV project with MS Visual C++
    • Creating an OpenCV project with Qt
    • Loading, displaying, and saving images
    • Creating a GUI application using Qt
    • Chapter 2: Manipulating the Pixels
      • Introduction
      • Accessing pixel values
      • Scanning an image with pointers
      • Scanning an image with iterators
      • Writing efficient image scanning loops
      • Scanning an image with neighbor access
      • Performing simple image arithmetic
      • Defining regions of interest
      • Chapter 3: Processing Images with Classes
        • Introduction
        • Using the Strategy pattern in algorithm design
        • Using a Controller to communicate with processing modules
        • Using the Singleton design pattern
        • Using the Model-View-Controller architecture to design an application
        • Converting color spaces
        • Chapter 4: Counting the Pixels with Histograms
          • Introduction
          • Computing the image histogram
          • Applying look-up tables to modify image appearance
          • Equalizing the image histogram
          • Backprojecting a histogram to detect specific image content
          • Using the mean shift algorithm to find an object
          • Retrieving similar images using histogram comparison
          • Chapter 5: Transforming Images with Morphological Operations
            • Introduction
            • Eroding and dilating images using morphological filters
            • Opening and closing images using morphological filters
            • Detecting edges and corners using morphological filters
            • Segmenting images using watersheds
            • Extracting foreground objects with the GrabCut algorithm
            • Chapter 6: Filtering the Images
              • Introduction
              • Filtering images using low-pass filters
              • Filtering images using a median filter
              • Applying directional filters to detect edges
              • Computing the Laplacian of an image
              • Chapter 7: Extracting Lines, Contours, and Components
                • Introduction
                • Detecting image contours with the Canny operator
                • Detecting lines in images with the Hough transform
                • Fitting a line to a set of points
                • Extracting the components' contours
                • Computing components' shape descriptors
                    • Chapter 10: Processing Video Sequences
                      • Introduction
                      • Reading video sequences
                      • Processing the video frames
                      • Writing video sequences
                      • Tracking feature points in video
                      • Extracting the foreground objects in video

                      Robert Laganière

                      Robert Laganière is a professor at the University of Ottawa, Canada. He received his Ph.D. degree from INRS-Telecommunications in Montreal in 1996. Dr. Laganière is a researcher in computer vision with an interest in video analysis, intelligent visual surveillance, and imagebased modeling. He is a co-founding member of the VIVA research lab. He is also a Chief Scientist at iWatchLife.com, a company offering a cloud-based solution for remote monitoring. Dr. Laganière is the co-author of Object-oriented Software Engineering published by McGraw Hill in 2001. Visit the author's website at http://www.laganiere.name.
                      Sorry, we don't have any reviews for this title yet.

                      Code Downloads

                      Download the code and support files for this book.


                      Submit Errata

                      Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                      Errata

                      - 12 submitted: last submission 13 Feb 2014

                      Errata type: Others | Page number: 44 | Errata date: 03 Aug 2011

                      second paragraph: "number of pixel values per rows" should be "number of pixel values per row" p. 64, 2nd paragraph: "the ROI will determined" s/b "the ROI will be determined" p. 85, 3rd para: "the concept of the concept of" s/b "the concept of" p. 94, 3rd para: "both which are" s/b "both of which are" p. 105 2nd para: "This later is" s/b "This latter is" p. 141 1st para: "theory in details here" s/b "theory in detail here" p. 141 2nd para: "Images differ from each others" s/b "Images differ from each other" p. 169 1st para: "Therefore, do not need" s/b "Therefore, you do not need"

                       

                      Errata type: Others | Page number: 42 | Errata date: 26 July 2011

                      In the second paragraph on the third line (word number 4 on the line) the word 'remainder' is misspelt as 'reminder'.

                       

                      Errata Type: Grammar Page No:37

                      On the second paragraph line number 4 The order of the words is inverted: "... 0 corresponds to black and corresponds 255 to white." should be "... 0 corresponds to black and 255 corresponds to white."

                      Errata Type:Grammar Page No:Multiple

                      p.g 30, second paragraph: "deprecate data structure" should be "deprecated data structure"

                      p.g 44, second paragraph: "number of pixel values per rows" should be "number of pixel values per row"

                      p.g 44, last code sample: "div2" should be "div / 2" p.g 48, third paragraph: "contained into a memory block" should be "contained in a memory block"

                      p.g 50, last paragraph: "specify larger step size" should be "specify a larger step size"

                      p.g 64, 2nd paragraph: "the ROI will determined" s/b "the ROI will be determined"

                      p.g 71, 2nd paragraph: "Vec3d" s/b "Vec3b"

                      p.g 85, 3rd para: "the concept of the concept of" s/b "the concept of"

                      p.g 90 1st para: "such that sum" s/b "such that the sum"

                      p.g 94, 3rd para: "both which are" s/b "both of which are"

                      p.g 95 1st code sample: "BRG" in comment s/b "BGR"

                      p.g 99 1st para: "with less than" s/b "with fewer than"

                      p.g 104 1st para: "consists in replacing" s/b "consists of replacing"

                      p.g 105 1st & 2nd para: "cv::calBackProject" s/b "cv::calcBackProject"

                      p.g 105 2nd para: "This later is" s/b "This latter is"

                      p.g 112 6th para: "proposes few" s/b "proposes a few"

                      p.g 131 1st para: "The left square is then obtained" s/b "The right square is then obtained"

                      p.g 137 2nd para: "The next recipe which presents" s/b "The next recipe presents"

                      p.g 141 1st para: "theory in details here" s/b "theory in detail here"

                      p.g 141 2nd para: "Images differ from each others" s/b "Images differ from each other"

                      p.g 153 2nd para: "variation is and it is normally computed" s/b "variation is and is normally computed"

                      p.g 156 2nd para: "since these ones are by definition" s/b "since these are by definition"

                      p.g 168 & p. 174 first argument to HoughLines should be "contours", not "test"

                      p.g 169 1st para: "Therefore, do not need" s/b "Therefore, you do not need"

                      p.g 175 2nd para: "transform adds few modifications" s/b "transform adds a few modifications"

                      Errata Type:50 Page No:Code

                      If you wish to start at the second row ... image.begin()+image.rows SHOULD be ... image.begin()+image.cols

                      Errata Type: 50 Page No:

                      "image.begin()+image.rows" should be "image.begin()+image.cols" and "end()-image.rows" should be "end()-image.cols"

                      Errata Type: Code Page No:269

                      On the 4th line from below, it appears "processor.etDelayetDelay(...", it should be "processor.setDelay(..."

                      Errata Type: Typo Page No:42

                      In the second paragraph on the third line (word number 4 on the line) the word 'remainder' is misspelt as 'reminder'

                      Errata Type: Code Page No: 184

                      On page 184: a const_iterator is used while a regular iterator should have been used since we are calling the erase method on it. You should make that change in the corresponding source file blobs.cpp.
                      Here the updated file:
                      #include
                      #include
                      #include
                      #include
                      #include
                      int main()
                      {
                      // Read input binary image
                      cv::Mat image= cv::imread("../binaryGroup.bmp",0);
                      if (!image.data)
                      return 0;
                      cv::namedWindow("Binary Image");
                      cv::imshow("Binary Image",image);
                      // Get the contours of the connected components
                      std::vector> contours;
                      cv::findContours(image,
                      contours, // a vector of contours
                      CV_RETR_EXTERNAL, // retrieve the external contours
                      CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
                      // Print contours' length
                      std::cout << "Contours: " << contours.size() << std::endl;
                      std::vector>::const_iterator itContours= contours.begin();
                      for ( ; itContours!=contours.end(); ++itContours) {
                      std::cout << "Size: " << itContours->size() << std::endl;
                      }
                      // draw black contours on white image
                      cv::Mat result(image.size(),CV_8U,cv::Scalar(255));
                      cv::drawContours(result,contours,
                      -1, // draw all contours
                      cv::Scalar(0), // in black
                      2); // with a thickness of 2
                      cv::namedWindow("Contours");
                      cv::imshow("Contours",result);
                      // Eliminate too short or too long contours
                      int cmin= 100; // minimum contour length
                      int cmax= 1000; // maximum contour length
                      std::vector>::iterator itc= contours.begin();
                      while (itc!=contours.end()) {
                      if (itc->size() < cmin || itc->size() > cmax)
                      itc= contours.erase(itc);
                      else
                      ++itc;
                      }
                      // draw contours on the original image
                      cv::Mat original= cv::imread("../group.jpg");
                      cv::drawContours(original,contours,
                      -1, // draw all contours
                      cv::Scalar(255,255,255), // in white
                      2); // with a thickness of 2
                      cv::namedWindow("Contours on Animals");
                      cv::imshow("Contours on Animals",original);
                      // Let's now draw black contours on white image
                      result.setTo(cv::Scalar(255));
                      cv::drawContours(result,contours,
                      -1, // draw all contours
                      cv::Scalar(0), // in black
                      1); // with a thickness of 1
                      image= cv::imread("../binaryGroup.bmp",0);
                      // testing the bounding box
                      cv::Rect r0= cv::boundingRect(cv::Mat(contours[0]));
                      cv::rectangle(result,r0,cv::Scalar(0),2);
                      // testing the enclosing circle
                      float radius;
                      cv::Point2f center;
                      cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);
                      cv::circle(result,cv::Point(center),static_cast(radius),cv::Scalar(0),2);
                      // cv::RotatedRect rrect= cv::fitEllipse(cv::Mat(contours[1]));
                      // cv::ellipse(result,rrect,cv::Scalar(0),2);
                      // testing the approximate polygon
                      std::vector poly;
                      cv::approxPolyDP(cv::Mat(contours[2]),poly,5,true);
                      std::cout << "Polygon size: " << poly.size() << std::endl;
                      // Iterate over each segment and draw it std::vector::const_iterator itp= poly.begin();
                      while (itp!=(poly.end()-1)) {
                      cv::line(result,*itp,*(itp+1),cv::Scalar(0),2);
                      ++itp;
                      } // last point linked to first point
                      cv::line(result,*(poly.begin()),*(poly.end()-1),cv::Scalar(20),2);
                      // testing the convex hull
                      std::vector hull;
                      cv::convexHull(cv::Mat(contours[3]),hull);
                      // Iterate over each segment and draw it
                      std::vector::const_iterator it= hull.begin(); while (it!=(hull.end()-1)) { cv::line(result,*it,*(it+1),cv::Scalar(0),2);
                      ++it;
                      } // last point linked to first point
                      cv::line(result,*(hull.begin()),*(hull.end()-1),cv::Scalar(20),2);
                      // testing the moments
                      // iterate over all contours
                      itc= contours.begin();
                      while (itc!=contours.end()) {
                      // compute all moments
                      cv::Moments mom= cv::moments(cv::Mat(*itc++));
                      // draw mass center
                      cv::circle(result,
                      // position of mass center converted to integer
                      cv::Point(mom.m10/mom.m00,mom.m01/mom.m00),
                      2,cv::Scalar(0),2); // draw black dot
                      }
                      cv::namedWindow("Some Shape descriptors");
                      cv::imshow("Some Shape descriptors",result);
                      // New call to findContours but with CV_RETR_LIST flag
                      image= cv::imread("../binaryGroup.bmp",0);
                      // Get the contours of the connected components cv::findContours(image,
                      contours, // a vector of contours
                      CV_RETR_LIST, // retrieve the external and internal contours
                      CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
                      // draw black contours on white image
                      result.setTo(cv::Scalar(255));
                      cv::drawContours(result,contours ,
                      -1, // draw all contours
                      cv::Scalar(0), // in black
                      2); // with a thickness of 2
                      cv::namedWindow("All Contours");
                      cv::imshow("All Contours",result);
                      cv::waitKey();
                      return 0;
                      }

                      Errata Type: code Chapter 4

                      In Chapter 4, the class ContentFinder.h is not present in the sample code. The solution to this can be found at http://www.laganiere.name/opencvCookbook/

                      Errata type: Typo | Page number: 37

                      The order of the words is inverted: "... 0 corresponds to black and corresponds 255 to white." should be "... 0 corresponds to black and 255 corresponds to white."

                       

                      Errata type: Typo | Page number: 219 | Second paragraph:

                      This relation allows the position of the image of a 3D scene point to be predicted onto the image plane of a camera.

                      should be:

                      This relation allows to predict what will be the position of the image of a 3D scene point onto the image plane of a camera.

                      Sample chapters

                      You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                      Frequently bought together

                      OpenCV 2 Computer Vision Application Programming Cookbook +    OpenGL Development Cookbook =
                      50% Off
                      the second eBook
                      Price for both: £16.14

                      Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                      What you will learn from this book

                      • Create advanced computer vision applications using sound object-oriented programming practices
                      • Iterate over an image to process each of its pixels
                      • Enhance an image or interesting parts of an image using histograms
                      • Use mathematical morphology to process binary images and to segment images into homogenous regions
                      • Filter images by modifying their frequency content
                      • Detect the lines, contours , and objects contained in an image
                      • Apply different interest point operators in order to characterize an image content
                      • Exploit the image geometry in order to match different views of a pictured scene
                      • Calibrate the camera from different image observations
                      • Reconstruct selected image elements in 3D

                      In Detail

                      In today's digital world, images are everywhere, and with the advent of powerful and affordable computing devices, it has become possible to create sophisticated applications manipulating images and videos. Adding special effects, enhancing image features, performing object recognition, and reconstructing 3D information are tasks that can be programmed easily with the OpenCV library, which is a widely used open source library that offers a rich set of advanced computer vision algorithms.

                      OpenCV 2 Computer Vision Application Programming Cookbook will introduce you to numerous computer vision algorithms included in the OpenCV library. You will learn how to read, write, create and manipulate images. You will explore different techniques commonly used in image analysis and how they can be effectively implemented in C++. The book provides a complete introduction to the OpenCV library and explains how to build your first computer vision program. You will be presented with a variety of computer vision algorithms and be exposed to important concepts in image analysis that will enable you to build your own computer vision applications.

                      The book helps you to get started with the library, showing you how to install and deploy the OpenCV library to write effective computer vision applications following good programming practices. The techniques to process an image and its pixels using the data structures offered by the library are explained in detail. You will learn how to build and manipulate an image histogram; how to detect lines and contours. You will be introduced to the concept of mathematical morphology and image filtering. The detection and use of interest points in computer vision is presented with applications for image matching and object recognition. Techniques to achieve camera calibration and 3D reconstruction are presented.

                      OpenCV 2 Computer Vision Application Programming Cookbook is your guide to the development of computer vision applications. It is a comprehensive reference that exposes you to computer vision concepts illustrated with extensive examples.

                      A step-by-step guide to computer vision programming using the C++ Interface of the OpenCV 2 library including advanced concepts

                      Approach

                      This is a cookbook that shows results obtained on real images with detailed explanations and the relevant screenshots. The recipes contain code accompanied with suitable explanations that will facilitate your learning.

                      Who this book is for

                      If you are a novice C++ programmer who wants to learn how to use the OpenCV library to build computer vision applications, then this cookbook is appropriate for you. It is also suitable for professional software developers wishing to be introduced to the concepts of computer vision programming. It can be used as a companion book in university-level computer vision courses. It constitutes an excellent reference for graduate students and researchers in image processing and computer vision. The book provides a good combination of basic to advanced recipes. Basic knowledge of C++ is required.

                      Code Download and Errata
                      Packt Anytime, Anywhere
                      Register Books
                      Print Upgrades
                      eBook Downloads
                      Video Support
                      Contact Us
                      Awards Voting Nominations Previous Winners
                      Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                      Resources
                      Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software