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