Giter Site home page Giter Site logo

build_environment_opencv_cxx's Introduction

Build_Environment_OpenCV_CXX

This repo provides tutorials on building environment of OpenCV (C++) on Windows and Mac (Both Intel and Apple silicon)

Windows

Reference

  1. 【C/C++】VS Code配置Opencv用于机器视觉数字图像处理技术

  2. Using GCC with MinGW

  3. MinGW C++ Download and Installation

System Configurations

OS: Windows11 22H2

Text Editor: Visual Studio Code 1.75.0

Build Tool: Cmake 3.26.0-rc 1

Compiler: gcc/g++ (8.1.0) by MinGW

I. Preparation

We need to make sure the following parts are installed: 1. Visual Studio Code (aka VS code) 2. MinGW 3. OpenCV 4. CMake

I.1 Install VS code

VS code is a handy and versitile text editor. It can be downloaded from the official website at https://code.visualstudio.com/download Open VS code, go to "Extensions" and search "c/c++". Install the C/C++ extension provided by Microsoft. After the installation, researt VS code.

I.2 Install OpenCV

OpenCV can be downloaded at https://opencv.org/releases. Follow the onscreen instructions and finalizae the installation. Make sure you note the installation path. In my case, it is installed at D:\opencv. This location is denoted as $CV$ in the rest of this tutorial for Windows.

Then you want to create a new folder under $CV$/build/x64 called MinGW (it is empty as of now).

I.3 Install CMake

CMake can be downloaded at https://cmake.org/download. Follow the onscreen instructions and finalizae the installation. Make sure you note the installation path. In my case, it is installed at D:\CMake, denoted as $CMAKE$.

I.4 Install MinGW

There are a bunch of comprehensive and detailed installation tutorials online. They are available at Using GCC with MinGW, MinGW C++ Download and Installation etc. The release page is here, in case you would like to explore by yourself or look for a previous version. To test if MinGW is properly installed, you can type

gcc -v

in Command Prompt. If the output is similar as shown below, the installation is successful. Also, Make sure you note the installation path. In my case, it is installed at C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64, denoted as $MINGW$.

II. Update Environment Variables

Go to Start (you can press Win button). Enter view advanced system settings. Click Environment Variables and double click Path under System variables section. Click New and mannually add the following two paths (seperately): $CMAKE$\bin and $MINGW$\bin. Please replace $CMAKE$ and $MINGW$ to your own paths. If you followed Using GCC with MinGW, you should have already added the MinGW path in the last step. Then you will only need to add CMake to the path. Only if MinGW path is added, you can test if MinGW is properly installed.

III. Use CMake

Open CMake(cmake-gui) by searching cmake in Start. Specify Where is the source code and Where to build the binaries. For the first domain, it is $CV$/sources. For the second domain, it is $CV$/build/x64/MinGW (the empty folder created in step I.2).

Click Configure and we choose the following options: MinGW Makefiles and Specify native compilers. Click Next.

We need to provide the paths of native C and C++ compilers. They are $MINGW$/bin/gcc.exe and $MINGW$/bin/g++.exe.

After the configuration is done, make sure the value of WITH_OPENGL is checked, and WITH_IPP, WITH_OBSSENSOR, WITH_MSMF and ENABLE_PRECOMPILED_HEADERS are unchecked. You can quickly find these items by typing their names in the Search domain. Then click Generate.

When the generation is done, you will see "Generating done" in the message. Go to the folder $CV$/build/x64/MinGW and right click to open a Terminal at current path. You can also realize this by opening a Command Prompt and navigating to $CV$/build/x64/MinGW by

cd $CV$/build/x64/MinGW

Double check that you are at $CV$/build/x64/MinGW. In the Terminal (or Command Prompt), type the command

mingw32-make.exe

This will take a while. Please be patient.

When this make process is done, you should see "[100%]" with no error message. If there is an error, try to search online and usually you are not the first one who met the issue. You should also go through all above steps and make sure all of them are very carefully followed. A typo will ruin the whole process. Please be careful.

In the same Terminal, type the command

mingw32-make.exe install

When it is done, CMake part is finished! Congratulations!

IV. Update Environment Variables (AGAIN)

Add $CV$/build/x64/MinGW/bin to Path following similar steps in II. Then you should be able to see all three new paths as shown below.

V. The Final Part

Create a working folder at a desired location. Mine is called "OpenCVTest" at C:\Users\Yiming Bian\Desktop\OpenCVTest (desktop). Download the folder ".vscode" in this repo and move it to the working directory, in my case it is C:\Users\Yiming Bian\Desktop\OpenCVTest. This folder contains three json files: c_cpp_properties.json, launch.json, and tasks.json. You need to modify them as will be mentioned.

Open VS code and open your working directory by File -> Open Folder....

Modify c_cpp_properties.json by replacing with your own $CV$/build/include and $MINGW$/bin/gcc.exe paths, as shown below.

Modify launch.json by replacing with your $MINGW$/bin/gdb.exe path.

For tasks.json, you may need to make a lot of changes if our OpenCV versions are different. First, you need to update your paths of $CV$/build/include and $CV$/build/x64/MinGW/lib. Go to $CV$/build/x64/MinGW/lib and you will see a bunch of files ending with .dll.a. Update this json file with .dll.a file names (ONLY).

You are ready to go!

Test

A test image (Tumbo.jpg) and a test code (main.cpp) are provided in this repo. If you are a beginner, feel free to download these files and move them to the working directory to test if the environment is properly configured. Put them at the same level of .vscode, not inside. In VS code, press F5 or Run -> Start Debugging, eqivalently, to run the code. If you see an image of Tumbo (my dog) pops up, congratulations!

Mac

Reference

  1. How to develop an OpenCV C++ algorithm in Xcode

  2. How to Download Xcode and Install it on Your Mac – and Update it for iOS Development

  3. COMPLETE Xcode Install Tutorial for Beginners | How to Install Xcode

  4. OpenCV 3.0.0 with Xcode

  5. How to create a txt file in Xcode and use it for input file

System Configurations

OS: MacOS Ventura 13.2

Chip: Apple M1

IDE: XCode 14.2 (14C18)

Compiler: Apple clang version 14.0.0 (clang-1400.0.29.202)

I. Preparation

We need to make sure the following parts are installed: 1. Xcode 2. OpenCV

I.1 Install Xcode

Xcode can be downloaded at https://apps.apple.com/us/app/xcode/id497799835?mt=12. Or you can go to App Store and download it from there. If you still find it difficult, Google and YouTube will be your friend. Tutorials such as How to Download Xcode and Install it on Your Mac – and Update it for iOS Development and COMPLETE Xcode Install Tutorial for Beginners | How to Install Xcode are easily accessible.

I.2 Install OpenCV

We download OpenCV using a package manager called HomeBrew. You can go to https://brew.sh and copy the command given under "Install HomeBrew". Open Terminal, paste the command and hit enter.

With HomeBrew installed, in the same Terminal window, type the command

brew install opencv

At the time of writing, the latest OpenCV version is 4.7.0. As pointed out by user "Ortomala Lokni" in this question, Cellar folder will be placed under /usr/local/ for Intel Mac while it is placed under /opt/homebrew/ for Apple silicon Mac. Cellar is the mother folder of opencv and the path of opencv is critical. Since I am using an M1 Mac, I will be using /opt/homebrew/Cellar as the mother folder of opencv. For Intel Mac users, just substitute the corresponding path with /usr/local/.

If you go to /opt/homebrew/Cellar/opencv/4.7.0_1/ (version could vary), you should see every component of OpenCV there. Navigating can be done by pressing Shift + Command + g in Finder and specifying the path.

II. Use Xcode

II.1 Create A Project

Open Xcode to create a new project, select Command Line Tool under macOS platform tab.

Specify the project name (in my case, it is "OpenCVTest") and language to C++.

II.2 Link OpenCV

First we need to make sure pkg-config is installed. Typing the following command in Terminal.

brew install pkg-config

It will either start the installation process or show you an message saying that it is installed already.

Then we set the configuration path by typing the command in the same Terminal window.

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

Now pkg-config is ready to tell us about the configuration settings of OpenCV so that we can link it to the OpenCVTest project.

To list OpenCV libraries, we type the command below in Terminal.

pkg-config --libs-only-l opencv4

We mark the figure below as FIG_A.

Open a new Terminal window (not necessary, just for a clear observation). We can query OpenCV modules by command

pkg-config --libs-only-L opencv4

In the same window, we query the location of OpenCV by command

pkg-config --cflags opencv4

You should observe similar outputs as shown below. This figure is marked as FIG_B.

With all three outputs above, we are finally ready to link OpenCV to our project in Xcode!

In Xcode, we first select the project instance, which consists of a blue App Store icon and project name, locating in the project navigator on the left of the code editor. Then select Build Settings and search for Other linker flags. Double click Other Linker Flags line and click +. Copy the output in FIG_A and paste in this domain as shown below.

Then we search for library search paths and do the same process to Library Search Paths line. Paste the first output (excluding "-L") in FIG_B as shown below.

Lastly, we search for header search path and paste the second output (excluding "-I") in FIG_B in Header Search Paths line.

Conguratulations! You are 99% done!

Test

A test image (Tumbo.jpg) and a test code (main.cpp) are provided in this repo. If you are a beginner, feel free to download and use these files for the testing purpose.

As there is already an initial main.cpp file in the project, I recommend you to replace its content with the given test code directly. To add the test image to our project, you should right click the blank area in the project navigator then select Add Files to "OpenCVTest"... (the project name varies). A common mistake is directly copying and the image to the project folder in Finder. It will not appear in the project navigator in Xcode. Please avoid this mistake.

With the code and image properly placed in the project, you should see a similar result as shown below.

The test code in this figure is slightly different from what is provided in this repo because I would like to share two ways of accessing a file (in this case, an image). As suggested in the code comments, two ways of specifying the image path are using absolute and relative paths.

Using an absolute path requires the coder to always provide the full path to the image file. It is very straightforward, however troublesome. Using a relative path is simpler and gives the coder a clear sense when manging the files. But an additional step is needed.

In this case, the test image Tumbo.jpg is added to the same location where main.cpp resides. We first select the project, then go to Build Phases. We click + to add a Copy Files instance. We sepecify the Destination as Products Directory and add Tumbo.jpg to the item list by clicking the + below. The final 1% is finished, and we are officially good to go!

Go to main.cpp and click the Start the active scheme (the play button) on top of the project navigator. You should see Tumbo shortly!

build_environment_opencv_cxx's People

Contributors

yimingbian avatar idontthinkso12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.