使用旧版候选区寻找方式。

This commit is contained in:
xinyang
2019-05-02 20:19:17 +08:00
parent ee83a373d3
commit 2f20367677
20 changed files with 12502 additions and 12395 deletions

View File

@@ -13,8 +13,7 @@
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);
void showContours(std::string windows_name, const cv::Mat &src, const std::vector<LightBlob> &light_blobs);
void showArmorBoxClass(std::string window_names, const cv::Mat &src, vector<cv::Rect2d> boxes_one,
vector<cv::Rect2d> boxes_two, vector<cv::Rect2d> boxes_three);
void showArmorBoxClass(std::string window_names, const cv::Mat &src, vector<cv::Rect2d> boxes[10]);
#endif /* _SHOW_IMAGES_H_ */

View File

@@ -1,6 +1,7 @@
//
// Created by xinyang on 19-3-27.
//
#define LOG_LEVEL LOG_NONE
#include <log.h>
#include <options/options.h>
#include <show_images/show_images.h>
@@ -40,14 +41,14 @@ void ArmorFinder::run(cv::Mat &src) {
tracker = TrackerToUse::create();
tracker->init(src_use, armor_box);
state = TRACKING_STATE;
LOGW("into track");
LOGM(STR_CTR(WORD_LIGHT_CYAN, "into track"));
}
}
break;
case TRACKING_STATE:
if(!stateTrackingTarget(src_use)){
state = SEARCHING_STATE;
//std::cout << "into search!" << std::endl;
LOGM(STR_CTR(WORD_LIGHT_YELLOW ,"into search!"));
}
break;
case STANDBY_STATE:
@@ -59,11 +60,14 @@ void ArmorFinder::run(cv::Mat &src) {
#define FOCUS_PIXAL (0.36/0.48*640)
bool ArmorFinder::sendBoxPosition() {
static int dx_add = 0;
auto rect = armor_box;
double dx = rect.x + rect.width/2 - 320;
double dy = rect.y + rect.height/2 - 240;
double dx = rect.x + rect.width/2 - 320 - 8;
dx_add += dx;
dx = dx + dx_add * 0;
double dy = rect.y + rect.height/2 - 240 - 30;
double yaw = atan(dx / FOCUS_PIXAL) * 180 / 3.14159265459;
double pitch = atan(dy / FOCUS_PIXAL) * 180 / 3.14159265459;
uart.sendTarget(yaw, pitch, 0);
uart.sendTarget(yaw, -pitch, 0);
return true;
}

View File

@@ -92,7 +92,7 @@ MatrixXd Classifier::softmax(const MatrixXd &input){
return tmp.array().exp() / tmp.array().exp().sum();
}
vector<vector<MatrixXd>> max_pool(const vector<vector<MatrixXd>> &input, int size){
vector<vector<MatrixXd>> Classifier::max_pool(const vector<vector<MatrixXd>> &input, int size){
vector<vector<MatrixXd>> output;
for(int sample=0; sample<input.size(); sample++) {
vector<MatrixXd> sub;

View File

@@ -31,10 +31,15 @@ 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_gray, src_bin;
if(src.type() == CV_8UC3){
cvtColor(src, src_gray, CV_BGR2GRAY);
}else if(src.type() == CV_8UC1){
src_gray = src.clone();
}
std::vector<std::vector<cv::Point> > light_contours;
cv::findContours(src, light_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::findContours(src_gray, 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)){
@@ -114,8 +119,8 @@ static bool findArmorBoxes(LightBlobs &light_blobs, std::vector<cv::Rect2d> &arm
double min_x, min_y, max_x, max_y;
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);
min_y = fmin(rect_left.y, rect_right.y) - 3;
max_y = fmax(rect_left.y + rect_left.height, rect_right.y + rect_right.height) + 3;
if (min_x < 0 || max_x > 640 || min_y < 0 || max_y > 480) {
continue;
}
@@ -147,31 +152,42 @@ bool judge_light_color(std::vector<LightBlob> &light, std::vector<LightBlob> &co
}
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
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::Mat split, src_bin;
LightBlobs light_blobs, light_blobs_, light_blobs_real;
std::vector<cv::Rect2d> armor_boxes, boxes_number[9];
armor_box = cv::Rect2d(0,0,0,0);
// cv::resize(src, pmsrc, cv::Size(320, 240));
imageColorSplit(src, split, enemy_color);
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;
// }
cv::cvtColor(src, src_gray, CV_BGR2GRAY);
// pipelineLightBlobPreprocess(src_gray);
cv::threshold(src_gray, src_bin, 120, 255, CV_THRESH_BINARY);
if(!findLightBlobs(src_bin, light_blobs)){
return false;
}
// if(!judge_light_color(light_blobs, pm_light_blobs, light_blobs_real)){
// return false;
// }
if(show_light_blobs){
showContours("blobs", split, light_blobs);
showContours("blobs", src_bin, light_blobs);
cv::waitKey(1);
}
if(!findArmorBoxes(light_blobs, armor_boxes)){
imageColorSplit(src, split, enemy_color);
imagePreProcess(split);
cv::threshold(split, src_bin, 120, 255, CV_THRESH_BINARY);
if(!findLightBlobs(src_bin, light_blobs_)){
return false;
}
if(show_light_blobs){
showContours("blobs_", src_bin, light_blobs_);
cv::waitKey(1);
}
if(!judge_light_color(light_blobs, light_blobs_, light_blobs_real)){
return false;
}
if(show_light_blobs){
showContours("blobs_real", src, light_blobs_real);
cv::waitKey(1);
}
if(!findArmorBoxes(light_blobs_real, armor_boxes)){
return false;
}
if(show_armor_boxes){
@@ -183,29 +199,21 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
cv::Mat roi = src(box).clone();
cv::resize(roi, roi, cv::Size(48, 36));
int c = classifier(roi);
switch(c){
case 1:
boxes_one.emplace_back(box);
break;
case 2:
boxes_two.emplace_back(box);
break;
case 3:
boxes_three.emplace_back(box);
break;
if(c){
boxes_number[c-1].emplace_back(box);
}
}
if(!boxes_one.empty()){
armor_box = boxes_one[0];
}else if(!boxes_two.empty()){
armor_box = boxes_two[0];
}else if(!boxes_three.empty()){
armor_box = boxes_three[0];
} else{
for(auto box : boxes_number){
if(!box.empty()){
armor_box = box[0];
}
}
if(armor_box == cv::Rect2d(0,0,0,0)){
return false;
}
if(show_armor_box){
showArmorBoxClass("class", src, boxes_one, boxes_two, boxes_three);
if(show_armor_boxes){
showArmorBoxClass("class", src, boxes_number);
}
}else{
armor_box = armor_boxes[0];

View File

@@ -18,8 +18,7 @@ void showArmorBoxVector(std::string windows_name, const cv::Mat &src, const std:
imshow(windows_name, image2show);
}
void showArmorBoxClass(std::string windows_name, const cv::Mat &src, vector<cv::Rect2d> boxes_one,
vector<cv::Rect2d> boxes_two, vector<cv::Rect2d> boxes_three){
void showArmorBoxClass(std::string window_names, const cv::Mat &src, vector<cv::Rect2d> boxes[10]){
static Mat image2show;
if (src.type() == CV_8UC1) // 黑白图像
{
@@ -28,16 +27,32 @@ void showArmorBoxClass(std::string windows_name, const cv::Mat &src, vector<cv::
{
image2show = src.clone();
}
for (auto &box:boxes_one) {
rectangle(image2show, box, Scalar(255, 0, 0), 1);
if(!boxes[0].empty()){
for(auto box : boxes[0]){
cv::rectangle(image2show, box, Scalar(255, 0, 0), 1);
}
}else if(!boxes[1].empty()){
for(auto box : boxes[1]){
cv::rectangle(image2show, box, Scalar(0, 255, 0), 1);
}
}else if(!boxes[2].empty()){
for(auto box : boxes[2]){
cv::rectangle(image2show, box, Scalar(0, 0, 255), 1);
}
}else if(!boxes[3].empty()){
for(auto box : boxes[3]){
cv::rectangle(image2show, box, Scalar(0, 255, 255), 1);
}
}else if(!boxes[4].empty()){
for(auto box : boxes[4]){
cv::rectangle(image2show, box, Scalar(255, 0, 255), 1);
}
}else if(!boxes[5].empty()){
for(auto box : boxes[5]){
cv::rectangle(image2show, box, Scalar(255, 255, 0), 1);
}
}
for (auto &box:boxes_two) {
rectangle(image2show, box, Scalar(0, 255, 0), 1);
}
for (auto &box:boxes_three) {
rectangle(image2show, box, Scalar(0, 0, 255), 1);
}
imshow(windows_name, image2show);
imshow(window_names, image2show);
}
void showArmorBox(std::string windows_name, const cv::Mat &src, cv::Rect2d armor_box) {