修改了摄像头读取方式

This commit is contained in:
xinyang
2019-04-27 16:16:53 +08:00
parent 9cfd26cc23
commit 4e47b38d7d
15 changed files with 272 additions and 255 deletions

View File

@@ -13,5 +13,8 @@
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);
#endif /* _SHOW_IMAGES_H_ */

View File

@@ -22,14 +22,15 @@ ArmorFinder::ArmorFinder(EnemyColor color, Uart &u, string paras_folder) :
void ArmorFinder::run(cv::Mat &src) {
cv::Mat src_use;
if (src.type() == CV_8UC3) {
cv::cvtColor(src, src_use, CV_RGB2GRAY);
}else{
// if (src.type() == CV_8UC3) {
// cv::cvtColor(src, src_use, CV_RGB2GRAY);
// }else{
src_use = src.clone();
}
cv::cvtColor(src_use, src_gray, CV_BayerBG2GRAY);
// }
cv::cvtColor(src_use, src_gray, CV_RGB2GRAY);
// return stateSearchingTarget(src_use);
stateSearchingTarget(src_use);
return;
switch (state){
case SEARCHING_STATE:
@@ -49,7 +50,7 @@ void ArmorFinder::run(cv::Mat &src) {
}
break;
case TRACKING_STATE:
if(!stateTrackingTarget(src_use)){
if(!stateTrackingTarget(src_gray)){
state = SEARCHING_STATE;
//std::cout << "into search!" << std::endl;
}

View File

@@ -258,6 +258,9 @@ Classifier::Classifier(const string &folder) : state(true){
fc1_b = load_fc_b(folder+"fc1_b");
fc2_w = load_fc_w(folder+"fc2_w");
fc2_b = load_fc_b(folder+"fc2_b");
if(state){
LOGM("Load paras success!");
}
}
//#define PRINT_MAT(name) (cout << #name":\n" << name << endl)
@@ -285,10 +288,16 @@ Classifier::operator bool() const {
}
int Classifier::operator()(const cv::Mat &image) {
MatrixXd x;
cv2eigen(image, x);
x /= 255.0;
vector<MatrixXd> sub = {x};
MatrixXd r, g, b;
std::vector<cv::Mat> channels;
cv::split(image, channels);
cv2eigen(channels[0], b);
cv2eigen(channels[1], g);
cv2eigen(channels[2], r);
r /= 255;
g /= 255;
b /= 255;
vector<MatrixXd> sub = {b, g, r};
vector<vector<MatrixXd>> in = {sub};
MatrixXd result = calculate(in);
MatrixXd::Index minRow, minCol;

View File

@@ -36,15 +36,17 @@ void imageColorSplit(cv::Mat &src_input, cv::Mat &split, EnemyColor color) {
resize(channels.at(0), blue, cv::Size(640, 480));
resize(channels.at(2), red, cv::Size(640, 480));
if(color == ENEMY_RED){
split = red;
split = red - blue;
}else if(color == ENEMY_BLUE){
split = blue;
split = blue - red;
}
}
}
void imagePreProcess(cv::Mat &src) {
// cv::medianBlur(src, src, 5);
static cv::Mat kernel_erode = getStructuringElement(cv::MORPH_RECT, cv::Size(1, 4));
erode(src, src, kernel_erode);

View File

@@ -114,8 +114,8 @@ static bool findArmorBoxes(LightBlobs &light_blobs, std::vector<cv::Rect2d> &arm
cv::Rect2d rect_left = light_blobs.at(static_cast<unsigned long>(i)).rect.boundingRect();
cv::Rect2d rect_right = light_blobs.at(static_cast<unsigned long>(j)).rect.boundingRect();
double min_x, min_y, max_x, max_y;
min_x = fmin(rect_left.x, rect_right.x) - 5;
max_x = fmax(rect_left.x + rect_left.width, rect_right.x + rect_right.width) + 5;
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;
if (min_x < 0 || max_x > 640 || min_y < 0 || max_y > 480) {
@@ -151,7 +151,7 @@ bool judge_light_color(std::vector<LightBlob> &light, std::vector<LightBlob> &co
bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
cv::Mat split, pmsrc=src.clone();
LightBlobs light_blobs, pm_light_blobs, light_blobs_real;
std::vector<cv::Rect2d> armor_boxes;
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);
@@ -174,20 +174,45 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
if(!findArmorBoxes(light_blobs, armor_boxes)){
return false;
}
if(show_armor_boxes){
showArmorBoxVector("boxes", split, armor_boxes);
cv::waitKey(1);
}
if(classifier){
for(const auto &box : armor_boxes){
for(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;
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);
break;
case 2:
boxes_two.emplace_back(box);
break;
case 3:
boxes_three.emplace_back(box);
break;
}
}
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];
}
if(show_armor_box){
showArmorBoxClass("class", src, boxes_one, boxes_two, boxes_three);
}
}else{
armor_box = armor_boxes[0];
}
if(show_armor_boxes){
showArmorBoxVector("boxes", split, armor_boxes);
if(show_armor_box){
showArmorBox("box", src, armor_box);
cv::waitKey(1);
}
if(split.size() == cv::Size(320, 240)){
@@ -196,6 +221,5 @@ bool ArmorFinder::stateSearchingTarget(cv::Mat &src) {
armor_box.width *= 2;
armor_box.height *= 2;
}
return sendBoxPosition();
}

View File

@@ -18,6 +18,28 @@ 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){
static Mat image2show;
if (src.type() == CV_8UC1) // 黑白图像
{
cvtColor(src, image2show, COLOR_GRAY2RGB);
} else if(src.type() == CV_8UC3) //RGB 彩色
{
image2show = src.clone();
}
for (auto &box:boxes_one) {
rectangle(image2show, box, Scalar(255, 0, 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);
}
void showArmorBox(std::string windows_name, const cv::Mat &src, cv::Rect2d armor_box) {
static Mat image2show;
if (src.type() == CV_8UC1) // 黑白图像