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
 
18
#if !defined COLORDETECT
19
#define COLORDETECT
20
 
21
#include <opencv2/core/core.hpp>
5 PointedEar 22
#include <opencv2/imgproc/imgproc.hpp>
3 PointedEar 23
 
24
class ColorDetector {
25
 
26
  private:
27
 
28
          // minimum acceptable distance
29
          int minDist;
30
 
31
          // target color
32
          cv::Vec3b target;
33
 
34
          // image containing resulting binary map
35
          cv::Mat result;
36
 
37
          // image containing color converted image
38
          cv::Mat converted;
39
 
40
          // inline private member function
41
          // Computes the distance from target color.
42
          int getDistance(const cv::Vec3b& color) const {
43
                 // return static_cast<int>(cv::norm<int,3>(cv::Vec3i(color[0]-target[0],color[1]-target[1],color[2]-target[2])));
44
                  return abs(color[0]-target[0])+
45
                                        abs(color[1]-target[1])+
46
                                        abs(color[2]-target[2]);
47
          }
48
 
49
  public:
50
 
51
          // empty constructor
52
          ColorDetector() : minDist(100) {
53
 
54
                  // default parameter initialization here
55
                  target[0]= target[1]= target[2]= 0;
56
          }
57
 
58
          // Getters and setters
59
 
60
          // Sets the color distance threshold.
61
          // Threshold must be positive, otherwise distance threshold
62
          // is set to 0.
63
          void setColorDistanceThreshold(int distance) {
64
 
65
                  if (distance<0)
66
                          distance=0;
67
                  minDist= distance;
68
          }
69
 
70
          // Gets the color distance threshold
71
          int getColorDistanceThreshold() const {
72
 
73
                  return minDist;
74
          }
75
 
76
          // Sets the color to be detected
77
          void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {
78
 
79
                  cv::Mat tmp(1,1,CV_8UC3);
80
          tmp.at<cv::Vec3b>(0,0)[0]= blue;
81
          tmp.at<cv::Vec3b>(0,0)[1]= green;
82
          tmp.at<cv::Vec3b>(0,0)[2]= red;
83
 
84
              // Converting the target to Lab color space 
85
              cv::cvtColor(tmp, tmp, CV_BGR2Lab);
86
 
87
          target= tmp.at<cv::Vec3b>(0,0);
88
          }
89
 
90
          // Sets the color to be detected
91
          void setTargetColor(cv::Vec3b color) {
92
 
93
                  cv::Mat tmp(1,1,CV_8UC3);
94
          tmp.at<cv::Vec3b>(0,0)= color;
95
 
96
              // Converting the target to Lab color space 
97
              cv::cvtColor(tmp, tmp, CV_BGR2Lab);
98
 
99
          target= tmp.at<cv::Vec3b>(0,0);
100
          }
101
 
102
          // Gets the color to be detected
103
          cv::Vec3b getTargetColor() const {
104
 
105
                  return target;
106
          }
107
 
108
          // Processes the image. Returns a 1-channel binary image.
109
          cv::Mat process(const cv::Mat &image);
110
};
111
 
112
 
113
#endif