整理代码

This commit is contained in:
xinyang
2019-08-14 11:48:41 +08:00
parent 9d94de939b
commit 20d95b3f81
11 changed files with 124 additions and 91 deletions

View File

@@ -32,6 +32,11 @@ static systime getFrontTime(const vector<systime> time_seq, const vector<float>
void ArmorFinder::antiTop() {
if (target_box.rect == cv::Rect2d()) return;
// 判断是否发生装甲目标切换。
// 记录切换前一段时间目标装甲的角度和时间
// 通过线性拟合计算出角度为0时对应的时间点
// 通过两次装甲角度为零的时间差计算陀螺旋转周期
// 根据旋转周期计算下一次装甲出现在角度为零的时间点
if (getPointLength(last_box.getCenter() - target_box.getCenter()) > last_box.rect.height * 1.5) {
auto front_time = getFrontTime(time_seq, angle_seq);
auto once_periodms = getTimeIntervalms(front_time, last_front_time);
@@ -39,7 +44,6 @@ void ArmorFinder::antiTop() {
// sendBoxPosition(0);
// return;
// }
LOGM(STR_CTR(WORD_GREEN, "Top period: %.1lf"), once_periodms);
top_periodms.push(once_periodms);
auto periodms = mean(top_periodms);

View File

@@ -58,7 +58,7 @@ ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, const string &paras_folder,
}
void ArmorFinder::run(cv::Mat &src) {
getsystime(frame_time);
getsystime(frame_time); // 获取当前帧时间(不是足够精确)
// stateSearchingTarget(src); // for debug
// goto end;
switch (state) {
@@ -87,10 +87,10 @@ void ArmorFinder::run(cv::Mat &src) {
break;
case STANDBY_STATE:
default:
stateStandBy();
stateStandBy(); // currently meaningless
}
end:
if(is_anti_top) {
if(is_anti_top) { // 判断当前是否为反陀螺模式
antiTop();
}else if(target_box.rect != cv::Rect2d()) {
anti_top_cnt = 0;

View File

@@ -1,6 +1,7 @@
//
// Created by xinyang on 19-4-19.
//
// 对本文件的大致描述请看classifier.h
//#define LOG_LEVEL LOG_NONE
#include <armor_finder/classifier/classifier.h>
@@ -321,7 +322,6 @@ int Classifier::operator()(const cv::Mat &image) {
vector<MatrixXd> sub = {b, g, r};
vector<vector<MatrixXd>> in = {sub};
MatrixXd result = calculate(in);
// cout << result << "==============" <<endl;
MatrixXd::Index minRow, minCol;
result.maxCoeff(&minRow, &minCol);
if(result(minRow, minCol) > 0.50){

View File

@@ -8,13 +8,13 @@
#include <log.h>
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
if (findArmorBox(src, target_box)) {
if (findArmorBox(src, target_box)) { // 在原图中寻找目标,并返回是否找到
if (last_box.rect != cv::Rect2d() &&
(getPointLength(last_box.getCenter() - target_box.getCenter()) > last_box.rect.height * 2.0) &&
anti_switch_cnt++ < 3) {
target_box = ArmorBox();
LOGM("anti-switch!");
return false;
anti_switch_cnt++ < 3) { // 判断当前目标和上次有效目标是否为同一个目标
target_box = ArmorBox(); // 并给3帧的时间,试图找到相同目标
LOGM("anti-switch!"); // 即刚发生目标切换内的3帧内不发送目标位置
return false; // 可以一定程度避免频繁多目标切换
} else {
anti_switch_cnt = 0;
return true;

View File

@@ -9,7 +9,7 @@
bool ArmorFinder::stateTrackingTarget(cv::Mat &src) {
auto pos = target_box.rect;
if(!tracker->update(src, pos)){
if(!tracker->update(src, pos)){ // 使用KCFTracker进行追踪
target_box = ArmorBox();
LOGW("Track fail!");
return false;
@@ -20,23 +20,20 @@ bool ArmorFinder::stateTrackingTarget(cv::Mat &src) {
return false;
}
// 获取相较于追踪区域两倍长款的区域,用于重新搜索,获取灯条信息
cv::Rect2d bigger_rect;
bigger_rect.x = pos.x - pos.width / 2.0;
bigger_rect.y = pos.y - pos.height / 2.0;
bigger_rect.height = pos.height * 2;
bigger_rect.width = pos.width * 2;
bigger_rect &= cv::Rect2d(0, 0, 640, 480);
// if(show_armor_box)
// showTrackSearchingPos("track", src, bigger_rect);
cv::Mat roi = src(bigger_rect).clone();
ArmorBox box;
if(findArmorBox(roi, box)) {
// 在区域内重新搜索。
if(findArmorBox(roi, box)) { // 如果成功获取目标,则利用搜索区域重新更新追踪器
target_box = box;
target_box.rect.x += bigger_rect.x;
target_box.rect.x += bigger_rect.x; // 添加roi偏移量
target_box.rect.y += bigger_rect.y;
for(auto &blob : target_box.light_blobs){
blob.rect.center.x += bigger_rect.x;
@@ -44,16 +41,16 @@ bool ArmorFinder::stateTrackingTarget(cv::Mat &src) {
}
tracker = TrackerToUse::create();
tracker->init(src, target_box.rect);
}else{
}else{ // 如果没有成功搜索目标,则使用判断是否跟丢。
roi = src(pos).clone();
if(classifier){
if(classifier){ // 分类器可用,使用分类器判断。
cv::resize(roi, roi, cv::Size(48, 36));
if(classifier(roi) == 0){
target_box = ArmorBox();
LOGW("Track classify fail range!");
return false;
}
}else{
}else{ // 分类器不可用,使用常规方法判断
cv::Mat roi_gray;
cv::cvtColor(roi, roi_gray, CV_RGB2GRAY);
cv::threshold(roi_gray, roi_gray, 180, 255, cv::THRESH_BINARY);
@@ -65,7 +62,6 @@ bool ArmorFinder::stateTrackingTarget(cv::Mat &src) {
}
target_box.rect = pos;
target_box.light_blobs.clear();
target_box = ArmorBox();
}
return true;
}