现可使用CNN分类器判断装甲板及其数字。
This commit is contained in:
@@ -12,16 +12,17 @@ IF(CCACHE_FOUND)
|
||||
ENDIF()
|
||||
|
||||
FIND_PACKAGE(OpenCV 3 REQUIRED)
|
||||
FIND_PACKAGE(Eigen3 REQUIRED)
|
||||
FIND_PACKAGE(Threads)
|
||||
|
||||
include_directories( ${EIGEN3_INCLUDE_DIR} )
|
||||
include_directories( ${PROJECT_SOURCE_DIR}/energy/include )
|
||||
include_directories( ${PROJECT_SOURCE_DIR}/armor/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} )
|
||||
|
||||
TARGET_LINK_LIBRARIES (run ${CMAKE_THREAD_LIBS_INIT})
|
||||
TARGET_LINK_LIBRARIES(run ${OpenCV_LIBS})
|
||||
TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/libMVSDK.so)
|
||||
TARGET_LINK_LIBRARIES(run ${CMAKE_THREAD_LIBS_INIT})
|
||||
TARGET_LINK_LIBRARIES(run ${OpenCV_LIBS})
|
||||
TARGET_LINK_LIBRARIES(run ${PROJECT_SOURCE_DIR}/libMVSDK.so)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/tracking.hpp>
|
||||
#include <uart/uart.h>
|
||||
#include <armor_finder/classifier/classifier.h>
|
||||
|
||||
typedef enum{
|
||||
ENEMY_BLUE, ENEMY_RED
|
||||
@@ -15,7 +16,7 @@ typedef enum{
|
||||
|
||||
class ArmorFinder{
|
||||
public:
|
||||
ArmorFinder(EnemyColor color, Uart &u);
|
||||
ArmorFinder(EnemyColor color, Uart &u, string paras_folder);
|
||||
~ArmorFinder() = default;
|
||||
|
||||
private:
|
||||
@@ -29,6 +30,9 @@ private:
|
||||
State state;
|
||||
cv::Rect2d armor_box;
|
||||
cv::Ptr<cv::Tracker> tracker;
|
||||
cv::Mat src_gray;
|
||||
|
||||
Classifier classifier;
|
||||
|
||||
int contour_area;
|
||||
Uart &uart;
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
#include <log.h>
|
||||
#include <armor_finder/armor_finder.h>
|
||||
|
||||
ArmorFinder::ArmorFinder(EnemyColor color, Uart &u) :
|
||||
ArmorFinder::ArmorFinder(EnemyColor color, Uart &u, string paras_folder) :
|
||||
uart(u),
|
||||
enemy_color(color),
|
||||
state(STANDBY_STATE)
|
||||
state(STANDBY_STATE),
|
||||
classifier(std::move(paras_folder)),
|
||||
contour_area(0)
|
||||
{
|
||||
auto para = TrackerToUse::Params();
|
||||
para.desc_npca = 1;
|
||||
@@ -25,6 +27,7 @@ void ArmorFinder::run(cv::Mat &src) {
|
||||
}else{
|
||||
src_use = src.clone();
|
||||
}
|
||||
cv::cvtColor(src_use, src_gray, CV_BayerBG2GRAY);
|
||||
|
||||
// return stateSearchingTarget(src_use);
|
||||
|
||||
|
||||
@@ -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 dy = box.y-box.height/2 - 240;
|
||||
return dx*dx + dy*dy;
|
||||
@@ -169,14 +169,23 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
|
||||
// }
|
||||
if(show_light_blobs){
|
||||
showContours("blobs", split, light_blobs);
|
||||
// showContours("pm blobs", pmsrc, pm_light_blobs);
|
||||
// showContours("blobs real", src, light_blobs_real);
|
||||
cv::waitKey(1);
|
||||
}
|
||||
if(!findArmorBoxes(light_blobs, armor_boxes)){
|
||||
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){
|
||||
showArmorBoxVector("boxes", split, armor_boxes);
|
||||
cv::waitKey(1);
|
||||
|
||||
246
main.cpp
246
main.cpp
@@ -1,123 +1,123 @@
|
||||
//
|
||||
// Created by xixiliadorabarry on 1/24/19.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#include "energy/energy.h"
|
||||
#include "uart/uart.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 "armor_finder/armor_finder.h"
|
||||
#include <options/options.h>
|
||||
#include <log.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
#define ENERGY_STATE 1
|
||||
#define ARMOR_STATE 0
|
||||
|
||||
int state = ARMOR_STATE;
|
||||
float curr_yaw=0, curr_pitch=0;
|
||||
float mark_yaw=0, mark_pitch=0;
|
||||
|
||||
void uartReceive(Uart* uart);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
process_options(argc, argv);
|
||||
Uart uart;
|
||||
thread receive(uartReceive, &uart);
|
||||
bool flag = true;
|
||||
|
||||
while (flag)
|
||||
{
|
||||
int ally_color = ALLY_RED;
|
||||
int energy_part_rotation = CLOCKWISE;
|
||||
|
||||
int from_camera = 1;
|
||||
if(!run_with_camera) {
|
||||
cout << "Input 1 for camera, 0 for video files" << endl;
|
||||
cin >> from_camera;
|
||||
}
|
||||
|
||||
WrapperHead *video;
|
||||
if(from_camera)
|
||||
video = new CameraWrapper;
|
||||
else
|
||||
video = new VideoWrapper("r_l_640.avi", "fan_640.avi");
|
||||
|
||||
if (video->init()) {
|
||||
cout << "Video source initialization successfully." << endl;
|
||||
}
|
||||
|
||||
Mat energy_src, armor_src;
|
||||
|
||||
ArmorFinder armorFinder(ENEMY_BLUE, uart);
|
||||
|
||||
Energy energy(uart);
|
||||
energy.setAllyColor(ally_color);
|
||||
energy.setRotation(energy_part_rotation);
|
||||
|
||||
while (video->read(energy_src, armor_src))
|
||||
{
|
||||
if(show_origin) {
|
||||
imshow("enery src", energy_src);
|
||||
imshow("armor src", armor_src);
|
||||
}
|
||||
if(state == ENERGY_STATE){
|
||||
energy.run(energy_src);
|
||||
}else{
|
||||
armorFinder.run(armor_src);
|
||||
}
|
||||
|
||||
if (waitKey(1) == 'q') {
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete video;
|
||||
cout << "Program fails. Restarting" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uartReceive(Uart* uart){
|
||||
char buffer[100];
|
||||
int cnt=0;
|
||||
while(true){
|
||||
char data;
|
||||
while((data=uart->receive()) != '\n'){
|
||||
buffer[cnt++] = data;
|
||||
if(cnt >= 100){
|
||||
LOGE("data receive over flow!");
|
||||
}
|
||||
}
|
||||
if(cnt == 10){
|
||||
if(buffer[8] == 'e'){
|
||||
state = ENERGY_STATE;
|
||||
LOGM("Energy state");
|
||||
}else if(buffer[8] == 'a'){
|
||||
state = ARMOR_STATE;
|
||||
LOGM("Armor state");
|
||||
}
|
||||
memcpy(&curr_yaw, buffer, 4);
|
||||
memcpy(&curr_pitch, buffer+4, 4);
|
||||
LOGM("Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
|
||||
if(buffer[9] == 1){
|
||||
mark_yaw = curr_yaw;
|
||||
mark_pitch = curr_pitch;
|
||||
LOGM("Marked");
|
||||
}
|
||||
}
|
||||
cnt = 0;
|
||||
}
|
||||
}
|
||||
//
|
||||
// Created by xixiliadorabarry on 1/24/19.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#include "energy/energy.h"
|
||||
#include "uart/uart.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 "armor_finder/armor_finder.h"
|
||||
#include <options/options.h>
|
||||
#include <log.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
#define ENERGY_STATE 1
|
||||
#define ARMOR_STATE 0
|
||||
|
||||
int state = ARMOR_STATE;
|
||||
float curr_yaw=0, curr_pitch=0;
|
||||
float mark_yaw=0, mark_pitch=0;
|
||||
|
||||
void uartReceive(Uart* uart);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
process_options(argc, argv);
|
||||
Uart uart;
|
||||
thread receive(uartReceive, &uart);
|
||||
bool flag = true;
|
||||
|
||||
while (flag)
|
||||
{
|
||||
int ally_color = ALLY_RED;
|
||||
int energy_part_rotation = CLOCKWISE;
|
||||
|
||||
int from_camera = 1;
|
||||
if(!run_with_camera) {
|
||||
cout << "Input 1 for camera, 0 for video files" << endl;
|
||||
cin >> from_camera;
|
||||
}
|
||||
|
||||
WrapperHead *video;
|
||||
if(from_camera)
|
||||
video = new CameraWrapper;
|
||||
else
|
||||
video = new VideoWrapper("r_l_640.avi", "fan_640.avi");
|
||||
|
||||
if (video->init()) {
|
||||
cout << "Video source initialization successfully." << endl;
|
||||
}
|
||||
|
||||
Mat energy_src, armor_src;
|
||||
|
||||
ArmorFinder armorFinder(ENEMY_BLUE, uart, "../paras/");
|
||||
|
||||
Energy energy(uart);
|
||||
energy.setAllyColor(ally_color);
|
||||
energy.setRotation(energy_part_rotation);
|
||||
|
||||
while (video->read(energy_src, armor_src))
|
||||
{
|
||||
if(show_origin) {
|
||||
imshow("enery src", energy_src);
|
||||
imshow("armor src", armor_src);
|
||||
}
|
||||
if(state == ENERGY_STATE){
|
||||
energy.run(energy_src);
|
||||
}else{
|
||||
armorFinder.run(armor_src);
|
||||
}
|
||||
|
||||
if (waitKey(1) == 'q') {
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
delete video;
|
||||
cout << "Program fails. Restarting" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uartReceive(Uart* uart){
|
||||
char buffer[100];
|
||||
int cnt=0;
|
||||
while(true){
|
||||
char data;
|
||||
while((data=uart->receive()) != '\n'){
|
||||
buffer[cnt++] = data;
|
||||
if(cnt >= 100){
|
||||
LOGE("data receive over flow!");
|
||||
}
|
||||
}
|
||||
if(cnt == 10){
|
||||
if(buffer[8] == 'e'){
|
||||
state = ENERGY_STATE;
|
||||
LOGM("Energy state");
|
||||
}else if(buffer[8] == 'a'){
|
||||
state = ARMOR_STATE;
|
||||
LOGM("Armor state");
|
||||
}
|
||||
memcpy(&curr_yaw, buffer, 4);
|
||||
memcpy(&curr_pitch, buffer+4, 4);
|
||||
LOGM("Get yaw:%f pitch:%f", curr_yaw, curr_pitch);
|
||||
if(buffer[9] == 1){
|
||||
mark_yaw = curr_yaw;
|
||||
mark_pitch = curr_pitch;
|
||||
LOGM("Marked");
|
||||
}
|
||||
}
|
||||
cnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user