Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 3 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
5 PointedEar 18
#include "color_detector/colordetector.h"
19
#include <opencv2/imgproc/imgproc.hpp>
3 PointedEar 20
 
21
cv::Mat ColorDetector::process(const cv::Mat &image) {
22
 
23
          // re-allocate binary map if necessary
24
          // same size as input image, but 1-channel
25
          result.create(image.rows,image.cols,CV_8U);
26
 
27
          // re-allocate intermediate image if necessary
28
          converted.create(image.rows,image.cols,image.type());
29
 
30
          // Converting to Lab color space 
31
          cv::cvtColor(image, converted, CV_BGR2Lab);
32
 
33
          // get the iterators
34
          cv::Mat_<cv::Vec3b>::iterator it= converted.begin<cv::Vec3b>();
35
          cv::Mat_<cv::Vec3b>::iterator itend= converted.end<cv::Vec3b>();
36
          cv::Mat_<uchar>::iterator itout= result.begin<uchar>();
37
 
38
          // for each pixel
39
          for ( ; it!= itend; ++it, ++itout) {
40
 
41
                // process each pixel ---------------------
42
 
43
                  // compute distance from target color
44
                  if (getDistance(*it)<minDist) {
45
 
46
                          *itout= 255;
47
 
48
                  } else {
49
 
50
                          *itout= 0;
51
                  }
52
 
53
        // end of pixel processing ----------------
54
          }
55
 
56
          return result;
57
}
58