更新了一大堆东西,我也说不清有什么。

This commit is contained in:
xinyang
2019-04-29 14:42:47 +08:00
parent 4015ee5910
commit 25927f0b34
9 changed files with 140 additions and 112 deletions

View File

@@ -2,6 +2,9 @@
// Created by xinyang on 19-3-27.
//
#include <log.h>
#include <options/options.h>
#include <show_images/show_images.h>
#include <opencv2/highgui.hpp>
#include <armor_finder/armor_finder.h>
ArmorFinder::ArmorFinder(EnemyColor color, Uart &u, string paras_folder) :
@@ -11,38 +14,48 @@ ArmorFinder::ArmorFinder(EnemyColor color, Uart &u, string paras_folder) :
classifier(std::move(paras_folder)),
contour_area(0)
{
auto para = TrackerToUse::Params();
para.desc_npca = 1;
para.desc_pca = 0;
tracker = TrackerToUse::create(para);
if(!tracker){
LOGW("Tracker Not init");
}
// auto para = TrackerToUse::Params();
// para.desc_npca = 1;
// para.desc_pca = 0;
// tracker = TrackerToUse::create(para);
// if(!tracker){
// LOGW("Tracker Not init");
// }
}
void ArmorFinder::run(cv::Mat &src) {
cv::Mat src_use;
// if (src.type() == CV_8UC3) {
// cv::cvtColor(src, src_use, CV_RGB2GRAY);
// }else{
src_use = src.clone();
// }
src_use = src.clone();
cv::cvtColor(src_use, src_gray, CV_RGB2GRAY);
stateSearchingTarget(src_use);
return;
if(show_armor_box){
showArmorBox("box", src, armor_box);
cv::waitKey(1);
}
// stateSearchingTarget(src_use);
// return;
switch (state){
case SEARCHING_STATE:
if(stateSearchingTarget(src_use)){
if((armor_box & cv::Rect2d(0, 0, 640, 480)) == armor_box) {
cv::Mat roi = src_use.clone()(armor_box);
cv::threshold(roi, roi, 200, 255, cv::THRESH_BINARY);
contour_area = cv::countNonZero(roi);
auto para = TrackerToUse::Params();
para.desc_npca = 1;
para.desc_pca = 0;
tracker = TrackerToUse::create(para);
// cv::Mat roi = src_gray.clone()(armor_box);
// cv::threshold(roi, roi, 200, 255, cv::THRESH_BINARY);
// contour_area = cv::countNonZero(roi);
// auto para = TrackerToUse::Params();
// para.desc_npca = 1;
// para.desc_pca = 0;
// tracker = TrackerToUse::create(para);
// tracker->init(src_gray, armor_box);
// tracker->update(src_gray, armor_box);
cv::Mat roi = src_use.clone()(armor_box), roi_gray;
cv::cvtColor(roi, roi_gray, CV_RGB2GRAY);
// cv::imshow("boxroi", roi);
// cv::waitKey(0);
cv::threshold(roi_gray, roi_gray, 180, 255, cv::THRESH_BINARY);
contour_area = cv::countNonZero(roi_gray);
LOGW("%d", contour_area);
tracker = TrackerToUse::create();
tracker->init(src_use, armor_box);
state = TRACKING_STATE;
LOGW("into track");
@@ -50,7 +63,7 @@ void ArmorFinder::run(cv::Mat &src) {
}
break;
case TRACKING_STATE:
if(!stateTrackingTarget(src_gray)){
if(!stateTrackingTarget(src_use)){
state = SEARCHING_STATE;
//std::cout << "into search!" << std::endl;
}

View File

@@ -259,7 +259,7 @@ Classifier::Classifier(const string &folder) : state(true){
fc2_w = load_fc_w(folder+"fc2_w");
fc2_b = load_fc_b(folder+"fc2_b");
if(state){
LOGM("Load paras success!");
LOGM("Load para success!");
}
}

View File

@@ -7,7 +7,6 @@
#include "image_process/image_process.h"
#include <log.h>
#include <show_images/show_images.h>
#include <options/options.h>
typedef std::vector<LightBlob> LightBlobs;
@@ -32,11 +31,10 @@ static void pipelineLightBlobPreprocess(cv::Mat &src) {
}
static bool findLightBlobs(const cv::Mat &src, LightBlobs &light_blobs) {
static cv::Mat src_bin;
// static cv::Mat src_bin;
cv::threshold(src, src_bin, 80, 255, CV_THRESH_BINARY);
std::vector<std::vector<cv::Point> > light_contours;
cv::findContours(src_bin, light_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::findContours(src, light_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
for (auto &light_contour : light_contours) {
cv::RotatedRect rect = cv::minAreaRect(light_contour);
if(isValidLightBlob(rect)){
@@ -117,7 +115,7 @@ static bool findArmorBoxes(LightBlobs &light_blobs, std::vector<cv::Rect2d> &arm
min_x = fmin(rect_left.x, rect_right.x);
max_x = fmax(rect_left.x + rect_left.width, rect_right.x + rect_right.width);
min_y = fmin(rect_left.y, rect_right.y) - 5;
max_y = fmax(rect_left.y + rect_left.height, rect_right.y + rect_right.height) + 5;
max_y = fmax(rect_left.y + rect_left.height, rect_right.y + rect_right.height);
if (min_x < 0 || max_x > 640 || min_y < 0 || max_y > 480) {
continue;
}
@@ -149,19 +147,21 @@ bool judge_light_color(std::vector<LightBlob> &light, std::vector<LightBlob> &co
}
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
cv::Mat split, pmsrc=src.clone();
cv::Mat split, pmsrc=src.clone(), src_bin;
LightBlobs light_blobs, pm_light_blobs, light_blobs_real;
std::vector<cv::Rect2d> armor_boxes, boxes_one, boxes_two, boxes_three;
// cv::resize(src, pmsrc, cv::Size(320, 240));
imageColorSplit(src, split, enemy_color);
imagePreProcess(split);
cv::resize(split, split, cv::Size(640, 480));
cv::threshold(split, src_bin, 130, 255, CV_THRESH_BINARY);
imagePreProcess(src_bin);
cv::imshow("bin", src_bin);
// cv::resize(split, split, cv::Size(640, 480));
// pipelineLightBlobPreprocess(pmsrc);
// if(!findLightBlobs(pmsrc, pm_light_blobs)){
// return false;
// }
if(!findLightBlobs(split, light_blobs)){
if(!findLightBlobs(src_bin, light_blobs)){
return false;
}
// if(!judge_light_color(light_blobs, pm_light_blobs, light_blobs_real)){
@@ -182,10 +182,7 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
for(auto box : armor_boxes){
cv::Mat roi = src(box).clone();
cv::resize(roi, roi, cv::Size(48, 36));
// cv::imshow("roi", roi);
// cv::waitKey(0);
int c = classifier(roi);
// cout << c << endl;
switch(c){
case 1:
boxes_one.emplace_back(box);
@@ -204,6 +201,8 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
armor_box = boxes_two[0];
}else if(!boxes_three.empty()){
armor_box = boxes_three[0];
} else{
return false;
}
if(show_armor_box){
showArmorBoxClass("class", src, boxes_one, boxes_two, boxes_three);
@@ -211,10 +210,6 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
}else{
armor_box = armor_boxes[0];
}
if(show_armor_box){
showArmorBox("box", src, armor_box);
cv::waitKey(1);
}
if(split.size() == cv::Size(320, 240)){
armor_box.x *= 2;
armor_box.y *= 2;

View File

@@ -5,16 +5,18 @@
#include <armor_finder/armor_finder.h>
bool ArmorFinder::stateTrackingTarget(cv::Mat &src) {
auto last = armor_box;
tracker->update(src, armor_box);
if(!tracker->update(src, armor_box)){
return false;
}
if((armor_box & cv::Rect2d(0, 0, 640, 480)) != armor_box){
return false;
}
cv::Mat roi = src(armor_box);
threshold(roi, roi, 200, 255, cv::THRESH_BINARY);
if(abs(cv::countNonZero(roi) - contour_area) > contour_area * 0.3){
cv::Mat roi = src.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);
if(abs(cv::countNonZero(roi_gray) - contour_area) > contour_area * 0.3){
return false;
}
return sendBoxPosition();