From cb6cf4b2fc15ae2af15a3b988de0cf46fe416500 Mon Sep 17 00:00:00 2001 From: xinyang <895639507@qq.com> Date: Tue, 2 Jul 2019 20:55:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=9C=BA=E5=90=AF=E5=8A=A8=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E7=8E=B0=E5=9C=A8=E4=BC=9A=E5=B0=9D=E8=AF=95=E4=BB=8E?= =?UTF-8?q?github=E4=B8=8A=E6=9B=B4=E6=96=B0=E5=BD=93=E5=89=8D=E5=88=86?= =?UTF-8?q?=E6=94=AF=E7=9A=84=E6=9C=80=E6=96=B0=E4=BB=A3=E7=A0=81=E5=B9=B6?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E8=BF=90=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- .../searching_state/searching_state.cpp | 113 ++++++++++++------ main.cpp | 22 ++-- others/include/log.h | 7 +- others/src/camera/camera_wrapper.cpp | 6 +- tools/{bind-monitor.sh => create-startup.sh} | 7 +- 6 files changed, 102 insertions(+), 55 deletions(-) rename tools/{bind-monitor.sh => create-startup.sh} (51%) mode change 100644 => 100755 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1417650..dae52f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,4 +46,4 @@ ELSE () MESSAGE(STATUS "Unsupport platform: ${CMAKE_SYSTEM_NAME}") ENDIF() -ADD_CUSTOM_TARGET(bind-monitor COMMAND "${PROJECT_SOURCE_DIR}/tools/bind-monitor.sh" "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}") \ No newline at end of file +ADD_CUSTOM_TARGET(create-startup COMMAND "${PROJECT_SOURCE_DIR}/tools/create-startup.sh" "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}") \ No newline at end of file diff --git a/armor/src/armor_finder/state_machine/searching_state/searching_state.cpp b/armor/src/armor_finder/state_machine/searching_state/searching_state.cpp index d9f24c5..16f6c78 100644 --- a/armor/src/armor_finder/state_machine/searching_state/searching_state.cpp +++ b/armor/src/armor_finder/state_machine/searching_state/searching_state.cpp @@ -28,31 +28,90 @@ bool rectangleContainPoint(cv::RotatedRect rectangle, cv::Point2f point) double indicator = pointPolygonTest(contour,point,true); return indicator >= 0; } + /// Todo: 下面的函数可以有性能优化,暂时未做。 -static double nonZeroRateOfRotateRect(const cv::Mat &bin, const cv::RotatedRect &rorect){ - auto rect = rorect.boundingRect(); +static double nonZeroRateOfRotateRect(const cv::Mat &bin, const cv::RotatedRect &rotrect){ + auto rect = rotrect.boundingRect(); if(rect.x < 0 || rect.y < 0 || rect.x+rect.width > bin.cols || rect.y+rect.height > bin.rows){ -// cout << "break" << endl; return 0; } auto roi=bin(rect); int cnt=0; for(int r=0; r(r, c)){ cnt++; } } } } - return double(cnt) / rorect.size.area(); + return double(cnt) / rotrect.size.area(); +} + +int linePointX(const cv::Point2f &p1, const cv::Point2f &p2, int y){ + return (p2.x-p1.x)/(p2.y-p1.y)*(y-p1.y)+p1.x; +} + +///Todo: 性能优化后的函数。(暂时还有点问题) +static double nonZeroRateOfRotateRect_opt(const cv::Mat &bin, const cv::RotatedRect &rotrect){ + int cnt=0; + cv::Point2f corners[4]; + rotrect.points(corners); + sort(corners, &corners[4], [](cv::Point2f p1, cv::Point2f p2){ + return p1.y < p2.y; + }); +// for(int r=corners[0].y; r(c, r)){ +// cnt++; +// } +// } +// } + for(int r=corners[0].y; r640) return 0; + for(int c=start; c(c, r)){ + cnt++; + } + } + } + for(int r=corners[1].y; r640) return 0; + for(int c=start; c(r, c)){ + cnt++; + } + } + } + for(int r=corners[2].y; r640) return 0; + for(int c=start; c(c, r)){ + cnt++; + } + } + } + return double(cnt) / rotrect.size.area(); } static bool isValidLightBlob(const cv::Mat &bin, const cv::RotatedRect &rect){ - return (lw_rate(rect) > 1.5) && + return (lw_rate(rect) > 1.8) && // (rect.size.width*rect.size.height < 3000) && (rect.size.width*rect.size.height > 1) && +// (nonZeroRateOfRotateRect_opt(bin, rect) > 0.8); (nonZeroRateOfRotateRect(bin, rect) > 0.8); } @@ -70,7 +129,7 @@ static bool findLightBlobs(const cv::Mat &src, LightBlobs &light_blobs) { }else if(src.type() == CV_8UC1){ src_gray = src.clone(); } - + LightBlobs all; std::vector > light_contours; cv::findContours(src_gray, light_contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE); @@ -79,12 +138,13 @@ static bool findLightBlobs(const cv::Mat &src, LightBlobs &light_blobs) { if(isValidLightBlob(src_gray, rect)){ light_blobs.emplace_back(rect); } + all.emplace_back(rect); } + showContours("all", src, all); return light_blobs.size() >= 2; } bool angelJudge(const LightBlob &light_blob_i, const LightBlob &light_blob_j) { - float angle_i = light_blob_i.rect.size.width > light_blob_i.rect.size.height ? light_blob_i.rect.angle : light_blob_i.rect.angle - 90; float angle_j = light_blob_j.rect.size.width > light_blob_j.rect.size.height ? light_blob_j.rect.angle : @@ -93,7 +153,6 @@ bool angelJudge(const LightBlob &light_blob_i, const LightBlob &light_blob_j) { } bool heightJudge(const LightBlob &light_blob_i, const LightBlob &light_blob_j) { cv::Point2f centers = light_blob_i.rect.center - light_blob_j.rect.center; - return abs(centers.y)<30; } @@ -101,38 +160,18 @@ bool lengthJudge(const LightBlob &light_blob_i, const LightBlob &light_blob_j) { double side_length; cv::Point2f centers = light_blob_i.rect.center - light_blob_j.rect.center; side_length = sqrt(centers.ddot(centers)); -// std::cout << "side:" << side_length << " length:" << light_blob_i.length << std::endl; return (side_length / light_blob_i.length < 6 && side_length / light_blob_i.length > 0.5); } bool lengthRatioJudge(const LightBlob &light_blob_i, const LightBlob &light_blob_j) { -// std::cout << "i:" << light_blob_i.length << " j:" << light_blob_j.length << std::endl; return (light_blob_i.length / light_blob_j.length < 2 && light_blob_i.length / light_blob_j.length > 0.5); } bool isCoupleLight(const LightBlob &light_blob_i, const LightBlob &light_blob_j, uint8_t enemy_color) { - if(light_blob_i.BlobColor != enemy_color || light_blob_j.BlobColor != enemy_color){ - return false; - } - if(!lengthRatioJudge(light_blob_i, light_blob_j)){ -// std::cout << "lengthRatioJudge" << std::endl; - return false; - } - if(!lengthJudge(light_blob_i, light_blob_j)){ -// std::cout << "lengthJudge" << std::endl; - return false; - } - if(!heightJudge(light_blob_i, light_blob_j)){ -// std::cout << "heightJudge" << std::endl; - return false; - } - if(!angelJudge(light_blob_i, light_blob_j)){ -// std::cout << "angelJudge" << std::endl; - return false; - } - return true; - return lengthRatioJudge(light_blob_i, light_blob_j) && + return light_blob_i.BlobColor == enemy_color && + light_blob_j.BlobColor == enemy_color && + lengthRatioJudge(light_blob_i, light_blob_j) && lengthJudge(light_blob_i, light_blob_j) && heightJudge(light_blob_i, light_blob_j) && angelJudge(light_blob_i, light_blob_j); @@ -156,8 +195,8 @@ static bool findArmorBoxes(LightBlobs &light_blobs, std::vector &arm double min_x, min_y, max_x, max_y; min_x = fmin(rect_left.x, rect_right.x) - 4; max_x = fmax(rect_left.x + rect_left.width, rect_right.x + rect_right.width) + 4; - min_y = fmin(rect_left.y, rect_right.y) - 4; - max_y = fmax(rect_left.y + rect_left.height, rect_right.y + rect_right.height) + 4; + min_y = fmin(rect_left.y, rect_right.y) - 0.3*(rect_left.height+rect_right.height)/2.0; + max_y = fmax(rect_left.y + rect_left.height, rect_right.y + rect_right.height) + 0.3*(rect_left.height+rect_right.height)/2.0; if (min_x < 0 || max_x > 640 || min_y < 0 || max_y > 480) { continue; } @@ -216,14 +255,18 @@ int prior_red[] = {0, 2, 3, 4, 1, 5, 7, 8, 9, 6}; int prior_blue[]= {5, 7, 8, 9, 6, 0, 2, 3, 4, 1}; bool ArmorFinder::stateSearchingTarget(cv::Mat &src) { - cv::Mat split, src_bin; + cv::Mat split, src_bin/*, edge*/; LightBlobs light_blobs, light_blobs_, light_blobs_real; std::vector armor_boxes, boxes_number[10]; armor_box = cv::Rect2d(0,0,0,0); cv::cvtColor(src, src_gray, CV_BGR2GRAY); +// cv::Canny(src_gray, edge, 100, 150); +// src_gray -= edge; +// cv::imshow("minus", src_gray); // pipelineLightBlobPreprocess(src_gray); cv::threshold(src_gray, src_bin, 170, 255, CV_THRESH_BINARY); + imagePreProcess(src_bin); // imshow("gray bin", src_bin); if(!findLightBlobs(src_bin, light_blobs)){ return false; diff --git a/main.cpp b/main.cpp index b2f2b79..34fb53c 100644 --- a/main.cpp +++ b/main.cpp @@ -32,7 +32,7 @@ mcu_data mcuData = { ARMOR_STATE, 0, 1, - ENEMY_BLUE, + ENEMY_RED, }; int main(int argc, char *argv[]) { @@ -57,8 +57,8 @@ int main(int argc, char *argv[]) { WrapperHead *video_armor=nullptr; WrapperHead *video_energy=nullptr; if (from_camera) { - video_armor = new CameraWrapper(0/*, "armor"*/); -// video_energy = new CameraWrapper(1, "energy"); + video_armor = new CameraWrapper(0, "armor"); + video_energy = new CameraWrapper(1, "energy"); } else { // string armor_video, energy_video; // lastVideo(armor_video, PROJECT_DIR"/armor_video/"); @@ -66,7 +66,7 @@ int main(int argc, char *argv[]) { // lastVideo(energy_video, PROJECT_DIR"/energy_video/"); // video_energy = new VideoWrapper(energy_video); video_armor = new VideoWrapper("/home/sjturm/Desktop/valid_video/armor/65.avi"); -// video_energy = new VideoWrapper("/home/sjturm/Desktop/valid_video/energy/121.avi"); + video_energy = new VideoWrapper("/home/sjturm/Desktop/valid_video/energy/121.avi"); } if (video_armor->init()) { LOGM("video_armor source initialization successfully."); @@ -75,13 +75,13 @@ int main(int argc, char *argv[]) { delete video_armor; video_armor = nullptr; } -// if (video_energy->init()) { -// LOGM("video_energy source initialization successfully."); -// } else { -// LOGW("video_energy source unavailable!"); -// delete video_energy; -// video_energy = nullptr; -// } + if (video_energy->init()) { + LOGM("video_energy source initialization successfully."); + } else { + LOGW("video_energy source unavailable!"); + delete video_energy; + video_energy = nullptr; + } Mat energy_src, armor_src; for (int i = 0; i < 10; i++) { diff --git a/others/include/log.h b/others/include/log.h index 51b4724..f923133 100644 --- a/others/include/log.h +++ b/others/include/log.h @@ -139,7 +139,7 @@ GetLocalTime(&te); \ LOGM(tag": %dms", (te.wSecond-ts.wSecond)*1000+(te.wMilliseconds-ts.wMilliseconds)); \ }while (0) - #else + #elif defined(Linux) #include #define CNT_TIME(tag, codes, ...) do{ \ static timeval ts, te; \ @@ -148,7 +148,10 @@ gettimeofday(&te, NULL); \ LOGM(tag": %.1lfms", (te.tv_sec-ts.tv_sec)*1000.0+(te.tv_usec-ts.tv_usec)/1000.0); \ }while (0) - #endif + #else + #warning "Unsupport plantform for CNT_TIME" + #define CNT_TIME(tag, codes, ...) codes + #endif #else #define CNT_TIME(tag, codes, ...) codes #endif diff --git a/others/src/camera/camera_wrapper.cpp b/others/src/camera/camera_wrapper.cpp index e000f93..3d4a6cc 100644 --- a/others/src/camera/camera_wrapper.cpp +++ b/others/src/camera/camera_wrapper.cpp @@ -75,10 +75,10 @@ bool CameraWrapper::init() { LOGM("successfully loaded %s!", filepath); #elif defined(Linux) CameraSetAeState(h_camera, false); - CameraSetExposureTime(h_camera, 8*1000); - CameraSetAnalogGain(h_camera, 16); + CameraSetExposureTime(h_camera, 10*1000); + CameraSetAnalogGain(h_camera, 20); if(mode == 0){ - CameraSetGain(h_camera, 100, 131, 113); + CameraSetGain(h_camera, 100, 130, 112); CameraSetLutMode(h_camera, LUTMODE_PRESET); } #endif diff --git a/tools/bind-monitor.sh b/tools/create-startup.sh old mode 100644 new mode 100755 similarity index 51% rename from tools/bind-monitor.sh rename to tools/create-startup.sh index 1a7c3c1..c81e515 --- a/tools/bind-monitor.sh +++ b/tools/create-startup.sh @@ -1,5 +1,6 @@ #!/bin/bash -echo "#!/bin/bash" > $2/monitor-run -echo "gnome-terminal -- bash -c \"echo sjturm | sudo -S $1/tools/monitor.sh \\\"$2/run --run-with-camera --save-video --wait-uart\\\"\"" >> $2/monitor-run -chmod +x $2/monitor-run +echo "#!/bin/bash" > $2/startup-run +echo "$1/tools/auto-pull.sh" >> $2/startup-run +echo "gnome-terminal -- bash -c \"echo sjturm | sudo -S $1/tools/monitor.sh \\\"$2/run --run-with-camera --save-video --wait-uart\\\"\"" >> $2/startup-run +chmod +x $2/startup-run