Declare its constructor private and provide a static interface to get the Colordetector object
void Setcolordistancethreshold (int) to set thresholds
void Settargetcolor (unsigned char, unsigned char, unsigned char)
void Settargetcolor (CV::VEC3B) for setting colors
BOOL Setinputimage (std::string) for loading pending images
The Cv::mat GetResult () const is used to return the processing result, and the result is represented by a pair of images
#ifndef colordetector_h_
#define Colordetector_h_
#include <opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include <string>
class colordetector{
private:
int mindist ;
CV::VEC3B Target;
Cv::mat result;
Cv::mat image;
Colordetector ();
Static Colordetector *singleton;
Public:
Static Colordetector * getinstance ();
static void Destory ();
void Setcolordistancethreshold (int);
int getcolordistancethreshold () const;
void Settargetcolor (unsigned char, unsigned char, unsigned char);
void Settargetcolor (CV::VEC3B);
CV::VEC3B Gettargetcolor () const;
void process ();
int getdistance (const cv::vec3b&) const;
Cv::mat GetResult () const;
BOOL Setinputimage (std::string);
Cv::mat getinputimage () const;
#endif/* Colordetector_h_ * *
#include "ColorDetector.h" colordetector* Colordetector::singleton = 0;
Colordetector::colordetector (): mindist (MB) {target[0] = target[1] = target[2] = 0;}
colordetector* colordetector::getinstance () {if (singleton = 0) {singleton = new Colordetector;
return singleton;
} void Colordetector::d estory () {if (singleton!=0) {Delete singleton;
} singleton = 0;
} void Colordetector::setcolordistancethreshold (int distance) {if (distance < 0) {distance = 0;
} mindist = distance;
int Colordetector::getcolordistancethreshold () const{return mindist;} void Colordetector::settargetcolor (unsigned char red, unsigned char green, unsigned char blue) {TA
RGET[2] = red;
TARGET[1] = green;
Target[0] = blue;
} void Colordetector::settargetcolor (cv::vec3b color) {target = color;} Cv::vec3b Colordetector::gettaRgetcolor () const{return target; int colordetector::getdistance (const cv::vec3b& color) const{return abs (Color[0]-target[0]) +abs (color[1]-targ
ET[1]) +abs (color[2]-target[2]);
} void Colordetector::p rocess () {result.create (image.rows, Image.cols, cv_8u);
Cv::mat_<cv::vec3b>::const_iterator it = image.begin<cv::vec3b> ();
Cv::mat_<cv::vec3b>::const_iterator itend = image.end<cv::vec3b> ();
Cv::mat_<uchar>::iterator itout = result.begin<uchar> ();
for (; it!=itend; ++it, ++itout) {if (Getdistance (*it) < mindist) {*itout = 255;
}else{*itout = 0;
}} Cv::mat Colordetector::getresult () const{return result;}
BOOL Colordetector::setinputimage (std::string filename) {image = Cv::imread (filename);
if (!image.data) {return false; } return true; Cv::mat colordetector::getinputimage () const{return image;}