整理代码,添加部分注释

This commit is contained in:
xinyang
2019-07-08 21:53:12 +08:00
parent ab0ddede0c
commit 49cc1e242e
16 changed files with 109 additions and 102 deletions

View File

@@ -1,17 +1,11 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT(AutoAim)
PROJECT(SJTU-RM-CV)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_BUILD_TYPE RELEASE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPATH=\"\\\"${PROJECT_SOURCE_DIR}\\\"\"")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${CMAKE_SYSTEM_NAME}")
# Todo
## 使用编译期固定选项,以略微提升性能。
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIX_OPTIONS")
## 固定使用相机运行
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRUN_WITH_CAMERA")
SET(BIN_NAME "run")
FIND_PROGRAM(CCACHE_FOUND ccache)
@@ -21,7 +15,6 @@ IF(CCACHE_FOUND)
MESSAGE("< Use ccache for compiler >")
ENDIF()
FIND_PACKAGE(Eigen3 REQUIRED)
FIND_PACKAGE(OpenCV 3 REQUIRED)
FIND_PACKAGE(Threads)
@@ -34,7 +27,7 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/armor/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/others/include)
FILE(GLOB_RECURSE sourcefiles "others/src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} )
ADD_EXECUTABLE(${BIN_NAME} main.cpp ${sourcefiles} others/include/constants.h)
TARGET_LINK_LIBRARIES(${BIN_NAME} ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(${BIN_NAME} ${OpenCV_LIBS})
@@ -52,3 +45,4 @@ ELSE ()
ENDIF()
ADD_CUSTOM_TARGET(create-startup COMMAND "${PROJECT_SOURCE_DIR}/tools/create-startup.sh" "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}")
ADD_CUSTOM_TARGET(train-cnn COMMAND "gnome-terminal" "--" "bash" "-c" "${PROJECT_SOURCE_DIR}/tools/TrainCNN/backward.py")

View File

@@ -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 &paras_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_ */

View File

@@ -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);

View File

@@ -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 &paras_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!"));
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -14,7 +14,6 @@ const int SRC_WIDTH_CAMERA = 640;
const int SRC_HEIGHT_CAMERA = 480;
const int SRC_WIDTH = 320;
const int SRC_HEIGHT = 240;
const double PI = 3.1415926;
const int CLOCKWISE = 1;
const int ANTICLOCKWISE = -1;
const float ATTACK_DISTANCE = 718;//cm

View File

@@ -9,6 +9,7 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <constants.h>
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>

View File

@@ -2,22 +2,17 @@
// Created by xixiliadorabarry on 1/24/19.
//
#include <iostream>
#include <thread>
#include <opencv2/core/core.hpp>
#include <fstream>
#include <energy/energy.h>
#include <serial/serial.h>
#include <energy/param_struct_define.h>
#include <energy/constant.h>
#include <camera/camera_wrapper.h>
#include <camera/video_wrapper.h>
#include <camera/wrapper_head.h>
#include <energy/energy.h>
#include <armor_finder/armor_finder.h>
#include <options/options.h>
#include <additions/additions.h>
#include <thread>
#define DO_NOT_CNT_TIME
#include <log.h>
using namespace cv;

View File

@@ -7,15 +7,6 @@
#include <stdint.h>
#include <serial/serial.h>
#include <opencv2/videoio.hpp>
#include <camera/wrapper_head.h>
#define ENEMY_BLUE 0
#define ENEMY_RED 1
#define BIG_ENERGY_STATE 'b'
#define SMALL_ENERGY_STATE 's'
#define ARMOR_STATE 'a'
struct mcu_data{
float curr_yaw;

View File

@@ -0,0 +1,17 @@
//
// Created by xinyang on 19-7-8.
//
#ifndef _CONSTANTS_H_
#define _CONSTANTS_H_
#define PI (3.14159265459)
#define ENEMY_BLUE 0
#define ENEMY_RED 1
#define BIG_ENERGY_STATE 'b'
#define SMALL_ENERGY_STATE 's'
#define ARMOR_STATE 'a'
#endif /* _CONSTANTS_H */

View File

@@ -3,17 +3,17 @@
//
#include <cstring>
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio/videoio_c.h>
#include <additions/additions.h>
#include <camera/camera_wrapper.h>
#include <log.h>
#include <opencv2/videoio/videoio_c.h>
#include <iostream>
#include <energy/energy.h>
#include <armor_finder/armor_finder.h>
#include <log.h>
#define RECEIVE_LOG_LEVEL LOG_MSG