OpenCV OpenCV Create a color recognition class-class COLORDETECTOR__OPENCV

Source: Internet
Author: User
Tags abs

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;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.