【发布时间】:2016-11-03 12:21:38
【问题描述】:
我想将现有的颜色检测从红色转换为灰色。我从this project(火焰检测系统)获取代码
我尝试实现自己的算法,但我认为我离我想要实现的目标还很远。我从this link得到算法
以下是原始代码片段,稍作修改:
void TargetExtractor::colorDetect(int redThreshold, double saturationThreshold) {
Mat temp;
GaussianBlur(mFrame, temp, Size(3, 3), 0);
uchar grayThreshold = 80;
for (int i = 0; i < temp.rows; i++) {
for (int j = 0; j < temp.cols; j++) {
if (mMask.at<uchar>(i, j) == 255) {
Vec3b& v = temp.at<Vec3b>(i, j);
uchar b = v[0];
uchar g = v[1];
uchar r = v[2];
//if (abs(r - g) < grayThreshold) {
// mMask.at<uchar>(i, j) = 0;
//}
double s = 1 - 3.0 * min(b, min(g, r)) / (b + g + r);
if (!(r > redThreshold && r >= g && g > b &&
s >= ((255 - r) * saturationThreshold / redThreshold))) {
mMask.at<uchar>(i, j) = 0;
}
}
}
}
}
评论部分是我尝试检测灰色区域,但它肯定不适合我。
从原始代码中检测移动的红色物体:
检测移动的灰色物体:
【问题讨论】:
标签: c++ opencv color-detection