前言
最近在研究桂电的代码,感觉挺有东西的,浅浅记录一下
1.图像预处理
1.1 图像二值化
1.1.1 红色
//图像二值化
cv::split(_src, splitSrc); //分离色彩通道
cv::cvtColor(_src, _graySrc, cv::COLOR_BGR2GRAY); //获取灰度图
cv::threshold(_graySrc, _separationSrcWhite, 240, 255, cv::THRESH_BINARY);
cv::bitwise_not(_separationSrcWhite, _separationSrcWhite);
if (enemyColor == ENEMY_RED) {
//敌方为红色
cv::threshold(_graySrc, _graySrc, _para.grayThreshold_RED, 255, cv::THRESH_BINARY); //灰度二值化
cv::subtract(splitSrc[2], splitSrc[0], _separationSrc); //红蓝通道相减
cv::subtract(splitSrc[2], splitSrc[1], _separationSrcGreen); //红绿通道相减
cv::threshold(_separationSrc, _separationSrc, _para.separationThreshold_RED, 255, cv::THRESH_BINARY); //红蓝二值化
cv::threshold(_separationSrcGreen, _separationSrcGreen, _para.separationThreshold_GREEN, 255, cv::THRESH_BINARY);//红绿二值化
cv::dilate(_separationSrc, _separationSrc, Util::structuringElement3());
cv::dilate(_separationSrcGreen, _separationSrcGreen, Util::structuringElement3()); //膨胀
_maxColor = _separationSrc & _graySrc & _separationSrcGreen & _separationSrcWhite; //逻辑与获得最终二值化图像
cv::dilate(_maxColor, _maxColor, Util::structuringElement3()); //膨胀
//cv::morphologyEx(_maxColor, _maxColor, cv::MORPH_OPEN, Util::structuringElement3());
}
最终得到的图像是 _maxColor = _separationSrc & _graySrc & _separationSrcGreen & _separationSrcWhite
以下四个二值化得到的
cv::threshold(_graySrc, _separationSrcWhite, 240, 255, cv::THRESH_BINARY);
//灰度二值化
cv::threshold(_graySrc, _graySrc, _para.grayThreshold_RED, 255, cv::THRESH_BINARY);
//红蓝二值化
cv::threshold(_separationSrc, _separationSrc, _para.separationThreshold_RED, 255, cv::THRESH_BINARY);
//红绿二值化
cv::threshold(_separationSrcGreen, _separationSrcGreen, _para.separationThreshold_GREEN, 255, cv::THRESH_BINARY);
1.1.2 蓝色
else {
cv::threshold(splitSrc[2], _purpleSrc, _para.grayThreshold_PURPLE, 255, cv::THRESH_BINARY); //防止误识别紫色基地
cv::bitwise_not(_purpleSrc, _purpleSrc);
//敌方为蓝色
......
}
2 找到轮廓findLightBarContour
这部分算是比较中规中矩
cv::RotatedRect scanRect = cv::minAreaRect(allContours[i]);//检测最小面积的矩形
cv::Point2f vertices[4];
scanRect.points(vertices);
if (fabs(vertices[1].x - vertices[3].x) > fabs(vertices[1].y - vertices[3].y))
continue;
其实是使用对角线上的点
【参考文档】minAreaRect方法生成检测对象的最小外接矩形
3.寻找攻击目标
#ifdef USE_BP
//net = cv::dnn::readNetFromCaffe("../trainData/deploy.prototxt", "../trainData/_iter_2000.caffemodel");
_net.load("../trainData/model_sigmoid_800_200.xml");
#else
一共是做了两套方案
①使用caffe
②使用pd
完整代码很长但是实际上可以归纳为两步:
(1)检查当前装甲板是否为工程(2代表工程)
(2)使用caffe或者pd进行判断
3.1使用caffe进行识别
3.1.2. deploy.prototxt
实际上是LeNet的配置文件
重点关注第一层和最后一层
第一层:
net.setInput(inputBlob, "data");
最后一层:
inputBlob = net.forward("loss");
3.1.3
相当于将最后一层经过softmax的结果展平
inputBlob = inputBlob.reshape(1, 1);//展平
Point classNumber;
cv::minMaxLoc(inputBlob, NULL, &classProb, NULL, &classNumber);//取最大值作为物体
classId = classNumber.x; // 类别:0是45度,1是装甲板
【minMaxLoc参考博文】OpenCV 找出图像中最小值最大值函数minMaxLoc
训练的时候,我猜想应该是将装甲板和45度角两条灯条匹配成装甲板的情况进行标注
3.1.4 代码
#if USE_CAFFE
//Mat inputBlob = dnn::blobFromImage(resizeImg, 0.00390625f, Size(20, 20), Scalar(), false);
Mat inputBlob = dnn::blobFromImage(resizeImg, 1, Size(20, 20), Scalar(), false);
net.setInput(inputBlob, "data");
inputBlob = net.forward("loss");
int classId;
double classProb;
inputBlob = inputBlob.reshape(1, 1);
Point classNumber;
cv::minMaxLoc(inputBlob, NULL, &classProb, NULL, &classNumber);
classId = classNumber.x; // 类别:0是45度,1是装甲板
// LOG::info("case of armor: " + to_string(classId) + " accuracy: " + to_string(classProb * 100));
if (classId == 0 || classProb < 0.6) {
_sampleData.classifyState = false;
armorStructs.erase(it);
continue;
}
3.2 使用bp神经网络
3.2.1 bp神经网络的概念
- 定义:是一种按误差逆传播算法训练的多层前馈网络
传播的对象是误差,传播的目的是得到所有层的估计误差,后向是说由后层误差推导前层误差
【参考文档】Bp神经网络
4.选择最终攻击目标
同样也是做了两套方案
①对ID进行攻击序列排序
②根据权重
4.1 findArmorByWeight根据权重
void ArmorDistinguish::findArmorByWeight(std::vector<ArmorStruct>& armorStructs, cv::RotatedRect& resultRect, ArmorRectHistoricalDataList& armorRectData) {
//float aver = 0.0f;
for (size_t i = 0; i < armorStructs.size(); ++i) {
对于每一个可能的装甲板更新armorRectData中的xxx ChangeRate
}
//找到权重最大的装甲板
maxWeightNum = findVarianceMaxWeight(_armorRectHistoricalData, armorRectData, _maxWeightValue);
resultRect = armorStructs[maxWeightNum].armorRect;
//判断目标装甲板类型
_armorType = armorStructs[maxWeightNum].armorType;
_leftLightBar = armorStructs[0].partLightBars[0];
_rightLightBar = armorStructs[0].partLightBars[1];
_nowCarID = armorStructs[maxWeightNum].carId;
}
(1)维护ArmorRectHistoricalDataList
(2)根据 各种变化率(面积,x,y,高宽等)决定权重