[Solved] error: ‘CV_GRAY2BGR’ was not declared in this scope

This is the problem of running Gao Xiang’s slambook2 code, there are two methods to solve it.

error: ‘CV_GRAY2BGR’ was not declared in this scope

/home/diyu/slambook2/ch8/optical_flow.cpp: In function ‘int main(int, char**)’:
/home/diyu/slambook2/ch8/optical_flow.cpp:145:37: error: ‘CV_GRAY2BGR’ was not declared in this scope
145 | cv::cvtColor(img2, img2_single, CV_GRAY2BGR);
| ^~~~~~~~~~~
/home/diyu/slambook2/ch8/direct_method.cpp: In function ‘void DirectPoseEstimationSingleLayer(const cv::Mat&, const cv::Mat&, const VecVector2d&, std::vector<double, std::allocator >, Sophus::SE3d&)’:
/home/diyu/slambook2/ch8/direct_method.cpp:206:35: error: ‘CV_GRAY2BGR’ was not declared in this scope
206 | cv::cvtColor(img2, img2_show, CV_GRAY2BGR);
| ^~~~~~~~~~~
make[2]: *** [CMakeFiles/optical_flow.dir/build.make:63:CMakeFiles/optical_flow.dir/optical_flow.cpp.o] ERROR 1
make[1]: *** [CMakeFiles/Makefile2:105:CMakeFiles/optical_flow.dir/all] Error 2
make[1]: *** Waiting for unfinished task…
make[2]: *** [CMakeFiles /direct_method.dir/build.make:63: CMakeFiles/direct_method.dir/direct_method.cpp.o] error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/direct_method.dir/all] error 2
make: *** [Makefile:84:all] Error 2

This experiment is because the code uses opencv2, and the cmkelists.txt file is opencv4

error: ‘CV_GRAY2BGR’ was not declared in this scope
which is the usage of opencv2, now opencv3 and 4 are COLOR_GARY2BGR

So there are two Method to Solve this error

  1. Change the usage of opencv2 in the code to the usage of opencv3 or 4. Change the CV_GRAY2BGR in the code to COLOR_GRAY2BGR, not sure if you just need to modify this.
  2. Change the opencv4 in the cmakelists file to opencv2.

Here choose the second method, modify it to opencv2

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *