Controller//控制器

#include<opencv2corecore.hpp>
#include<opencv2imgprocimgproc.hpp>
#include<opencv2highguihighgui.hpp>
#include<iostream>


using namespace std;
using namespace cv;




class ColorDetector
{
public:
ColorDetector():minDist(100)
{
target[0]=target[1]=target[0]=0;
};
void setColorDistanceThreshold(int distance)
{
if(distance<0)
distance=0;
minDist=distance;
};
int getColorDistanceThreshold() const
{
return minDist;
};
void setTargetColor(unsigned char red,
                   unsigned char green,
unsigned char blue)
{
target[0]=blue;
target[1]=green;
target[2]=red;
};
void setTargetColor(cv::Vec3b color)
{
target=color;
};
cv::Vec3b getTargetColor() const
{
return target;
};
int getDistance(const cv::Vec3b& color) const
{
return abs(color[0]-target[0])+abs(color[1]-target[1])+abs(color[2]-target[2]);
};
cv::Mat process(const cv::Mat &image);//核心算法。在类外实现
private:
int minDist;
cv::Vec3b target;
cv::Mat result;
};
cv::Mat ColorDetector::process(const cv::Mat &image)
{
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;
}
}
return result;
}
class ColorDetectController//控制器
{
private:
ColorDetector *cdetect;
cv::Mat image;
cv::Mat result;
public:
ColorDetectController()
{
cdetect=new ColorDetector();
};
void setColorDistanceThreshold(int distance)
{
cdetect->setColorDistanceThreshold(distance);
};
int getColorDistanceThreshold() const
{
return cdetect->getColorDistanceThreshold();
};
void setTargetColor(unsigned char red,unsigned char green,unsigned char blue)
{
cdetect->setTargetColor(red,green,blue);
};
void getTargetColor(unsigned char &red,unsigned char &green,unsigned char &blue)const
{
cv::Vec3b color=cdetect->getTargetColor();
red=color[2];
green=color[1];
blue=color[0];
};
bool setInputImage(std::string filename)
{
image=cv::imread(filename);
if(!image.data)
return false;
else
return true;
};
const cv::Mat getInputImage()const
{
return image;
};
void process()
{
result=cdetect->process(image);
};
const cv::Mat getLastResult() const
{
return result;
};
~ColorDetectController()
{
delete cdetect;
};
}
原文地址:https://www.cnblogs.com/claireyuancy/p/6801258.html