From ff016a8ae9c2896ff5fe84c1a72313b4887e9200 Mon Sep 17 00:00:00 2001 From: YOUNG Date: Tue, 21 Mar 2023 23:11:49 +0800 Subject: [PATCH 1/3] :recycle: Refactor Code to OpenCV4 and Fixed. --- .clang-format | 213 ++ .vscode/c_cpp_properties.json | 18 + CMakeLists.txt | 31 +- Examples/Monocular/mono_euroc.cc | 6 +- Examples/Monocular/mono_kitti.cc | 6 +- Examples/Monocular/mono_tum.cc | 6 +- Examples/RGB-D/rgbd_tum.cc | 8 +- Examples/ROS/ORB_SLAM2/CMakeLists.txt | 20 +- Examples/Stereo/stereo_euroc.cc | 8 +- Examples/Stereo/stereo_kitti.cc | 8 +- KeyFrameTrajectory_Kitti00.txt | 1972 +++++++++++++++++ README.md | 4 +- Thirdparty/DBoW2/CMakeLists.txt | 6 +- .../g2o/g2o/core/sparse_block_matrix.hpp | 2 +- include/LoopClosing.h | 2 +- include/ORBextractor.h | 2 +- include/PnPsolver.h | 14 +- include/System.h | 1 + src/FrameDrawer.cc | 2 +- src/LocalMapping.cc | 1 + src/PnPsolver.cc | 135 +- src/Sim3Solver.cc | 2 +- src/Tracking.cc | 32 +- 23 files changed, 2352 insertions(+), 147 deletions(-) create mode 100644 .clang-format create mode 100644 .vscode/c_cpp_properties.json create mode 100644 KeyFrameTrajectory_Kitti00.txt diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..6fc239ea15 --- /dev/null +++ b/.clang-format @@ -0,0 +1,213 @@ +# 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto +Language: Cpp +# BasedOnStyle: LLVM + +# 访问说明符(public、private等)的偏移 +AccessModifierOffset: -4 + +# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行) +AlignAfterOpenBracket: Align + +# 连续赋值时,对齐所有等号 +AlignConsecutiveAssignments: true + +# 连续声明时,对齐所有声明的变量名 +AlignConsecutiveDeclarations: false + +# 右对齐逃脱换行(使用反斜杠换行)的反斜杠 +AlignEscapedNewlines: Right + +# 水平对齐二元和三元表达式的操作数 +AlignOperands: true + +# 对齐连续的尾随的注释 +AlignTrailingComments: true + +# 不允许函数声明的所有参数在放在下一行 +AllowAllParametersOfDeclarationOnNextLine: false + +# 不允许短的块放在同一行 +AllowShortBlocksOnASingleLine: true + +# 允许短的case标签放在同一行 +AllowShortCaseLabelsOnASingleLine: true + +# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All +AllowShortFunctionsOnASingleLine: None + +# 允许短的if语句保持在同一行 +AllowShortIfStatementsOnASingleLine: true + +# 允许短的循环保持在同一行 +AllowShortLoopsOnASingleLine: true + +# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), +# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义) +AlwaysBreakAfterReturnType: None + +# 总是在多行string字面量前换行 +AlwaysBreakBeforeMultilineStrings: false + +# 总是在template声明后换行 +AlwaysBreakTemplateDeclarations: true + +# false表示函数实参要么都在同一行,要么都各自一行 +BinPackArguments: true + +# false表示所有形参要么都在同一行,要么都各自一行 +BinPackParameters: true + +# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 +BraceWrapping: + # class定义后面 + AfterClass: false + # 控制语句后面 + AfterControlStatement: false + # enum定义后面 + AfterEnum: false + # 函数定义后面 + AfterFunction: false + # 命名空间定义后面 + AfterNamespace: false + # struct定义后面 + AfterStruct: false + # union定义后面 + AfterUnion: false + # extern之后 + AfterExternBlock: false + # catch之前 + BeforeCatch: false + # else之前 + BeforeElse: false + # 缩进大括号 + IndentBraces: false + # 分离空函数 + SplitEmptyFunction: false + # 分离空语句 + SplitEmptyRecord: false + # 分离空命名空间 + SplitEmptyNamespace: false + +# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) +BreakBeforeBinaryOperators: NonAssignment + +# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), +# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), +# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom +# 注:这里认为语句块也属于函数 +BreakBeforeBraces: Custom + +# 在三元运算符前换行 +BreakBeforeTernaryOperators: false + +# 在构造函数的初始化列表的冒号后换行 +BreakConstructorInitializers: AfterColon + +#BreakInheritanceList: AfterColon + +BreakStringLiterals: false + +# 每行字符的限制,0表示没有限制 +ColumnLimit: 0 + +CompactNamespaces: true + +# 构造函数的初始化列表要么都在同一行,要么都各自一行 +ConstructorInitializerAllOnOneLineOrOnePerLine: false + +# 构造函数的初始化列表的缩进宽度 +ConstructorInitializerIndentWidth: 4 + +# 延续的行的缩进宽度 +ContinuationIndentWidth: 4 + +# 去除C++11的列表初始化的大括号{后和}前的空格 +Cpp11BracedListStyle: true + +# 继承最常用的指针和引用的对齐方式 +DerivePointerAlignment: false + +# 固定命名空间注释 +FixNamespaceComments: true + +# 缩进case标签 +IndentCaseLabels: false + +IndentPPDirectives: None + +# 缩进宽度 +IndentWidth: 4 + +# 函数返回类型换行时,缩进函数声明或函数定义的函数名 +IndentWrappedFunctionNames: false + +# 保留在块开始处的空行 +KeepEmptyLinesAtTheStartOfBlocks: false + +# 连续空行的最大数量 +MaxEmptyLinesToKeep: 1 + +# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All +NamespaceIndentation: None + +# 指针和引用的对齐: Left, Right, Middle +PointerAlignment: Right + +# 允许重新排版注释 +ReflowComments: true + +# 允许排序#include +SortIncludes: false + +# 允许排序 using 声明 +SortUsingDeclarations: false + +# 在C风格类型转换后添加空格 +SpaceAfterCStyleCast: false + +# 在Template 关键字后面添加空格 +SpaceAfterTemplateKeyword: true + +# 在赋值运算符之前添加空格 +SpaceBeforeAssignmentOperators: true + +# SpaceBeforeCpp11BracedList: true + +# SpaceBeforeCtorInitializerColon: true + +# SpaceBeforeInheritanceColon: true + +# 开圆括号之前添加一个空格: Never, ControlStatements, Always +SpaceBeforeParens: ControlStatements + +# SpaceBeforeRangeBasedForLoopColon: true + +# 在空的圆括号中添加空格 +SpaceInEmptyParentheses: false + +# 在尾随的评论前添加的空格数(只适用于//) +SpacesBeforeTrailingComments: 1 + +# 在尖括号的<后和>前添加空格 +SpacesInAngles: false + +# 在C风格类型转换的括号中添加空格 +SpacesInCStyleCastParentheses: false + +# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格 +SpacesInContainerLiterals: true + +# 在圆括号的(后和)前添加空格 +SpacesInParentheses: false + +# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响 +SpacesInSquareBrackets: false + +# 标准: Cpp03, Cpp11, Auto +Standard: Cpp11 + +# tab宽度 +TabWidth: 4 + +# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always +UseTab: Never \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000..0a6a0c69c8 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/opencv4", + "/usr/include/eigen3" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 08a8af468e..465e9ff565 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,42 +7,45 @@ ENDIF() MESSAGE("Build type: " ${CMAKE_BUILD_TYPE}) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native") -# Check C++11 or C++0x support +# Check c++14 or C++0x support include(CheckCXXCompilerFlag) -CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14) CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) -if(COMPILER_SUPPORTS_CXX11) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - add_definitions(-DCOMPILEDWITHC11) - message(STATUS "Using flag -std=c++11.") +if(COMPILER_SUPPORTS_CXX14) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") + add_definitions(-DCOMPILEDWITHC14) + message(STATUS "Using flag -std=c++14.") elseif(COMPILER_SUPPORTS_CXX0X) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") add_definitions(-DCOMPILEDWITHC0X) message(STATUS "Using flag -std=c++0x.") else() - message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") + message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no c++14 support. Please use a different C++ compiler.") endif() LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules) -find_package(OpenCV 3.0 QUIET) +find_package(OpenCV 4.2 QUIET) if(NOT OpenCV_FOUND) - find_package(OpenCV 2.4.3 QUIET) + find_package(OpenCV 3.4.7 QUIET) if(NOT OpenCV_FOUND) - message(FATAL_ERROR "OpenCV > 2.4.3 not found.") + message(FATAL_ERROR "OpenCV > 3.4.7 not found.") endif() endif() +message(STATUS "OpenCV library status:") +message(STATUS " version: ${OpenCV_VERSION}") +message(STATUS " libraries: ${OpenCV_LIBS}") +message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") -find_package(Eigen3 3.1.0 REQUIRED) +find_package(Eigen3 3.1.0 REQUIRED NO_MODULE) find_package(Pangolin REQUIRED) include_directories( ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include -${EIGEN3_INCLUDE_DIR} ${Pangolin_INCLUDE_DIRS} ) diff --git a/Examples/Monocular/mono_euroc.cc b/Examples/Monocular/mono_euroc.cc index 4bcb90f170..1b17783f56 100644 --- a/Examples/Monocular/mono_euroc.cc +++ b/Examples/Monocular/mono_euroc.cc @@ -70,7 +70,7 @@ int main(int argc, char **argv) for(int ni=0; ni 2.4.3 not found.") + message(FATAL_ERROR "OpenCV > 3.4.7 not found.") endif() endif() diff --git a/Examples/Stereo/stereo_euroc.cc b/Examples/Stereo/stereo_euroc.cc index 6bc09c50be..67b574cdd5 100644 --- a/Examples/Stereo/stereo_euroc.cc +++ b/Examples/Stereo/stereo_euroc.cc @@ -116,8 +116,8 @@ int main(int argc, char **argv) for(int ni=0; ni 2.4.3 not found.") + message(FATAL_ERROR "OpenCV > 3.4.7 not found.") endif() endif() diff --git a/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp b/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp index 8dfa99c1b7..247e8d8668 100644 --- a/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp +++ b/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp @@ -648,7 +648,7 @@ namespace g2o { destColumnMap.insert(sparseRowSorted[0]); for (size_t j = 1; j < sparseRowSorted.size(); ++j) { typename SparseBlockMatrix::IntBlockMap::iterator hint = destColumnMap.end(); - --hint; // cppreference says the element goes after the hint (until C++11) + --hint; // cppreference says the element goes after the hint (until c++14) destColumnMap.insert(hint, sparseRowSorted[j]); } } diff --git a/include/LoopClosing.h b/include/LoopClosing.h index 7eb0416b15..c3adf13acd 100644 --- a/include/LoopClosing.h +++ b/include/LoopClosing.h @@ -47,7 +47,7 @@ class LoopClosing typedef pair,int> ConsistentGroup; typedef map, - Eigen::aligned_allocator > > KeyFrameAndPose; + Eigen::aligned_allocator > > KeyFrameAndPose; public: diff --git a/include/ORBextractor.h b/include/ORBextractor.h index 66e8e7a547..268148a1ee 100644 --- a/include/ORBextractor.h +++ b/include/ORBextractor.h @@ -23,7 +23,7 @@ #include #include -#include +#include namespace ORB_SLAM2 diff --git a/include/PnPsolver.h b/include/PnPsolver.h index f92544fc8f..2db4feb31c 100644 --- a/include/PnPsolver.h +++ b/include/PnPsolver.h @@ -93,16 +93,16 @@ class PnPsolver { void choose_control_points(void); void compute_barycentric_coordinates(void); - void fill_M(CvMat * M, const int row, const double * alphas, const double u, const double v); + void fill_M(cv::Mat * M, const int row, const double * alphas, const double u, const double v); void compute_ccs(const double * betas, const double * ut); void compute_pcs(void); void solve_for_sign(void); - void find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho, double * betas); - void find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho, double * betas); - void find_betas_approx_3(const CvMat * L_6x10, const CvMat * Rho, double * betas); - void qr_solve(CvMat * A, CvMat * b, CvMat * X); + void find_betas_approx_1(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas); + void find_betas_approx_2(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas); + void find_betas_approx_3(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas); + void qr_solve(cv::Mat * A, cv::Mat * b, cv::Mat * X); double dot(const double * v1, const double * v2); double dist2(const double * p1, const double * p2); @@ -110,9 +110,9 @@ class PnPsolver { void compute_rho(double * rho); void compute_L_6x10(const double * ut, double * l_6x10); - void gauss_newton(const CvMat * L_6x10, const CvMat * Rho, double current_betas[4]); + void gauss_newton(const cv::Mat * L_6x10, const cv::Mat * Rho, double current_betas[4]); void compute_A_and_b_gauss_newton(const double * l_6x10, const double * rho, - double cb[4], CvMat * A, CvMat * b); + double cb[4], cv::Mat * A, cv::Mat * b); double compute_R_and_t(const double * ut, const double * betas, double R[3][3], double t[3]); diff --git a/include/System.h b/include/System.h index b377b453d1..e05aa8eec4 100644 --- a/include/System.h +++ b/include/System.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "Tracking.h" #include "FrameDrawer.h" diff --git a/src/FrameDrawer.cc b/src/FrameDrawer.cc index e23b86c207..6a90b6afe4 100644 --- a/src/FrameDrawer.cc +++ b/src/FrameDrawer.cc @@ -72,7 +72,7 @@ cv::Mat FrameDrawer::DrawFrame() } // destroy scoped mutex -> release mutex if(im.channels()<3) //this should be always true - cvtColor(im,im,CV_GRAY2BGR); + cvtColor(im,im,cv::COLOR_GRAY2BGR); //Draw if(state==Tracking::NOT_INITIALIZED) //INITIALIZING diff --git a/src/LocalMapping.cc b/src/LocalMapping.cc index 6c87a6e55a..5396402c5b 100644 --- a/src/LocalMapping.cc +++ b/src/LocalMapping.cc @@ -24,6 +24,7 @@ #include "Optimizer.h" #include +#include namespace ORB_SLAM2 { diff --git a/src/PnPsolver.cc b/src/PnPsolver.cc index af3511b165..88efabd7f4 100644 --- a/src/PnPsolver.cc +++ b/src/PnPsolver.cc @@ -385,21 +385,19 @@ void PnPsolver::choose_control_points(void) // Take C1, C2, and C3 from PCA on the reference points: - CvMat * PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F); + cv::Mat PW0 = cv::Mat(number_of_correspondences, 3, CV_64F); double pw0tpw0[3 * 3], dc[3], uct[3 * 3]; - CvMat PW0tPW0 = cvMat(3, 3, CV_64F, pw0tpw0); - CvMat DC = cvMat(3, 1, CV_64F, dc); - CvMat UCt = cvMat(3, 3, CV_64F, uct); + cv::Mat PW0tPW0 = cv::Mat(3, 3, CV_64F, pw0tpw0); + cv::Mat DC = cv::Mat(3, 1, CV_64F, dc); + cv::Mat UCt = cv::Mat(3, 3, CV_64F, uct); for(int i = 0; i < number_of_correspondences; i++) for(int j = 0; j < 3; j++) - PW0->data.db[3 * i + j] = pws[3 * i + j] - cws[0][j]; + PW0.at(3 * i + j) = pws[3 * i + j] - cws[0][j]; - cvMulTransposed(PW0, &PW0tPW0, 1); - cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T); - - cvReleaseMat(&PW0); + cv::mulTransposed(PW0, PW0tPW0, 1); + cv::SVD::compute(PW0tPW0, DC, UCt, cv::Mat(), cv::SVD::MODIFY_A | cv::SVD::NO_UV); for(int i = 1; i < 4; i++) { double k = sqrt(dc[i - 1] / number_of_correspondences); @@ -411,14 +409,14 @@ void PnPsolver::choose_control_points(void) void PnPsolver::compute_barycentric_coordinates(void) { double cc[3 * 3], cc_inv[3 * 3]; - CvMat CC = cvMat(3, 3, CV_64F, cc); - CvMat CC_inv = cvMat(3, 3, CV_64F, cc_inv); + cv::Mat CC = cv::Mat(3, 3, CV_64F, cc); + cv::Mat CC_inv = cv::Mat(3, 3, CV_64F, cc_inv); for(int i = 0; i < 3; i++) for(int j = 1; j < 4; j++) cc[3 * i + j - 1] = cws[j][i] - cws[0][i]; - cvInvert(&CC, &CC_inv, CV_SVD); + cv::invert(CC, CC_inv, cv::DECOMP_SVD); double * ci = cc_inv; for(int i = 0; i < number_of_correspondences; i++) { double * pi = pws + 3 * i; @@ -433,10 +431,10 @@ void PnPsolver::compute_barycentric_coordinates(void) } } -void PnPsolver::fill_M(CvMat * M, +void PnPsolver::fill_M(cv::Mat * M, const int row, const double * as, const double u, const double v) { - double * M1 = M->data.db + row * 12; + double * M1 = M->ptr(row * 12); double * M2 = M1 + 12; for(int i = 0; i < 4; i++) { @@ -479,23 +477,22 @@ double PnPsolver::compute_pose(double R[3][3], double t[3]) choose_control_points(); compute_barycentric_coordinates(); - CvMat * M = cvCreateMat(2 * number_of_correspondences, 12, CV_64F); + cv::Mat M = cv::Mat(2 * number_of_correspondences, 12, CV_64F); for(int i = 0; i < number_of_correspondences; i++) - fill_M(M, 2 * i, alphas + 4 * i, us[2 * i], us[2 * i + 1]); + fill_M(&M, 2 * i, alphas + 4 * i, us[2 * i], us[2 * i + 1]); double mtm[12 * 12], d[12], ut[12 * 12]; - CvMat MtM = cvMat(12, 12, CV_64F, mtm); - CvMat D = cvMat(12, 1, CV_64F, d); - CvMat Ut = cvMat(12, 12, CV_64F, ut); + cv::Mat MtM = cv::Mat(12, 12, CV_64F, mtm); + cv::Mat D = cv::Mat(12, 1, CV_64F, d); + cv::Mat Ut = cv::Mat(12, 12, CV_64F, ut); - cvMulTransposed(M, &MtM, 1); - cvSVD(&MtM, &D, &Ut, 0, CV_SVD_MODIFY_A | CV_SVD_U_T); - cvReleaseMat(&M); + cv::mulTransposed(M, MtM, 1); + cv::SVD::compute(MtM, D, Ut, cv::Mat(), cv::SVD::MODIFY_A | cv::SVD::NO_UV); double l_6x10[6 * 10], rho[6]; - CvMat L_6x10 = cvMat(6, 10, CV_64F, l_6x10); - CvMat Rho = cvMat(6, 1, CV_64F, rho); + cv::Mat L_6x10 = cv::Mat(6, 10, CV_64F, l_6x10); + cv::Mat Rho = cv::Mat(6, 1, CV_64F, rho); compute_L_6x10(ut, l_6x10); compute_rho(rho); @@ -588,12 +585,12 @@ void PnPsolver::estimate_R_and_t(double R[3][3], double t[3]) } double abt[3 * 3], abt_d[3], abt_u[3 * 3], abt_v[3 * 3]; - CvMat ABt = cvMat(3, 3, CV_64F, abt); - CvMat ABt_D = cvMat(3, 1, CV_64F, abt_d); - CvMat ABt_U = cvMat(3, 3, CV_64F, abt_u); - CvMat ABt_V = cvMat(3, 3, CV_64F, abt_v); + cv::Mat ABt = cv::Mat(3, 3, CV_64F, abt); + cv::Mat ABt_D = cv::Mat(3, 1, CV_64F, abt_d); + cv::Mat ABt_U = cv::Mat(3, 3, CV_64F, abt_u); + cv::Mat ABt_V = cv::Mat(3, 3, CV_64F, abt_v); - cvSetZero(&ABt); + ABt.setTo(0); for(int i = 0; i < number_of_correspondences; i++) { double * pc = pcs + 3 * i; double * pw = pws + 3 * i; @@ -605,7 +602,7 @@ void PnPsolver::estimate_R_and_t(double R[3][3], double t[3]) } } - cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_V, CV_SVD_MODIFY_A); + cv::SVD::compute(ABt, ABt_D, ABt_U, ABt_V, cv::SVD::MODIFY_A); for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) @@ -664,21 +661,21 @@ double PnPsolver::compute_R_and_t(const double * ut, const double * betas, // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44] // betas_approx_1 = [B11 B12 B13 B14] -void PnPsolver::find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho, +void PnPsolver::find_betas_approx_1(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas) { double l_6x4[6 * 4], b4[4]; - CvMat L_6x4 = cvMat(6, 4, CV_64F, l_6x4); - CvMat B4 = cvMat(4, 1, CV_64F, b4); - - for(int i = 0; i < 6; i++) { - cvmSet(&L_6x4, i, 0, cvmGet(L_6x10, i, 0)); - cvmSet(&L_6x4, i, 1, cvmGet(L_6x10, i, 1)); - cvmSet(&L_6x4, i, 2, cvmGet(L_6x10, i, 3)); - cvmSet(&L_6x4, i, 3, cvmGet(L_6x10, i, 6)); + cv::Mat L_6x4 = cv::Mat(6, 4, CV_64F, l_6x4); + cv::Mat B4 = cv::Mat(4, 1, CV_64F, b4); + + for (int i = 0; i < 6; i++) { + L_6x4.at(i, 0) = L_6x10->at(i, 0); + L_6x4.at(i, 1) = L_6x10->at(i, 1); + L_6x4.at(i, 2) = L_6x10->at(i, 3); + L_6x4.at(i, 3) = L_6x10->at(i, 6); } - cvSolve(&L_6x4, Rho, &B4, CV_SVD); + cv::solve(L_6x4, *Rho, B4, cv::DECOMP_SVD); if (b4[0] < 0) { betas[0] = sqrt(-b4[0]); @@ -696,20 +693,20 @@ void PnPsolver::find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho, // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44] // betas_approx_2 = [B11 B12 B22 ] -void PnPsolver::find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho, +void PnPsolver::find_betas_approx_2(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas) { double l_6x3[6 * 3], b3[3]; - CvMat L_6x3 = cvMat(6, 3, CV_64F, l_6x3); - CvMat B3 = cvMat(3, 1, CV_64F, b3); + cv::Mat L_6x3 = cv::Mat(6, 3, CV_64F, l_6x3); + cv::Mat B3 = cv::Mat(3, 1, CV_64F, b3); for(int i = 0; i < 6; i++) { - cvmSet(&L_6x3, i, 0, cvmGet(L_6x10, i, 0)); - cvmSet(&L_6x3, i, 1, cvmGet(L_6x10, i, 1)); - cvmSet(&L_6x3, i, 2, cvmGet(L_6x10, i, 2)); + L_6x3.at(i, 0) = L_6x10->at(i, 0); + L_6x3.at(i, 1) = L_6x10->at(i, 1); + L_6x3.at(i, 2) = L_6x10->at(i, 2); } - cvSolve(&L_6x3, Rho, &B3, CV_SVD); + cv::solve(L_6x3, *Rho, B3, cv::DECOMP_SVD); if (b3[0] < 0) { betas[0] = sqrt(-b3[0]); @@ -728,22 +725,22 @@ void PnPsolver::find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho, // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44] // betas_approx_3 = [B11 B12 B22 B13 B23 ] -void PnPsolver::find_betas_approx_3(const CvMat * L_6x10, const CvMat * Rho, +void PnPsolver::find_betas_approx_3(const cv::Mat * L_6x10, const cv::Mat * Rho, double * betas) { double l_6x5[6 * 5], b5[5]; - CvMat L_6x5 = cvMat(6, 5, CV_64F, l_6x5); - CvMat B5 = cvMat(5, 1, CV_64F, b5); + cv::Mat L_6x5 = cv::Mat(6, 5, CV_64F, l_6x5); + cv::Mat B5 = cv::Mat(5, 1, CV_64F, b5); for(int i = 0; i < 6; i++) { - cvmSet(&L_6x5, i, 0, cvmGet(L_6x10, i, 0)); - cvmSet(&L_6x5, i, 1, cvmGet(L_6x10, i, 1)); - cvmSet(&L_6x5, i, 2, cvmGet(L_6x10, i, 2)); - cvmSet(&L_6x5, i, 3, cvmGet(L_6x10, i, 3)); - cvmSet(&L_6x5, i, 4, cvmGet(L_6x10, i, 4)); + L_6x5.at(i, 0) = L_6x10->at(i, 0); + L_6x5.at(i, 1) = L_6x10->at(i, 1); + L_6x5.at(i, 2) = L_6x10->at(i, 2); + L_6x5.at(i, 3) = L_6x10->at(i, 3); + L_6x5.at(i, 4) = L_6x10->at(i, 4); } - cvSolve(&L_6x5, Rho, &B5, CV_SVD); + cv::solve(L_6x5, *Rho, B5, cv::SVD::NO_UV); if (b5[0] < 0) { betas[0] = sqrt(-b5[0]); @@ -810,18 +807,18 @@ void PnPsolver::compute_rho(double * rho) } void PnPsolver::compute_A_and_b_gauss_newton(const double * l_6x10, const double * rho, - double betas[4], CvMat * A, CvMat * b) + double betas[4], cv::Mat * A, cv::Mat * b) { for(int i = 0; i < 6; i++) { const double * rowL = l_6x10 + i * 10; - double * rowA = A->data.db + i * 4; + double * rowA = A->ptr(i * 4); rowA[0] = 2 * rowL[0] * betas[0] + rowL[1] * betas[1] + rowL[3] * betas[2] + rowL[6] * betas[3]; rowA[1] = rowL[1] * betas[0] + 2 * rowL[2] * betas[1] + rowL[4] * betas[2] + rowL[7] * betas[3]; rowA[2] = rowL[3] * betas[0] + rowL[4] * betas[1] + 2 * rowL[5] * betas[2] + rowL[8] * betas[3]; rowA[3] = rowL[6] * betas[0] + rowL[7] * betas[1] + rowL[8] * betas[2] + 2 * rowL[9] * betas[3]; - cvmSet(b, i, 0, rho[i] - + b->at(i, 0) = rho[i] - ( rowL[0] * betas[0] * betas[0] + rowL[1] * betas[0] * betas[1] + @@ -833,22 +830,22 @@ void PnPsolver::compute_A_and_b_gauss_newton(const double * l_6x10, const double rowL[7] * betas[1] * betas[3] + rowL[8] * betas[2] * betas[3] + rowL[9] * betas[3] * betas[3] - )); + ); } } -void PnPsolver::gauss_newton(const CvMat * L_6x10, const CvMat * Rho, +void PnPsolver::gauss_newton(const cv::Mat * L_6x10, const cv::Mat * Rho, double betas[4]) { const int iterations_number = 5; double a[6*4], b[6], x[4]; - CvMat A = cvMat(6, 4, CV_64F, a); - CvMat B = cvMat(6, 1, CV_64F, b); - CvMat X = cvMat(4, 1, CV_64F, x); + cv::Mat A = cv::Mat(6, 4, CV_64F, a); + cv::Mat B = cv::Mat(6, 1, CV_64F, b); + cv::Mat X = cv::Mat(4, 1, CV_64F, x); for(int k = 0; k < iterations_number; k++) { - compute_A_and_b_gauss_newton(L_6x10->data.db, Rho->data.db, + compute_A_and_b_gauss_newton(L_6x10->ptr(0), Rho->ptr(), betas, &A, &B); qr_solve(&A, &B, &X); @@ -857,7 +854,7 @@ void PnPsolver::gauss_newton(const CvMat * L_6x10, const CvMat * Rho, } } -void PnPsolver::qr_solve(CvMat * A, CvMat * b, CvMat * X) +void PnPsolver::qr_solve(cv::Mat * A, cv::Mat * b, cv::Mat * X) { static int max_nr = 0; static double * A1, * A2; @@ -875,7 +872,7 @@ void PnPsolver::qr_solve(CvMat * A, CvMat * b, CvMat * X) A2 = new double[nr]; } - double * pA = A->data.db, * ppAkk = pA; + double * pA = A->ptr(0), *ppAkk = pA; for(int k = 0; k < nc; k++) { double * ppAik = ppAkk, eta = fabs(*ppAik); for(int i = k + 1; i < nr; i++) { @@ -919,7 +916,7 @@ void PnPsolver::qr_solve(CvMat * A, CvMat * b, CvMat * X) } // b <- Qt b - double * ppAjj = pA, * pb = b->data.db; + double * ppAjj = pA, *pb = b->ptr(0); for(int j = 0; j < nc; j++) { double * ppAij = ppAjj, tau = 0; for(int i = j; i < nr; i++) { @@ -936,7 +933,7 @@ void PnPsolver::qr_solve(CvMat * A, CvMat * b, CvMat * X) } // X = R-1 b - double * pX = X->data.db; + double * pX = X->ptr(0); pX[nc - 1] = pb[nc - 1] / A2[nc - 1]; for(int i = nc - 2; i >= 0; i--) { double * ppAij = pA + i * nc + (i + 1), sum = 0; diff --git a/src/Sim3Solver.cc b/src/Sim3Solver.cc index 4ad44ff0de..0ff7e733e8 100644 --- a/src/Sim3Solver.cc +++ b/src/Sim3Solver.cc @@ -214,7 +214,7 @@ cv::Mat Sim3Solver::find(vector &vbInliers12, int &nInliers) void Sim3Solver::ComputeCentroid(cv::Mat &P, cv::Mat &Pr, cv::Mat &C) { - cv::reduce(P,C,1,CV_REDUCE_SUM); + cv::reduce(P,C,1,cv::REDUCE_SUM); C = C/P.cols; for(int i=0; i1e-5) || imDepth.type()!=CV_32F) @@ -242,16 +242,16 @@ cv::Mat Tracking::GrabImageMonocular(const cv::Mat &im, const double ×tamp) if(mImGray.channels()==3) { if(mbRGB) - cvtColor(mImGray,mImGray,CV_RGB2GRAY); + cvtColor(mImGray,mImGray,cv::COLOR_RGB2GRAY); else - cvtColor(mImGray,mImGray,CV_BGR2GRAY); + cvtColor(mImGray,mImGray,cv::COLOR_BGR2GRAY); } else if(mImGray.channels()==4) { if(mbRGB) - cvtColor(mImGray,mImGray,CV_RGBA2GRAY); + cvtColor(mImGray,mImGray,cv::COLOR_RGBA2GRAY); else - cvtColor(mImGray,mImGray,CV_BGRA2GRAY); + cvtColor(mImGray,mImGray,cv::COLOR_BGRA2GRAY); } if(mState==NOT_INITIALIZED || mState==NO_IMAGES_YET) From 06c5198d3a08ddbfe8a1c2fbbf4763689ea0778a Mon Sep 17 00:00:00 2001 From: YOUNG Date: Tue, 21 Mar 2023 23:30:47 +0800 Subject: [PATCH 2/3] :memo: Update gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 611b5adb2c..c58145d030 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ build/ *~ lib/ +.vscode +.clang-format From a57a0e03a37ff52e58c69eb5b4748697d7feec7c Mon Sep 17 00:00:00 2001 From: YOUNG Date: Tue, 21 Mar 2023 23:31:59 +0800 Subject: [PATCH 3/3] :memo: Update gitignore --- .clang-format | 213 ---------------------------------- .vscode/c_cpp_properties.json | 18 --- 2 files changed, 231 deletions(-) delete mode 100644 .clang-format delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 6fc239ea15..0000000000 --- a/.clang-format +++ /dev/null @@ -1,213 +0,0 @@ -# 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto -Language: Cpp -# BasedOnStyle: LLVM - -# 访问说明符(public、private等)的偏移 -AccessModifierOffset: -4 - -# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行) -AlignAfterOpenBracket: Align - -# 连续赋值时,对齐所有等号 -AlignConsecutiveAssignments: true - -# 连续声明时,对齐所有声明的变量名 -AlignConsecutiveDeclarations: false - -# 右对齐逃脱换行(使用反斜杠换行)的反斜杠 -AlignEscapedNewlines: Right - -# 水平对齐二元和三元表达式的操作数 -AlignOperands: true - -# 对齐连续的尾随的注释 -AlignTrailingComments: true - -# 不允许函数声明的所有参数在放在下一行 -AllowAllParametersOfDeclarationOnNextLine: false - -# 不允许短的块放在同一行 -AllowShortBlocksOnASingleLine: true - -# 允许短的case标签放在同一行 -AllowShortCaseLabelsOnASingleLine: true - -# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All -AllowShortFunctionsOnASingleLine: None - -# 允许短的if语句保持在同一行 -AllowShortIfStatementsOnASingleLine: true - -# 允许短的循环保持在同一行 -AllowShortLoopsOnASingleLine: true - -# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数), -# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义) -AlwaysBreakAfterReturnType: None - -# 总是在多行string字面量前换行 -AlwaysBreakBeforeMultilineStrings: false - -# 总是在template声明后换行 -AlwaysBreakTemplateDeclarations: true - -# false表示函数实参要么都在同一行,要么都各自一行 -BinPackArguments: true - -# false表示所有形参要么都在同一行,要么都各自一行 -BinPackParameters: true - -# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效 -BraceWrapping: - # class定义后面 - AfterClass: false - # 控制语句后面 - AfterControlStatement: false - # enum定义后面 - AfterEnum: false - # 函数定义后面 - AfterFunction: false - # 命名空间定义后面 - AfterNamespace: false - # struct定义后面 - AfterStruct: false - # union定义后面 - AfterUnion: false - # extern之后 - AfterExternBlock: false - # catch之前 - BeforeCatch: false - # else之前 - BeforeElse: false - # 缩进大括号 - IndentBraces: false - # 分离空函数 - SplitEmptyFunction: false - # 分离空语句 - SplitEmptyRecord: false - # 分离空命名空间 - SplitEmptyNamespace: false - -# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行) -BreakBeforeBinaryOperators: NonAssignment - -# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似), -# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似), -# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom -# 注:这里认为语句块也属于函数 -BreakBeforeBraces: Custom - -# 在三元运算符前换行 -BreakBeforeTernaryOperators: false - -# 在构造函数的初始化列表的冒号后换行 -BreakConstructorInitializers: AfterColon - -#BreakInheritanceList: AfterColon - -BreakStringLiterals: false - -# 每行字符的限制,0表示没有限制 -ColumnLimit: 0 - -CompactNamespaces: true - -# 构造函数的初始化列表要么都在同一行,要么都各自一行 -ConstructorInitializerAllOnOneLineOrOnePerLine: false - -# 构造函数的初始化列表的缩进宽度 -ConstructorInitializerIndentWidth: 4 - -# 延续的行的缩进宽度 -ContinuationIndentWidth: 4 - -# 去除C++11的列表初始化的大括号{后和}前的空格 -Cpp11BracedListStyle: true - -# 继承最常用的指针和引用的对齐方式 -DerivePointerAlignment: false - -# 固定命名空间注释 -FixNamespaceComments: true - -# 缩进case标签 -IndentCaseLabels: false - -IndentPPDirectives: None - -# 缩进宽度 -IndentWidth: 4 - -# 函数返回类型换行时,缩进函数声明或函数定义的函数名 -IndentWrappedFunctionNames: false - -# 保留在块开始处的空行 -KeepEmptyLinesAtTheStartOfBlocks: false - -# 连续空行的最大数量 -MaxEmptyLinesToKeep: 1 - -# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All -NamespaceIndentation: None - -# 指针和引用的对齐: Left, Right, Middle -PointerAlignment: Right - -# 允许重新排版注释 -ReflowComments: true - -# 允许排序#include -SortIncludes: false - -# 允许排序 using 声明 -SortUsingDeclarations: false - -# 在C风格类型转换后添加空格 -SpaceAfterCStyleCast: false - -# 在Template 关键字后面添加空格 -SpaceAfterTemplateKeyword: true - -# 在赋值运算符之前添加空格 -SpaceBeforeAssignmentOperators: true - -# SpaceBeforeCpp11BracedList: true - -# SpaceBeforeCtorInitializerColon: true - -# SpaceBeforeInheritanceColon: true - -# 开圆括号之前添加一个空格: Never, ControlStatements, Always -SpaceBeforeParens: ControlStatements - -# SpaceBeforeRangeBasedForLoopColon: true - -# 在空的圆括号中添加空格 -SpaceInEmptyParentheses: false - -# 在尾随的评论前添加的空格数(只适用于//) -SpacesBeforeTrailingComments: 1 - -# 在尖括号的<后和>前添加空格 -SpacesInAngles: false - -# 在C风格类型转换的括号中添加空格 -SpacesInCStyleCastParentheses: false - -# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格 -SpacesInContainerLiterals: true - -# 在圆括号的(后和)前添加空格 -SpacesInParentheses: false - -# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响 -SpacesInSquareBrackets: false - -# 标准: Cpp03, Cpp11, Auto -Standard: Cpp11 - -# tab宽度 -TabWidth: 4 - -# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always -UseTab: Never \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 0a6a0c69c8..0000000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**", - "/usr/include/opencv4", - "/usr/include/eigen3" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "c17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 -} \ No newline at end of file