From c7b76ea9ba76568f3928f0310780527865dbf2e1 Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Tue, 5 Nov 2024 11:02:54 +0100 Subject: [PATCH] refreshed code --- src/common/areas/CMakeLists.txt | 7 ++-- src/common/areas/areas.cpp | 25 +++++++------ src/common/areas/roomsSegmentation2.cpp | 49 +++++++++++++------------ 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/common/areas/CMakeLists.txt b/src/common/areas/CMakeLists.txt index f159f06a..cca157cd 100644 --- a/src/common/areas/CMakeLists.txt +++ b/src/common/areas/CMakeLists.txt @@ -6,10 +6,12 @@ project(areas) -file(GLOB folder_source *.cpp) +#file(GLOB folder_source areas.cpp) +file(GLOB folder_source roomsSegmentation2.cpp) + file(GLOB folder_header *.h) -find_package(YARP 3.3 COMPONENTS os sig dev math gsl idl_tools REQUIRED) +find_package(YARP 3.9 COMPONENTS os sig dev math gsl idl_tools REQUIRED) find_package(OpenCV) source_group("Source Files" FILES ${folder_source}) @@ -19,4 +21,3 @@ include_directories(${OpenCV_INCLUDE_DIRS} ${ICUB_INCLUDE_DIRS}) add_executable(${PROJECT_NAME} ${folder_source} ${folder_header}) target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES} ${YARP_LIBRARIES} ${ICUB_LIBRARIES}) install(TARGETS ${PROJECT_NAME} DESTINATION bin) - diff --git a/src/common/areas/areas.cpp b/src/common/areas/areas.cpp index 18c1e3df..85548a52 100644 --- a/src/common/areas/areas.cpp +++ b/src/common/areas/areas.cpp @@ -1,6 +1,6 @@ -#include "opencv2/imgproc.hpp" -#include "opencv2/highgui.hpp" -#include +#include +#include +#include #include #include #include @@ -14,6 +14,7 @@ using namespace cv; using namespace std; using namespace yarp::os; using namespace yarp::dev; +using namespace yarp::dev::Nav2D; const int w = 500; int levels = 3; @@ -88,14 +89,14 @@ int main( int argc, char** argv) for (auto y = 0; y (x, y) = c; } #endif @@ -108,7 +109,7 @@ int main( int argc, char** argv) contours.resize(contours0.size()); for (size_t k = 0; k < contours0.size(); k++) { - yarp::dev::Map2DArea area; + Map2DArea area; double accuracy = 4; approxPolyDP(Mat(contours0[k]), contours[k], accuracy, true); #if SIMULATION_ONLY diff --git a/src/common/areas/roomsSegmentation2.cpp b/src/common/areas/roomsSegmentation2.cpp index 23f6595e..56741cac 100644 --- a/src/common/areas/roomsSegmentation2.cpp +++ b/src/common/areas/roomsSegmentation2.cpp @@ -1,6 +1,6 @@ -#include "opencv2/imgproc.hpp" -#include "opencv2/highgui.hpp" -#include +#include +#include +#include #include #include #include @@ -14,6 +14,7 @@ using namespace cv; using namespace std; using namespace yarp::os; using namespace yarp::dev; +using namespace yarp::dev::Nav2D; #define FROM_MAPS 1 //#define DRAW_WINDOWS 1 @@ -59,7 +60,7 @@ int main( int argc, char** argv) // Perform the distance transform algorithm cv::Mat dist; - distanceTransform(img, dist, CV_DIST_L2, 3); + distanceTransform(img, dist, DistanceTypes::DIST_L2, 3); // Normalize the distance image for range = {0.0, 1.0) normalize(dist, dist, 0, 1., cv::NORM_MINMAX); @@ -68,7 +69,7 @@ int main( int argc, char** argv) imshow( "distNorm", dist ); #endif // Threshold to obtain the peaks - threshold(dist, dist, .4, 1., CV_THRESH_BINARY); + threshold(dist, dist, .4, 1., ThresholdTypes::THRESH_BINARY); #ifdef DRAW_WINDOWS namedWindow( "distThresh", 1 ); @@ -93,7 +94,7 @@ int main( int argc, char** argv) // Find the contours std::vector > cnt; - findContours(dist_8u,cnt,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); + findContours(dist_8u,cnt, RetrievalModes::RETR_EXTERNAL, ContourApproximationModes::CHAIN_APPROX_NONE); // Get the moments and mass center (future use) @@ -107,8 +108,8 @@ int main( int argc, char** argv) for( size_t i = 0; i< cnt.size(); i++ ) { - // Draw the forground marker - drawContours(markers, cnt, static_cast(i), cv::Scalar::all(static_cast(i)+1), CV_FILLED); + // Draw the foreground marker + drawContours(markers, cnt, static_cast(i), cv::Scalar::all(static_cast(i)+1), LineTypes::FILLED); //collect moments and mass center (mc) of each contours. Currently I don't need them, maybe later.... mu[i] = moments( cnt[i], false ); @@ -124,7 +125,7 @@ int main( int argc, char** argv) imshow( "Marker1000", markers*10000 ); #endif - cv::cvtColor(img, img, CV_GRAY2RGB); + cv::cvtColor(img, img, ColorConversionCodes::COLOR_GRAY2RGB); watershed(img, markers); @@ -180,8 +181,8 @@ int main( int argc, char** argv) vector > contours0; vector hierarchy; - //findContours( im_gray, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); - findContours( im_gray, contours0, RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); + //findContours( im_gray, contours0, hierarchy, RETR_TREE, ContourApproximationModes::CHAIN_APPROX_SIMPLE); + findContours( im_gray, contours0, RETR_EXTERNAL, ContourApproximationModes::CHAIN_APPROX_NONE); Mat drawing0= Mat::zeros( im_gray.size(), CV_8UC1 ); @@ -204,7 +205,7 @@ int main( int argc, char** argv) vector > contours; //find contours, only external pixels? - findContours( drawingList[i], contours, RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); + findContours( drawingList[i], contours, RETR_EXTERNAL, ContourApproximationModes::CHAIN_APPROX_NONE); //calculate the max area maxArea[i]=contourArea(contours[0]); //one only contour for i-th room @@ -225,7 +226,7 @@ int main( int argc, char** argv) //contours11.resize(contours[0].size()); contours11.resize(1); - yarp::dev::Map2DArea area; + Map2DArea area; double accuracy = 3; approxPolyDP(Mat(contours0[i]), contours11[0], accuracy, true); #if SIMULATION_ONLY @@ -240,7 +241,7 @@ int main( int argc, char** argv) area.points.push_back(yarp::math::Vec2D(contours[k][kk].x, contours[k][kk].y)); yDebug() << k << " " << kk << " x:" << contours[k][kk].x << " y:" << contours[k][kk].y; #else - MapGrid2D::XYWorld worldc = the_map.cell2World(MapGrid2D::XYCell(contours11[0][kk].x, contours11[0][kk].y)); + XYWorld worldc = the_map.cell2World(XYCell(contours11[0][kk].x, contours11[0][kk].y)); area.points.push_back(yarp::math::Vec2D(worldc.x, worldc.y)); yDebug() << i << " " << kk << " x:" << contours11[0][kk].x << "(" << worldc.x << ") y:" << contours11[0][kk].y << " (" << worldc.y << ")"; #endif @@ -254,7 +255,7 @@ int main( int argc, char** argv) std::string area_name = "auto_area" + to_string(i); if (m_iMap->storeArea(area_name, area)) { - yInfo() << "Area " << area_name << "succesfully stored into map server"; + yInfo() << "Area " << area_name << "successfully stored into map server"; } else { @@ -310,19 +311,19 @@ static bool getImage(cv::Mat &img, std::string mapname) size_t w = the_map.width(); size_t h = the_map.height(); yInfo() << "get image of size " << w << h; - img = Mat::zeros(h,w, CV_8UC1); + img = Mat::zeros((int)h, (int)w, CV_8UC1); for (auto x=0; x(y, x) = c; }