Open CMake, and point it to the OpenCV sources directory.
Create a new folder under OpenCV called my_build.
Point CMake's "Where to build the binaries" field to the my_build folder.
Hit Configure. CMake will ask you about the compiler. If you want the CodeBlocks build, you should specify that it's MinGW for CodeBlocks, because OpenCV 's cmake file will output a CodeBlocks file that will help with compiling. You make need to specify the native compiler to match the compiler used for your project.
Once the Configure process has finished, everything should be in red. This does not indicate an error. CMake needs you to verify the configuration data.
1 - Find the "With" drop-down, and uncheck WITH_IPP (and IPP_A, if it is checked). These only work under Visual Studios.
2 - Under "Build", check BUILD_SHARED_LIBS for dlls, uncheck for .a
3 - Check under the "CMake" tab to make sure CMake hasn't changed any of your compiler options, as it often does. It changed 2 of mine. Check: CMAKE_AR, CMAKE_LINKER, CMAKE_MAKE_PROGRAM, NM, OBJCOPY, OBJDUMP, RANLIB, RC_COMPILER, STRIP
Click "Configure" again.
Any time you click configure, you should check all of the above options because CMake sometimes loses track of your settings. Once you have nothing else in red, and every option is correct, click Generate.
Navigate to the my_build directory under the OpenCV folder. You should see an OpenCV.cbp. Open it in CodeBlocks.
Configure CodeBlocks to use the same compiler that was specified in CMake.
Compile.
You will most likely encounter 4 errors in 'opencv/sources/modules/videoio/src/cap_dshow.cpp' pertaining to ISampleGrabberCB, ISampleGrabber, IEnumPIDMap, and IMPEG2PIDMap. Each of these interfaces needs a virtual destructor added to them (for example, in IEnumPIDMap, add virtual ~IEnumPIDMap(){} ). Adding these fixes the issues.
Hit compile. Sit back and wait.
If you are using TDM as the compiler, you'll most likely encounter ANOTHER issue with libstdc++ claiming a whole mess of things are redefined/have multiple definitions. If this happens, go to your my_build directory in Cygwin, and run the following command:
grep -rl 'lstdc' ./ | xargs sed -i 's/lstdc/static-libstdc/g'
The command replaces a bunch of dynamic linking instances with statically linked instances of libstdc.
Hit compile to start compiling from where you left off.
Testing
Once the compile is done, in the my_build libraries, you will have a /lib/ with the .a's, and a /bin/ with the dlls
Create a new console project in CodeBlocks, and use the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a ...
(more)
in cmake you have to set variables OPENCV_EXTRA_MODULES_PATH to to your path for example opencv_contrib/modules
I haven't got any problem with IPP. But cmake want to load IPP. So if you haven't got an internet link you have to download before and give path after
Done that extra modules part with no problems.
Effectively I don't have internet-could disabling IPP be the cause of my issues ?
you can download IPP on another machine using this link. After you have to set variable for IPP path (I don't know variable name)
Have you successfully built an opencv library with contrib modules using cmake/mingw without unticking WITH_IPP ?
No I don't use mingw. I use VS 2013.
Okay, so could you explain the step by step method to get opencv3 with contrib modules to work in VS? First build with cmake then what ? Thx !
You don't build a project cmake you create your own project to build opencv (opencv.sln) You can use CMake GUI 3.3.0 (with 3.29 is good too). Open cmake GUI select where folder where you clone opencv. select where you want to build (normally same folder) check box advanced and grouped. Click in configure and generate.Normally nothing must be red (or may be some warnings). It's OK when opencv.sln is created.
You can select some options BUILD_EXAMPLES give path to opencv_contrib (in that case you have to disable build_opencv_adas) WITh_opencl with_opengl
Now you can create your own project using cmake
Did all that, but when I build project in VS2015 it's giving me plenty of opencv_core alerts and fails to build... Did I miss anything ? I used cmake to configure then generate the opencv project file, specifying where to contrib folder was and other options. What should I look for now ?
I have just try with VS 2015 and cmake 3.3.0 with opencv 3.0 I have got only two errors one in project opencv_perf_core and (sample) disparity_filtering.
What are your errors?
@mubb, which gcc version are you using ? that thing might be too old, if it has no atomic_fetch instructions, it won't even build the core opencv libs properly (save the contrib ones)