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 | // image containing color converted image |
||
37 | cv::Mat converted; |
||
38 | |||
39 | // inline private member function |
||
40 | // Computes the distance from target color. |
||
41 | 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 abs(color[0]-target[0])+ |
||
44 | abs(color[1]-target[1])+ |
||
45 | abs(color[2]-target[2]); |
||
46 | } |
||
47 | |||
48 | public: |
||
49 | |||
50 | // empty constructor |
||
51 | ColorDetector() : minDist(100) { |
||
52 | |||
53 | // default parameter initialization here |
||
54 | target[0]= target[1]= target[2]= 0; |
||
55 | } |
||
56 | |||
57 | // Getters and setters |
||
58 | |||
59 | // Sets the color distance threshold. |
||
60 | // Threshold must be positive, otherwise distance threshold |
||
61 | // is set to 0. |
||
62 | void setColorDistanceThreshold(int distance) { |
||
63 | |||
64 | if (distance<0) |
||
65 | distance=0; |
||
66 | minDist= distance; |
||
67 | } |
||
68 | |||
69 | // Gets the color distance threshold |
||
70 | int getColorDistanceThreshold() const { |
||
71 | |||
72 | return minDist; |
||
73 | } |
||
74 | |||
75 | // Sets the color to be detected |
||
76 | void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) { |
||
77 | |||
78 | cv::Mat tmp(1,1,CV_8UC3); |
||
79 | tmp.at<cv::Vec3b>(0,0)[0]= blue; |
||
80 | tmp.at<cv::Vec3b>(0,0)[1]= green; |
||
81 | tmp.at<cv::Vec3b>(0,0)[2]= red; |
||
82 | |||
83 | // Converting the target to Lab color space |
||
84 | cv::cvtColor(tmp, tmp, CV_BGR2Lab); |
||
85 | |||
86 | target= tmp.at<cv::Vec3b>(0,0); |
||
87 | } |
||
88 | |||
89 | // Sets the color to be detected |
||
90 | void setTargetColor(cv::Vec3b color) { |
||
91 | |||
92 | cv::Mat tmp(1,1,CV_8UC3); |
||
93 | tmp.at<cv::Vec3b>(0,0)= color; |
||
94 | |||
95 | // Converting the target to Lab color space |
||
96 | cv::cvtColor(tmp, tmp, CV_BGR2Lab); |
||
97 | |||
98 | target= tmp.at<cv::Vec3b>(0,0); |
||
99 | } |
||
100 | |||
101 | // Gets the color to be detected |
||
102 | cv::Vec3b getTargetColor() const { |
||
103 | |||
104 | return target; |
||
105 | } |
||
106 | |||
107 | // Processes the image. Returns a 1-channel binary image. |
||
108 | cv::Mat process(const cv::Mat &image); |
||
109 | }; |
||
110 | |||
111 | |||
112 | #endif |