Location>code7788 >text

Clion+OpenCV(C++ Edition) Development Environment Configuration Tutorial WinMac

Popularity:556 ℃/2024-08-28 23:22:36

Clion+OpenCV(C++ Edition) Development Environment Configuration Tutorial Win/Mac

Usually in the study and competition are used in the Python version of OpenCV, recently encountered a project using the host computer performance is limited so decided to use the visual aspects of OpenCV in C++ to save the host computer resources to improve the speed of operation, in the view of a variety of information on the Internet found that the information is uneven some blogs around and around the way, and some blogs are not too detailed for each step. too detailed.So I decided to record myself and share the process of configuring my environment for your reference.

Windows Configuration Methods

I. Download relevant software and tools

Here are some of the development and configuration of the environment needs to be used in the process of software and tools, here to give my personal test can use the link, if you can not open or slow download speed you can try Science on the Internet!

​      · Clion:CLion: A Cross-Platform IDE for C and C++ by JetBrains

​      · OpenCV:OpenCV 4.10.0_Github

Download the Winodws version of the exe installation file

      · Cmake:Cmake_Binary distributions

In the Binary distributions section, select the version that suits your needs and download it.

1

  Caveats:

​     - Clion and OpenCV recommend to download the latest version

​     - If OpenCV needs to use a previous version, you need to re-download MinGW additionally.

​     - All installation paths should not be in Chinese! Definitely no Chinese!!!

II. Installation process

!!! Check again to see if it has been downloaded in your own computerInstallation packages for Clion, OpenCV, Cmake。!!!

(i) Clion installation

Clion is Jetson launched a C and C + + integrated development environment, which can support embedded development, although this software is paid but if you are students or educators can apply for free use but the speed through the slow roughly 1Days like, there is another way to do not do too much reminiscence here (why don't you take a look at the almighty Tao Bao it???). Why don't you check out the almighty Tao Po?)Meanwhile, since Clion comes with MinGW, you don't actually need to download MinGW again during the process of configuring the C environment, as other online tutorials have suggested!

Clion's installation is relatively simple, we will not expand in detail here, the only thing to note is that in the installation interface, remember to check the "Add Bin file to the PATH" to avoid adding too much trouble later on, but after the installation is complete, you need to reboot to update the system environment variables.

2

(ii) Downloading and compiling OpenCV

Next, open the OpenCV we downloaded from Github"opencv-4.10."Installation package, double-click to run after the reader will let the reader choose a directory to decompress to generate a package of OpenCV files, there are two folders a "build" is the official pre-compiled can be used directly in the C + + functional package, and a "sources" is the corresponding source code. The details are shown in the picture below:

3

Although we can directly use the official compiled files for development, but our own computer Clion's MinGW and other environments and has been compiled do not match some of the more strange problems are prone to occur, so it is recommended that we re-compile, this time you need to use our Cmake, we open our first download Cmake installer, in the Bin files can find "cmake_gui.exe" this software double-click to open.

4

After opening Cmake then we will Clion comes with MinGW added to the system environment variables to compile, first of all, open the system settings, in the search box, search for "environment" select Edit System Environment Variables, in the pop-up window, select the "Environment Variables" a

ocd2

Then double-click on "Path" in the "System Variables" pane below to enter the editing window of the system environment variable.

ocd3

Then click Browse in the pop-up window to find the installation path of Clion and select "bin"-> "mingw"-> "bin".

5

After adding MinGw to the environment variable, we can start compiling, we open Cmake, in the first column of the path, select the OpenCV source folder we unzipped at the beginning of the generation of the folder "sources", and then in the third column to add a compiled files stored in the address, this address can be independently selected in the statistics directory of the opencv source code, we recommend that a new folder be created in the statistics directory. It is recommended to create a new folder in the statistics directory of the opencv source code to store the compiled files, and then you can click Configure in the lower left corner to configure it.(The first click will bring up the compile options, select MinGW MakeFiles in the drop down box and click Finish to start configuration)

67

In this process there may be a compilation failure, then we should not be anxious, generally there will be only two kinds of error report the first is the beginning of the prompt can not find MinGW this is generally the failure to add the environment variables caused by the check found that there is no problem with the environment variables, you can reboot the computer to update the configuration, and then if it is the configuration of the process of reporting errors or failures is generally the source code in the ffmpeg folder lack of files compiler automatically download failure (no XXXX, then it will be very slow or even failure, it is recommended to copy the address from the log to the browser to download their own ffmpeg folder added to the opencv source code), the ffmpeg folder.Remember to delete the compiled folder if you restart the configuration each time!!!! (Just delete the storage directory)Avoid previous error reporting to have an effect on this one.

If the log window shows (Configure Done) the configuration was successful.

8

Next, we can click on "Generate" to generate MakeFiles, there is "Generate Done!

9

At this point we can open the build directory we created (mine is called mingw_build), then open CMD in the current folder (type CMD in the path pane above the folder to open CMD in the path of the current folder), and in the CMD window that appears, type in "mingw32-make -j4" to start compiling.(-j4 means to use 4 threads to compile, if the computer configuration is higher you can change the appropriate number of threads to accelerate compilation)

10

The compilation begins as shown

11

If there is no red error in the middle of the compilation is complete, if there is an error in the middle of the compilation may be due to the MinGw and OpenCV version is not compatible with the latest OpenCV and the latest CLion comes with MinGW can be perfectly adapted to if there are problems with the compilation of the version you can check the version of the appropriate to raise or lower the version of the MinGW / OpenCV!

13

After compiling without errors we can type "mingw32-make install" again to generate our compiled environment installation package

14

This process is generally not reported as an error

14

The following figure is the completion of the installation, then we compile the environment generated by the installation package bin folder ("opencv"-> "mingw_build"-> "install"-> "x64"-> "mingw"-> "bin") added to the environment variable can be

15

    The C++ version of OpenCV for Windows has been installed, please see the last section for how to use it in Clion.

Mac Configuration Methods

Mac computers running Mac OS kernel is evolved from Unix, so the Mac system also has the same package management tools as Linux, the most well-known is HomeBrew, through this tool we can configure a good OpenCV development environment is very convenient. To install OpenCV we need to install HomeBrew, then we will step by step to elegantly configure the OpenCV environment.

#First check if HomeBrew is present on your computer.
brew -v #If there is an output, it means HomeBrew exists in your computer.
#If HomeBrew is present, uninstall it first.
/bin/bash -c "$(curl -fsSL /Homebrew/install/HEAD/)"
#Similar to ROS, HomeBrew also has a one-click automated install script made by the gods (but may require Science on the Internet)
/bin/bash -c "$(curl -fsSL /Homebrew/install/HEAD/)" #Just follow the prompts and install step by step.

After installing HomeBrew, we can install OpenCV with a single command.

brew install opencv #Install OpenCV

Just be patient and wait for the run to complete

The C++ version of OpenCV for Mac has been installed, see the last section for how to use it in Clion.

Testing OpenCV

We create a new project in Clion and modify the CmakeLists file generated by default

cmake_minimum_required(VERSION 3.19)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV)

include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(untitled )

target_link_libraries(untitled ${OpenCV_LIBS})

Then run the following code

#include <iostream>
#include <opencv2/>

using namespace std;
using namespace cv;

int main() {
    Mat srcImage = imread("Your image path");
    if (!) {
        std::cout << "Image not loaded";
        return -1;
    }
    imshow("image", srcImage);
    waitKey(0);
    return 0;
}

    No errors and the image is displayed properly means the installation is successful!!!!

Possible errors

  1. (of a computer) runbrew install opencvreport an error:Error: /usr/local/opt/qt is not a valid keg

    First delete the contents of the /usr/local/opt/qt folder (a backup is recommended):

    # Copy to make a backup, replacing xxxxxx with your username
    cp -r /usr/local/opt/qt /Users/xxxxx/
    # Delete the file, sudo it if you need root privileges
    rm -r /usr/local/opt/qt
    

    Follow the prompts from brew doctor to create a symbolic connection:

     brew link --overwrite qt
    

    Then just run the install or update command again

     brew install qt
     # maybe
     brew upgrade
    

If you encounter any problems during the configuration process, please feel free to come to me to solve them or ask me to improve and supplement this tutorial!

If you encounter any problems during the configuration process, please feel free to come to me to solve them or ask me to improve and supplement this tutorial!

Everyone in the configuration process encountered any problems welcome to come to me to solve or find the end of my perfect additions to this tutorial!