Build openCV based on Codeblocks 搬运自: https://medium.com/@sourabhjigjinni/install-opencv-4-0-0-for-c-windows-7-10-code-blocks-tdm-gcc-64-dff65addf162
[toc]
This is a step-by-step installation of OpenCV 4.1.0 on Windows. I was inspired to make this installation guide to help people avoid the stress I went through to install this on windows. I have obviously installed this myself with the help of other people’s slightly older blogs. Thanks to Zahid Hasan for the guide on version 3.2.0 which most of this is based on.
Download Code::Blocks here or google codeblocks > downloads > Download the binary Release.
Click on the sourceforge.net link for the codeblocks-17.12-setup.exe option as we want to use TDM (64 bit) compiler.
Download TDM-GCC here. Make sure to select the 64 bit version.
Install it in C:\ drive. it will look like C:\TDM-GCC-64. The bin folder should be registered automatically in system path during the installation process
Download OpenCV 4.1.0 and click sources to get the zip/excute file.
Create folders:
- C:\opencv\source\
- C:\opencv\build\
Download CMake and install.
- Open cmake, set source path to C:\opencv\source\ and binary path to C:\opencv\build.
- Click configure
- Choose CodeBlock — MinGW Makefiles (Should be set by default)
After configuring you will see options in red. We need to disable some of these to build the system:
- disable WITH_MFMS (media foundation needs special win sdk, only available for VS)
- ENABLE_PRECOMPILED_HEADERS=OFF
- WITH_IPP=OFF WITH_TBB=OFF (again libs available are for VS only)
MAKE SURE
- WITH_MFMS=OFF
- WITH_IPP=OFF,
- WITH_TBB=OFF
- ENABLE_PRECOMPILED_HEADERS=OFF
And
- WITH_OPENCL=ON
- WITH_OPENCL_D3D11_NV=OFF
- WITH_DIRECTX=ON
Lastly
- BUILD_PROTOBUF = OFF
- PROTOBUF_UPDATE_FILES = OFF
- WITH_PROTOBUF = OFF
Next click Generate.
- You will find a codeblocks project file (opencv.cbp) in C:\opencv\build folder. Just double click it and codeblocks should load it. If it doesn’t just find the codeblocks app and open it.
- Go to ‘settings‘, choose ‘compiler’ and click ‘Toolchain executable‘. In the ‘compiler’s installation directory‘ field choose the “bin” folder of MinGW C:\TDM-GCC-64\bin. set the following:
-
c compile: gcc.exe
-
c++ compiler: g++.exe
-
Linker for dynamic libs: ar.exe
- DONT BUILD TARGET IN A HURRY, this is where I went wrong, while following Zahid Hasan’s article. Select build > select target > install
- After this step you can build.
Tip: the percentage of the build done is shown here. This will take a while depending on your hardware. It took me 1.5 hours.
\5. Add *C:\opencv\build\install\x64\mingw\bin* to the path.
Tip: you can check path variables with echo %path:;=&echo.% in command prompt.
- Create a test project in Code::Blocks. Select console application.
- Go to settings -> compiler. Select ‘search directories’ and in the ‘compiler’ tab chose the followings:
- C:\opencv\build\install\include
- C:\opencv\build\install\include\opencv2
- Select ‘Linker’ tab and add C:\opencv\build\install\x64\mingw\lib
-
Go to ‘Linker Settings’ and add all the libraries from C:\OpenCV\my_build\install\x64\mingw\lib folder
If it looks like this, all is good. Just one last step of using c++11 standard.
- Set the compiler to c++11 standard (Settings -> Compiler)
- Edit your main.cpp and add this:
#include <iostream> #include <opencv2\highgui\highgui.hpp> #include <opencv2\opencv.hpp> using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { cout << "Error initializing video camera!" << endl; return -1; } char* windowName = "Webcam Feed"; namedWindow(windowName, WINDOW_AUTOSIZE); while (1) { Mat frame; bool bSuccess = cap.read(frame); if (!bSuccess) { cout << "Error reading frame from camera feed" << endl; break; } imshow(windowName, frame); switch (waitKey(10)) { case 27: return 0; } } return 0; }
- Build and run
If u get an error like this restart your computer.
If your program compiles your webcam should start. If u don’t have a webcam try to open an image with openCV.