现可使用CNN分类器判断装甲板及其数字。

This commit is contained in:
xinyang
2019-04-20 14:30:57 +08:00
parent f891f1da34
commit 83e9a0daf9
5 changed files with 152 additions and 135 deletions

View File

@@ -12,16 +12,17 @@ IF(CCACHE_FOUND)
ENDIF() ENDIF()
FIND_PACKAGE(OpenCV 3 REQUIRED) FIND_PACKAGE(OpenCV 3 REQUIRED)
FIND_PACKAGE(Eigen3 REQUIRED)
FIND_PACKAGE(Threads) FIND_PACKAGE(Threads)
include_directories( ${EIGEN3_INCLUDE_DIR} )
include_directories( ${PROJECT_SOURCE_DIR}/energy/include ) include_directories( ${PROJECT_SOURCE_DIR}/energy/include )
include_directories( ${PROJECT_SOURCE_DIR}/armor/include ) include_directories( ${PROJECT_SOURCE_DIR}/armor/include )
include_directories( ${PROJECT_SOURCE_DIR}/include ) include_directories( ${PROJECT_SOURCE_DIR}/include )
include_directories( ${PROJECT_SOURCE_DIR}/src )
FILE(GLOB_RECURSE sourcefiles "src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
FILE(GLOB_RECURSE sourcefiles "src/*.cpp" "energy/src/*cpp" "armor/src/*.cpp")
add_executable(run main.cpp ${sourcefiles} ) add_executable(run main.cpp ${sourcefiles} )
TARGET_LINK_LIBRARIES (run ${CMAKE_THREAD_LIBS_INIT}) TARGET_LINK_LIBRARIES(run ${CMAKE_THREAD_LIBS_INIT})
TARGET_LINK_LIBRARIES(run ${OpenCV_LIBS}) TARGET_LINK_LIBRARIES(run ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/libMVSDK.so) TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/libMVSDK.so)

View File

@@ -8,6 +8,7 @@
#include <opencv2/core.hpp> #include <opencv2/core.hpp>
#include <opencv2/tracking.hpp> #include <opencv2/tracking.hpp>
#include <uart/uart.h> #include <uart/uart.h>
#include <armor_finder/classifier/classifier.h>
typedef enum{ typedef enum{
ENEMY_BLUE, ENEMY_RED ENEMY_BLUE, ENEMY_RED
@@ -15,7 +16,7 @@ typedef enum{
class ArmorFinder{ class ArmorFinder{
public: public:
ArmorFinder(EnemyColor color, Uart &u); ArmorFinder(EnemyColor color, Uart &u, string paras_folder);
~ArmorFinder() = default; ~ArmorFinder() = default;
private: private:
@@ -29,6 +30,9 @@ private:
State state; State state;
cv::Rect2d armor_box; cv::Rect2d armor_box;
cv::Ptr<cv::Tracker> tracker; cv::Ptr<cv::Tracker> tracker;
cv::Mat src_gray;
Classifier classifier;
int contour_area; int contour_area;
Uart &uart; Uart &uart;

View File

@@ -4,10 +4,12 @@
#include <log.h> #include <log.h>
#include <armor_finder/armor_finder.h> #include <armor_finder/armor_finder.h>
ArmorFinder::ArmorFinder(EnemyColor color, Uart &u) : ArmorFinder::ArmorFinder(EnemyColor color, Uart &u, string paras_folder) :
uart(u), uart(u),
enemy_color(color), enemy_color(color),
state(STANDBY_STATE) state(STANDBY_STATE),
classifier(std::move(paras_folder)),
contour_area(0)
{ {
auto para = TrackerToUse::Params(); auto para = TrackerToUse::Params();
para.desc_npca = 1; para.desc_npca = 1;
@@ -25,6 +27,7 @@ void ArmorFinder::run(cv::Mat &src) {
}else{ }else{
src_use = src.clone(); src_use = src.clone();
} }
cv::cvtColor(src_use, src_gray, CV_BayerBG2GRAY);
// return stateSearchingTarget(src_use); // return stateSearchingTarget(src_use);

View File

@@ -99,7 +99,7 @@ bool isCoupleLight(const LightBlob &light_blob_i, const LightBlob &light_blob_j)
} }
double centerDistance(cv::Rect2d box){ double centerDistance(const cv::Rect2d &box){
double dx = box.x-box.width/2 - 320; double dx = box.x-box.width/2 - 320;
double dy = box.y-box.height/2 - 240; double dy = box.y-box.height/2 - 240;
return dx*dx + dy*dy; return dx*dx + dy*dy;
@@ -169,14 +169,23 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
// } // }
if(show_light_blobs){ if(show_light_blobs){
showContours("blobs", split, light_blobs); showContours("blobs", split, light_blobs);
// showContours("pm blobs", pmsrc, pm_light_blobs);
// showContours("blobs real", src, light_blobs_real);
cv::waitKey(1); cv::waitKey(1);
} }
if(!findArmorBoxes(light_blobs, armor_boxes)){ if(!findArmorBoxes(light_blobs, armor_boxes)){
return false; return false;
} }
armor_box = armor_boxes[0]; if(classifier){
for(const auto &box : armor_boxes){
cv::Mat roi = src(box).clone();
cv::resize(roi, roi, cv::Size(60, 45));
if(classifier(roi)){
armor_box = box;
break;
}
}
}else{
armor_box = armor_boxes[0];
}
if(show_armor_boxes){ if(show_armor_boxes){
showArmorBoxVector("boxes", split, armor_boxes); showArmorBoxVector("boxes", split, armor_boxes);
cv::waitKey(1); cv::waitKey(1);

246
main.cpp
View File

@@ -1,123 +1,123 @@
// //
// Created by xixiliadorabarry on 1/24/19. // Created by xixiliadorabarry on 1/24/19.
// //
#include <iostream> #include <iostream>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include "energy/energy.h" #include "energy/energy.h"
#include "uart/uart.h" #include "uart/uart.h"
#include "energy/param_struct_define.h" #include "energy/param_struct_define.h"
#include "energy/constant.h" #include "energy/constant.h"
#include "camera/camera_wrapper.h" #include "camera/camera_wrapper.h"
#include "camera/video_wrapper.h" #include "camera/video_wrapper.h"
#include "camera/wrapper_head.h" #include "camera/wrapper_head.h"
#include "armor_finder/armor_finder.h" #include "armor_finder/armor_finder.h"
#include <options/options.h> #include <options/options.h>
#include <log.h> #include <log.h>
#include <thread> #include <thread>
using namespace cv; using namespace cv;
using namespace std; using namespace std;
#define ENERGY_STATE 1 #define ENERGY_STATE 1
#define ARMOR_STATE 0 #define ARMOR_STATE 0
int state = ARMOR_STATE; int state = ARMOR_STATE;
float curr_yaw=0, curr_pitch=0; float curr_yaw=0, curr_pitch=0;
float mark_yaw=0, mark_pitch=0; float mark_yaw=0, mark_pitch=0;
void uartReceive(Uart* uart); void uartReceive(Uart* uart);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
process_options(argc, argv); process_options(argc, argv);
Uart uart; Uart uart;
thread receive(uartReceive, &uart); thread receive(uartReceive, &uart);
bool flag = true; bool flag = true;
while (flag) while (flag)
{ {
int ally_color = ALLY_RED; int ally_color = ALLY_RED;
int energy_part_rotation = CLOCKWISE; int energy_part_rotation = CLOCKWISE;
int from_camera = 1; int from_camera = 1;
if(!run_with_camera) { if(!run_with_camera) {
cout << "Input 1 for camera, 0 for video files" << endl; cout << "Input 1 for camera, 0 for video files" << endl;
cin >> from_camera; cin >> from_camera;
} }
WrapperHead *video; WrapperHead *video;
if(from_camera) if(from_camera)
video = new CameraWrapper; video = new CameraWrapper;
else else
video = new VideoWrapper("r_l_640.avi", "fan_640.avi"); video = new VideoWrapper("r_l_640.avi", "fan_640.avi");
if (video->init()) { if (video->init()) {
cout << "Video source initialization successfully." << endl; cout << "Video source initialization successfully." << endl;
} }
Mat energy_src, armor_src; Mat energy_src, armor_src;
ArmorFinder armorFinder(ENEMY_BLUE, uart); ArmorFinder armorFinder(ENEMY_BLUE, uart, "../paras/");
Energy energy(uart); Energy energy(uart);
energy.setAllyColor(ally_color); energy.setAllyColor(ally_color);
energy.setRotation(energy_part_rotation); energy.setRotation(energy_part_rotation);
while (video->read(energy_src, armor_src)) while (video->read(energy_src, armor_src))
{ {
if(show_origin) { if(show_origin) {
imshow("enery src", energy_src); imshow("enery src", energy_src);
imshow("armor src", armor_src); imshow("armor src", armor_src);
} }
if(state == ENERGY_STATE){ if(state == ENERGY_STATE){
energy.run(energy_src); energy.run(energy_src);
}else{ }else{
armorFinder.run(armor_src); armorFinder.run(armor_src);
} }
if (waitKey(1) == 'q') { if (waitKey(1) == 'q') {
flag = false; flag = false;
break; break;
} }
} }
delete video; delete video;
cout << "Program fails. Restarting" << endl; cout << "Program fails. Restarting" << endl;
} }
return 0; return 0;
} }
void uartReceive(Uart* uart){ void uartReceive(Uart* uart){
char buffer[100]; char buffer[100];
int cnt=0; int cnt=0;
while(true){ while(true){
char data; char data;
while((data=uart->receive()) != '\n'){ while((data=uart->receive()) != '\n'){
buffer[cnt++] = data; buffer[cnt++] = data;
if(cnt >= 100){ if(cnt >= 100){
LOGE("data receive over flow!"); LOGE("data receive over flow!");
} }
} }
if(cnt == 10){ if(cnt == 10){
if(buffer[8] == 'e'){ if(buffer[8] == 'e'){
state = ENERGY_STATE; state = ENERGY_STATE;
LOGM("Energy state"); LOGM("Energy state");
}else if(buffer[8] == 'a'){ }else if(buffer[8] == 'a'){
state = ARMOR_STATE; state = ARMOR_STATE;
LOGM("Armor state"); LOGM("Armor state");
} }
memcpy(&curr_yaw, buffer, 4); memcpy(&curr_yaw, buffer, 4);
memcpy(&curr_pitch, buffer+4, 4); memcpy(&curr_pitch, buffer+4, 4);
LOGM("Get yaw:%f pitch:%f", curr_yaw, curr_pitch); LOGM("Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
if(buffer[9] == 1){ if(buffer[9] == 1){
mark_yaw = curr_yaw; mark_yaw = curr_yaw;
mark_pitch = curr_pitch; mark_pitch = curr_pitch;
LOGM("Marked"); LOGM("Marked");
} }
} }
cnt = 0; cnt = 0;
} }
} }