1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#if !defined COLORDETECT
#define COLORDETECT
#include <opencv2/opencv.hpp>
class ColorDetector {
private:
// minimum acceptable distance
int minDist;
// target color
cv::Vec3b target;
// image containing resulting binary map
cv::Mat result;
// image containing color converted image
cv::Mat converted;
// inline private member function
// Computes the distance from target color.
int getDistance(const cv::Vec3b& color) const {
// return static_cast<int>(cv::norm<int,3>(cv::Vec3i(color[0]-target[0],color[1]-target[1],color[2]-target[2])));
return abs(color[0]-target[0])+
abs(color[1]-target[1])+
abs(color[2]-target[2]);
}
public:
// empty constructor
ColorDetector() : minDist(100) {
// default parameter initialization here
target[0]= target[1]= target[2]= 0;
}
// Getters and setters
// Sets the color distance threshold.
// Threshold must be positive, otherwise distance threshold
// is set to 0.
void setColorDistanceThreshold(int distance) {
if (distance<0)
distance=0;
minDist= distance;
}
// Gets the color distance threshold
int getColorDistanceThreshold() const {
return minDist;
}
// Sets the color to be detected
void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {
cv::Mat tmp(1,1,CV_8UC3);
tmp.at<cv::Vec3b>(0,0)[0]= blue;
tmp.at<cv::Vec3b>(0,0)[1]= green;
tmp.at<cv::Vec3b>(0,0)[2]= red;
// Converting the target to Lab color space
cv::cvtColor(tmp, tmp, CV_BGR2Lab);
target= tmp.at<cv::Vec3b>(0,0);
}
// Sets the color to be detected
void setTargetColor(cv::Vec3b color) {
cv::Mat tmp(1,1,CV_8UC3);
tmp.at<cv::Vec3b>(0,0)= color;
// Converting the target to Lab color space
cv::cvtColor(tmp, tmp, CV_BGR2Lab);
target= tmp.at<cv::Vec3b>(0,0);
}
// Gets the color to be detected
cv::Vec3b getTargetColor() const {
return target;
}
// Processes the image. Returns a 1-channel binary image.
cv::Mat process(const cv::Mat &image);
};
#endif |
Partager