Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
1
/*------------------------------------------------------------------------------------------*\
1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 3 of the cookbook:  
2
   This file contains material supporting chapter 3 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
4
   by Robert Laganiere, Packt Publishing, 2011.
5

5

6
   This program is free software; permission is hereby granted to use, copy, modify,
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,
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
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
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.
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.
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,
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.
13
   and any consequent failure, is purely the responsibility of the user.
14
 
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
16
\*------------------------------------------------------------------------------------------*/
17
17
18
#include "colordetector.h"
18
#include "color_detector/colordetector.h"
-
 
19
#include <opencv2/imgproc/imgproc.hpp>
19
       
20
       
20
cv::Mat ColorDetector::process(const cv::Mat &image) {
21
cv::Mat ColorDetector::process(const cv::Mat &image) {
21
       
22
       
22
          // re-allocate binary map if necessary
23
          // re-allocate binary map if necessary
23
          // same size as input image, but 1-channel
24
          // same size as input image, but 1-channel
24
          result.create(image.rows,image.cols,CV_8U);
25
          result.create(image.rows,image.cols,CV_8U);
25
26
26
          // re-allocate intermediate image if necessary
27
          // re-allocate intermediate image if necessary
27
          converted.create(image.rows,image.cols,image.type());
28
          converted.create(image.rows,image.cols,image.type());
28
29
29
          // Converting to Lab color space 
30
          // Converting to Lab color space 
30
          cv::cvtColor(image, converted, CV_BGR2Lab);
31
          cv::cvtColor(image, converted, CV_BGR2Lab);
31
32
32
          // get the iterators
33
          // get the iterators
33
          cv::Mat_<cv::Vec3b>::iterator it= converted.begin<cv::Vec3b>();
34
          cv::Mat_<cv::Vec3b>::iterator it= converted.begin<cv::Vec3b>();
34
          cv::Mat_<cv::Vec3b>::iterator itend= converted.end<cv::Vec3b>();
35
          cv::Mat_<cv::Vec3b>::iterator itend= converted.end<cv::Vec3b>();
35
          cv::Mat_<uchar>::iterator itout= result.begin<uchar>();
36
          cv::Mat_<uchar>::iterator itout= result.begin<uchar>();
36
37
37
          // for each pixel
38
          // for each pixel
38
          for ( ; it!= itend; ++it, ++itout) {
39
          for ( ; it!= itend; ++it, ++itout) {
39
       
40
       
40
                // process each pixel ---------------------
41
                // process each pixel ---------------------
41
42
42
                  // compute distance from target color
43
                  // compute distance from target color
43
                  if (getDistance(*it)<minDist) {
44
                  if (getDistance(*it)<minDist) {
44
45
45
                          *itout= 255;
46
                          *itout= 255;
46
47
47
                  } else {
48
                  } else {
48
49
49
                          *itout= 0;
50
                          *itout= 0;
50
                  }
51
                  }
51
52
52
        // end of pixel processing ----------------
53
        // end of pixel processing ----------------
53
          }
54
          }
54
55
55
          return result;
56
          return result;
56
}
57
}
57
58
58
 
59