There are 4 steps to compiling OpenCV_SFM on windows.
- Building gFlags and Glog.
- Building Ceres-Solver
- Building VTK
- Building OpenCV_SFM.
Lets follow them one by one.
- Create a folder named ‘ceres’ at the location where you want to build ceres-solver. I created it in my Documents folder.
- Inside this folder download and unzip the source code for gflags and glog such that you get the document tree like ‘ceres/gflag’ and ‘ceres/glog’.
- Lets build gflag first. Open Cmake and select the source path as the path to the gflag folder inside the ceres folder.
- Create a folder inside gflag folder named build. Use the path to this build folder for the build path.
- Click on configure and select the installed MSVC compiler on your system. Gflags should be configured without any error. Click on generate. A solution would be generated inside the gflags/build folder.
- Open the solution file in Visual Studio and Build it in Debug and Release Configuration. We have successfully built gflags. Now lets move on to Glog.
- Inside the glog folder, create a folder named build.
- Open Cmake. Give the source path as the path to your glog folder and the build path as the path to the build folder inside glog.
- Click on configure and select the installed MSVC compiler on your system. Glog might ask for gflags after you configure. If it does then give the path to the build folder of gflag for the GFLAG_DIR variable and click on configure again. It should find the include and library path for gflag. If it doesn’t do it manually by giving the path to GFLAG_INCLUDE_DIR and GFLAG_LIBRARY_DIR. Please note we are building the Release Library for SFM. So wherever required give the path to the Release folder for the libraries. If you want to build Debug Library, simply repeat the steps again and this time give path to Debug Libraries instead of Release. This would help you build Debug Libraries as well.
- Once glog configure picks up everything it needs, click on generate. A solution file will be generated in the build folder just like gflag. Now, just like gflag, open this solution file in Visual Studio and build it in Release and Debug mode. We have successfully built the glog library.
- Download and unzip the latest stable release of Eigen inside the ceres folder. There is no need to build eigen. So now you have 3 folders inside the ceres folder. Ceres/gflag, ceres/glog, ceres/eigen.
- We are ready to build the ceres-solver. Download and unzip the ceres-solver source inside the ceres folder. Make a new folder named ceres-bin for the build.
- Open cmake and give the path to the source folder as the path to the ceres folder. Give the path to the build folder as the path to the ceres-bin folder.
- Click on configure. Select the MSVC compiler. There would be errors. First one is usually the eigen error. Give the path to the ceres/eigen folder for the EIGEN_DIR variable and click on configure again.
- This time, there will be errors for Gflags and Glog. Give the required GFLAG_INCLUDE_DIR and GFLAG_LIBRARY and GLOG_INCLUDE_DIR and GLOG_LIBRARY path and click on configure.
- This should resolve all the issues. CERES will be configured to build without any SPARSE Library. Click on Generate. A solution for MSVC will be generated in the ‘ceres-bin’ folder.
- Open the ceres.sln file in your Visual Studio, in Administrator Mode. In the solution view tab, find the project named ceres. Right Click on the project and select Properties. On the Properties window, on the left hand side, expand the C/C++ option. Inside that list, select Compiler Optimizations and set it to “Disabled.”
- Now build the project in Release Mode. Ceres will be built. Once the solution will be built, find the INSTALL project. Right click on it. Project Only > Build Only this project.
- Ceres Solver is now installed on your system.
- Follow similar steps for installing VTK.
- Once all of this is done, try generating the SFM Module from OpenCV_NonFree modules. It should build sucessfully. If you face any issues, try dropping an email. I will try to help as and when possible.
Disclaimer: This Post assumes, you know that SFM in OpenCV is a part of its Non Free Module and that its use for any commercial purpose is defined by its license for which you might have to pay. I hold no legal responsibility for any lapses arising out of non licensing of module.