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