整理代码,添加部分注释
This commit is contained in:
@@ -6,63 +6,61 @@
|
||||
#define _ARMOR_FINDER_H_
|
||||
|
||||
#include <map>
|
||||
#include <constants.h>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/tracking.hpp>
|
||||
#include <serial/serial.h>
|
||||
#include <armor_finder/classifier/classifier.h>
|
||||
#include "additions/additions.h"
|
||||
|
||||
extern std::map<int, string> id2name;
|
||||
extern std::map<string, int> name2id;
|
||||
|
||||
class ArmorFinder{
|
||||
public:
|
||||
ArmorFinder(uint8_t &color, Serial &u, string paras_folder, const uint8_t &use);
|
||||
~ArmorFinder() = default;
|
||||
|
||||
private:
|
||||
typedef cv::TrackerKCF TrackerToUse;
|
||||
|
||||
typedef enum{
|
||||
SEARCHING_STATE, TRACKING_STATE, STANDBY_STATE
|
||||
} State;
|
||||
|
||||
const uint8_t &enemy_color;
|
||||
State state;
|
||||
cv::Rect2d armor_box;
|
||||
int boxid;
|
||||
cv::Ptr<cv::Tracker> tracker;
|
||||
|
||||
Classifier classifier;
|
||||
|
||||
int contour_area;
|
||||
Serial &serial;
|
||||
const uint8_t &use_classifier;
|
||||
|
||||
bool stateSearchingTarget(cv::Mat &src);
|
||||
bool stateTrackingTarget(cv::Mat &src);
|
||||
bool stateStandBy();
|
||||
public:
|
||||
void run(cv::Mat &src);
|
||||
bool sendBoxPosition();
|
||||
};
|
||||
#include <additions/additions.h>
|
||||
|
||||
#define BLOB_RED ENEMY_RED
|
||||
#define BLOB_BLUE ENEMY_BLUE
|
||||
|
||||
extern std::map<int, string> id2name; //装甲板id到名称的map
|
||||
extern std::map<string, int> name2id; //装甲板名称到id的map
|
||||
|
||||
|
||||
/********************* 自瞄类定义 **********************/
|
||||
class ArmorFinder{
|
||||
public:
|
||||
ArmorFinder(uint8_t &color, Serial &u, const string ¶s_folder, const uint8_t &use);
|
||||
~ArmorFinder() = default;
|
||||
|
||||
private:
|
||||
typedef cv::TrackerKCF TrackerToUse; // Tracker类型定义
|
||||
|
||||
typedef enum{
|
||||
SEARCHING_STATE, TRACKING_STATE, STANDBY_STATE
|
||||
} State; // 自瞄状态枚举定义
|
||||
|
||||
const uint8_t &enemy_color; // 敌方颜色,引用外部变量,自动变化
|
||||
State state; // 自瞄状态对象实例
|
||||
cv::Rect2d armor_box; // 当前目标位置
|
||||
int boxid; // 当前目标id
|
||||
cv::Ptr<cv::Tracker> tracker; // tracker对象实例
|
||||
Classifier classifier; // CNN分类器对象实例,用于数字识别
|
||||
int contour_area; // 装甲区域亮点个数,用于数字识别未启用时判断是否跟丢(已弃用)
|
||||
Serial &serial; // 串口对象,引用外部变量,用于和能量机关共享同一个变量
|
||||
const uint8_t &use_classifier; // 标记是否启用CNN分类器,引用外部变量,自动变化
|
||||
|
||||
bool stateSearchingTarget(cv::Mat &src); // searching state主函数
|
||||
bool stateTrackingTarget(cv::Mat &src); // tracking state主函数
|
||||
bool stateStandBy(); // stand by state主函数(已弃用)
|
||||
public:
|
||||
void run(cv::Mat &src); // 自瞄主函数
|
||||
bool sendBoxPosition(); // 和主控板通讯
|
||||
};
|
||||
|
||||
/******************* 灯条类定义 ***********************/
|
||||
class LightBlob {
|
||||
public:
|
||||
cv::RotatedRect rect;
|
||||
double length;
|
||||
uint8_t BlobColor;
|
||||
cv::RotatedRect rect; //灯条位置
|
||||
double length; //灯条长度
|
||||
uint8_t BlobColor; //灯条颜色
|
||||
|
||||
LightBlob(cv::RotatedRect &r) : rect(r) {
|
||||
length = max(rect.size.height, rect.size.width);
|
||||
};
|
||||
bool operator<(LightBlob &l2) { return this->rect.center.x < l2.rect.center.x; }
|
||||
bool operator<=(LightBlob &l2) { return this->rect.center.x <= l2.rect.center.x; }
|
||||
bool operator>(LightBlob &l2) { return this->rect.center.x > l2.rect.center.x; }
|
||||
bool operator>=(LightBlob &l2) { return this->rect.center.x >= l2.rect.center.x; }
|
||||
};
|
||||
|
||||
#endif /* _ARMOR_FINDER_H_ */
|
||||
|
||||
@@ -6,10 +6,9 @@
|
||||
#define _SHOW_IMAGES_H_
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <armor_finder/armor_finder.h>
|
||||
|
||||
//
|
||||
void showArmorBoxVector(std::string windows_name, const cv::Mat &src, const std::vector<cv::Rect2d> &armor_box);
|
||||
void showArmorBox(std::string windows_name, const cv::Mat &src, cv::Rect2d armor_box, int boxid);
|
||||
void showContours(std::string windows_name, const cv::Mat &src, const std::vector<LightBlob> &light_blobs);
|
||||
|
||||
@@ -12,19 +12,19 @@ std::map<int, string> id2name = {
|
||||
{-1, "OO"},{ 0, "NO"},
|
||||
{ 1, "B1"},{ 2, "B2"},{ 3, "B3"},{ 4, "B4"},{ 5, "B5"},{ 6, "B7"},{ 7, "B8"},
|
||||
{ 8, "R1"},{ 9, "R2"},{10, "R3"},{11, "R4"},{12, "R5"},{13, "R7"},{14, "R8"},
|
||||
};
|
||||
}; //装甲板id到名称的map
|
||||
|
||||
std::map<string, int> name2id = {
|
||||
{"OO", -1},{"NO", 0},
|
||||
{"B1", 1},{"B2", 2},{"B3", 3},{"B4", 4},{"B5", 5},{"B7", 6},{"B8", 7},
|
||||
{"R1", 8},{"R2", 9},{"R3", 10},{"R4", 11},{"R5", 12},{"R7", 13},{"R8", 14},
|
||||
};
|
||||
}; //装甲板名称到id的map
|
||||
|
||||
ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, string paras_folder, const uint8_t &use) :
|
||||
ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, const string ¶s_folder, const uint8_t &use) :
|
||||
serial(u),
|
||||
enemy_color(color),
|
||||
state(STANDBY_STATE),
|
||||
classifier(std::move(paras_folder)),
|
||||
classifier(paras_folder),
|
||||
contour_area(0),
|
||||
use_classifier(use),
|
||||
boxid(-1)
|
||||
@@ -32,11 +32,10 @@ ArmorFinder::ArmorFinder(uint8_t &color, Serial &u, string paras_folder, const u
|
||||
}
|
||||
|
||||
void ArmorFinder::run(cv::Mat &src) {
|
||||
static int tracking_cnt = 0;
|
||||
cv::Mat src_use;
|
||||
src_use = src.clone();
|
||||
static int tracking_cnt = 0; // 记录追踪帧数,用于定时退出追踪
|
||||
cv::Mat src_use = src.clone(); // 实际参与计算的图像对象
|
||||
|
||||
if(show_armor_box){
|
||||
if(show_armor_box){ // 根据条件显示当前目标装甲板
|
||||
showArmorBox("box", src, armor_box, boxid);
|
||||
cv::waitKey(1);
|
||||
}
|
||||
@@ -45,14 +44,14 @@ void ArmorFinder::run(cv::Mat &src) {
|
||||
switch (state){
|
||||
case SEARCHING_STATE:
|
||||
if(stateSearchingTarget(src_use)){
|
||||
if((armor_box & cv::Rect2d(0, 0, 640, 480)) == armor_box) {
|
||||
if(!classifier && use_classifier){
|
||||
cv::Mat roi = src_use.clone()(armor_box), roi_gray;
|
||||
if((armor_box & cv::Rect2d(0, 0, 640, 480)) == armor_box) { // 判断装甲板区域是否脱离图像区域
|
||||
if(!classifier || !use_classifier){ /* 如果分类器不可用或者不使用分类器 */
|
||||
cv::Mat roi = src_use.clone()(armor_box), roi_gray; /* 就使用装甲区域亮点数判断是否跟丢 */
|
||||
cv::cvtColor(roi, roi_gray, CV_RGB2GRAY);
|
||||
cv::threshold(roi_gray, roi_gray, 180, 255, cv::THRESH_BINARY);
|
||||
contour_area = cv::countNonZero(roi_gray);
|
||||
}
|
||||
tracker = TrackerToUse::create();
|
||||
tracker = TrackerToUse::create(); // 成功搜寻到装甲板,创建tracker对象
|
||||
tracker->init(src_use, armor_box);
|
||||
state = TRACKING_STATE;
|
||||
tracking_cnt = 0;
|
||||
@@ -61,7 +60,7 @@ void ArmorFinder::run(cv::Mat &src) {
|
||||
}
|
||||
break;
|
||||
case TRACKING_STATE:
|
||||
if(++tracking_cnt>100 || !stateTrackingTarget(src_use)){
|
||||
if(++tracking_cnt>100 || !stateTrackingTarget(src_use)){ // 最多追踪100帧图像
|
||||
state = SEARCHING_STATE;
|
||||
LOGM(STR_CTR(WORD_LIGHT_YELLOW ,"into search!"));
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
|
||||
#include <armor_finder/armor_finder.h>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <image_process/image_process.h>
|
||||
#include <log.h>
|
||||
#include <show_images/show_images.h>
|
||||
#include <options/options.h>
|
||||
#include <log.h>
|
||||
#include "image_process.h"
|
||||
|
||||
typedef std::vector<LightBlob> LightBlobs;
|
||||
|
||||
@@ -255,20 +255,21 @@ static string prior_red[] = {
|
||||
};
|
||||
|
||||
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
|
||||
cv::Mat split, src_bin, color;
|
||||
LightBlobs light_blobs;
|
||||
std::vector<cv::Rect2d> armor_boxes, boxes_number[14];
|
||||
std::vector<cv::Mat> channels;
|
||||
std::vector<cv::Mat> channels; // 通道拆分
|
||||
cv::Mat src_bin, color; // 二值图和颜色通道图
|
||||
LightBlobs light_blobs; // 存储所有可能的灯条
|
||||
std::vector<cv::Rect2d> armor_boxes; // 装甲板候选区
|
||||
std::vector<cv::Rect2d> boxes_number[15]; // 装甲板候选区放置在对应id位置
|
||||
|
||||
armor_box = cv::Rect2d(0, 0, 0, 0);
|
||||
armor_box = cv::Rect2d(0, 0, 0, 0); // 重置目标装甲板位置
|
||||
|
||||
cv::split(src, channels);
|
||||
if (enemy_color == ENEMY_BLUE)
|
||||
color = channels[0];
|
||||
else if (enemy_color == ENEMY_RED)
|
||||
color = channels[2];
|
||||
cv::threshold(color, src_bin, 170, 255, CV_THRESH_BINARY);
|
||||
imagePreProcess(src_bin);
|
||||
cv::split(src, channels); /************************/
|
||||
if (enemy_color == ENEMY_BLUE) /* */
|
||||
color = channels[0]; /* 根据目标颜色进行通道提取 */
|
||||
else if (enemy_color == ENEMY_RED) /* */
|
||||
color = channels[2]; /************************/
|
||||
cv::threshold(color, src_bin, 170, 255, CV_THRESH_BINARY); // 二值化对应通道
|
||||
imagePreProcess(src_bin); // 开闭运算
|
||||
if (!findLightBlobs(src_bin, light_blobs)) {
|
||||
return false;
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
#include <show_images/show_images.h>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <log.h>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
/**************************
|
||||
* 显示多个装甲板区域 *
|
||||
**************************/
|
||||
void showArmorBoxVector(std::string windows_name, const cv::Mat &src, const std::vector<cv::Rect2d> &armor_box) {
|
||||
static Mat image2show;
|
||||
if (src.type() == CV_8UC1) {// 黑白图像
|
||||
@@ -17,6 +21,9 @@ void showArmorBoxVector(std::string windows_name, const cv::Mat &src, const std:
|
||||
imshow(windows_name, image2show);
|
||||
}
|
||||
|
||||
/**************************
|
||||
* 显示多个装甲板区域及其类别 *
|
||||
**************************/
|
||||
void showArmorBoxClass(std::string window_names, const cv::Mat &src, vector<cv::Rect2d> boxes[10]) {
|
||||
static Mat image2show;
|
||||
if (src.type() == CV_8UC1) { // 黑白图像
|
||||
@@ -45,6 +52,9 @@ void showArmorBoxClass(std::string window_names, const cv::Mat &src, vector<cv::
|
||||
imshow(window_names, image2show);
|
||||
}
|
||||
|
||||
/**************************
|
||||
* 显示多个装甲板区域及其类别 *
|
||||
**************************/
|
||||
void showArmorBox(std::string windows_name, const cv::Mat &src, cv::Rect2d armor_box, int boxid) {
|
||||
static Mat image2show;
|
||||
if (src.type() == CV_8UC1) { // 黑白图像
|
||||
@@ -67,6 +77,9 @@ void showArmorBox(std::string windows_name, const cv::Mat &src, cv::Rect2d armor
|
||||
imshow(windows_name, image2show);
|
||||
}
|
||||
|
||||
/**************************
|
||||
* 显示多个灯条区域 *
|
||||
**************************/
|
||||
void showContours(std::string windows_name, const cv::Mat &src, const std::vector<LightBlob> &light_blobs) {
|
||||
static Mat image2show;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user