【OpenCV】OpenCV创建颜色识别类-class ColorDetector
来源:互联网 发布:南昌金域名都租房 编辑:程序博客网 时间:2024/05/20 14:42
将其构造函数声明为private,提供静态的接口来获得ColorDetector对象
void setColorDistanceThreshold(int) 用于设置阈值
void setTargetColor(unsigned char, unsigned char, unsigned char)
void setTargetColor(cv::Vec3b) 用于设置颜色
bool setInputImage(std::string) 用于载入待处理图像
cv::Mat getResult() const 用于返回处理结果,结果用一副图像表示
#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(100){ target[0] = target[1] = target[2] = 0;}ColorDetector* ColorDetector::getInstance(){ if(singleton == 0){ singleton = new ColorDetector; } return singleton;}void ColorDetector::destory(){ 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){ target[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]-target[1])+abs(color[2]-target[2]);}void ColorDetector::process(){ 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;}
0 0
- 【OpenCV】OpenCV创建颜色识别类-class ColorDetector
- opencv 颜色识别
- OpenCV颜色识别
- OpenCV颜色识别
- OpenCV颜色识别
- OpenCV颜色识别
- opencv 颜色识别
- Opencv颜色识别
- OpenCV颜色识别
- opencv颜色识别
- opencv颜色识别一
- OPENCV颜色识别
- opencv 颜色识别
- Java+Opencv 颜色识别
- OpenCV颜色识别
- OpenCV颜色识别
- OpenCV颜色识别
- RaspiCam & opencv 单一颜色识别
- ios gpuimage输入和得到原始数据
- iOS开发之手势解锁详解
- Activity生命周期
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- WPS 设置默认标题格式,段前段后间距
- 【OpenCV】OpenCV创建颜色识别类-class ColorDetector
- Fragment和Activity间的通讯的实现
- OpenLayers类库的
- 成为了ACMer,我有了梦想
- C++ 设定图像最大边长度并原比例压缩图片
- mysql数据库体系结构——存储过程
- 使用golang抓取京东全部商品分类信息
- drawerLayout
- jquery获取html元素的绝对位置和相对位置的方法