Subversion Repositories OpenCV2-Cookbook

Rev

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 CD_CNTRLLR
19
#define CD_CNTRLLR
20
 
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/highgui/highgui.hpp>
23
#include "colordetector.h"
24
 
25
class ColorDetectController {
26
 
27
  private:
28
 
29
        static ColorDetectController *singleton; // pointer to the singleton
30
 
31
        ColorDetector *cdetect;
32
 
33
        // The image to be processed
34
        cv::Mat image;
35
        cv::Mat result;
36
 
37
  public:
38
        ColorDetectController() { // private constructor
39
 
40
                  //setting up the application
41
                  cdetect= new ColorDetector();
42
        }
43
 
44
          // Sets the color distance threshold
45
          void setColorDistanceThreshold(int distance) {
46
 
47
                  cdetect->setColorDistanceThreshold(distance);
48
          }
49
 
50
          // Gets the color distance threshold
51
          int getColorDistanceThreshold() const {
52
 
53
                  return cdetect->getColorDistanceThreshold();
54
          }
55
 
56
          // Sets the color to be detected
57
          void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {
58
 
59
                  cdetect->setTargetColor(red,green,blue);
60
          }
61
 
62
          // Gets the color to be detected
63
          void getTargetColor(unsigned char &red, unsigned char &green, unsigned char &blue) const {
64
 
65
                  cv::Vec3b color= cdetect->getTargetColor();
66
 
67
                  red= color[2];
68
                  green= color[1];
69
                  blue= color[0];
70
          }
71
 
72
          // Sets the input image. Reads it from file.
73
          bool setInputImage(std::string filename) {
74
 
75
                  image= cv::imread(filename);
76
 
77
                  if (!image.data)
78
                          return false;
79
                  else
80
                          return true;
81
          }
82
 
83
          // Returns the current input image.
84
          const cv::Mat getInputImage() const {
85
 
86
                  return image;
87
          }
88
 
89
          // Performs image processing.
90
          void process() {
91
 
92
                  result= cdetect->process(image);
93
          }
94
 
95
 
96
          // Returns the image result from the latest processing.
97
          const cv::Mat getLastResult() const {
98
 
99
                  return result;
100
          }
101
 
102
          // Deletes all processor objects created by the controller.
103
          ~ColorDetectController() {
104
 
105
                  delete cdetect;
106
          }
107
 
108
          // Singleton static members
109
          static ColorDetectController *getInstance() {
110
 
111
                  if (singleton == 0)
112
                        singleton= new ColorDetectController;
113
 
114
                  return singleton;
115
          }
116
 
117
          // Releases the singleton instance of this controller.
118
          static void destroy() {
119
 
120
                  if (singleton != 0) {
121
                          delete singleton;
122
                          singleton= 0;
123
                  }
124
          }
125
};
126
 
127
#endif