How to Install OpenCV on Mac and CentOS?

OpenCV is an open-source cross-platform computer vision library released under BSD open source license.

It provides C ++, C, Python, Java and other language call interface, you can run on Windows, Linux, Mac OS, iOS, Android and other operating systems.

Because you need to call an OpenCV image recognition algorithm, the way to sort out the OpenCV 2.4 Mac and CentOS under the installation and configuration.

Install OpenCV on Mac

The project development environment is on Mac. You can use Homebrew to install OpenCV on Mac. To install OpenCV on Mac you need to make sure the xCode have installed.

Homebrew installation is as follows

Before you do this, make sure you have Homebrew installed.

First, find the installation package:

$ brew search opencv
opencv opencv@2

To install version 2.x of OpenCV, use the following command:

$ brew install opencv@2

The following is the process of compiling and installing OpenCV

1. Installcmake

Source installation, you need to use cmake to build the project. First, need to installcmake

$ sudo brew install cmake

2. Download OpenCV

Download OpenCV and switch to the specified branch:

$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git checkout 2.4

Currently, OpenCV main branch version 3.3+, because we want to use the 2.4 version, so after downloading you need to switch to the 2.4 branch.

3. Compile and install OpenCV

$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

4. Verify installation

OpenCV source samples directory, OpenCV contains a number of sample programs, we can use these examples to verify the OpenCV installation is successful.

First, compile these examples:

$ cd ../samples
$ mkdir build
$ cd build
$ cmake ..
$ make

After compiling, you can run any one of the examples to verify, such as:

$ ./cpp/cpp-example-em

How to Install OpenCV on Mac and CentOS?

Using OpenCV in Xcode

1. Create a project

Open xCode and create a macOS-Command Line Tool project with C ++ project language selection:

How to Install OpenCV on Mac and CentOS?

2. Configure the path

Select the target of the project, and configure its Header Search Path and Library Search Path to find the path, where:

Header Search Path is configured as: /usr/local/include
Library Search Path is configured as: /usr/local/lib

How to Install OpenCV on Mac and CentOS?

3. Add a library reference to the project

Create a new project called libs group (New Group), and then press shift + command + g in the pop-up window, type /usr/local/lib. After entering the specified directory, the files are sorted by type, and file references such as libopecv _ *. Dylib are added to the libs group.

How to Install OpenCV on Mac and CentOS?

4. Write the project code

Write the following code in main.cpp:

//
// main.cpp
// OpencvTest

# include &ltiostream>
# include &ltopencv2/core/core.hpp>
# include &ltopencv2/highgui/highgui.hpp>
# include &ltopencv2/opencv.hpp>

using namespace std;
using namespace cv;

IplImage* doCanny(IplImage* image_input,
                  double lowThresh,
                  double highThresh,
                  double aperture)
{
    if(image_input->nChannels != 1)
        return (0);

    IplImage* image_output = cvCreateImage(cvGetSize(image_input),
                                           image_input->depth,
                                           image_input->nChannels);

    cvCanny(image_input,image_output,lowThresh,highThresh,aperture);

    return(image_output);
}

int main(int argc, char* argv[])
{
    cvNamedWindow("Camera" , CV_WINDOW_AUTOSIZE );

    CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY);

    assert(capture != NULL);

    IplImage *frame = 0;
    frame = cvQueryFrame(capture);

    IplImage *frame_edge = cvCreateImage(cvGetSize(frame),
                                         IPL_DEPTH_8U,
                                         1);
    while(1)
    {
        frame = cvQueryFrame(capture);
        if(!frame) break;

        cvConvertImage(frame,frame_edge,0);
        frame = cvCloneImage(frame_edge);

        frame_edge = doCanny(frame_edge,70,90,3);

        cvShowImage("Camera",frame_edge);
        char c = cvWaitKey(15);
        if(c == 27) break;
    }

    cvReleaseCapture(&capture);
    cvReleaseImage( &frame_edge );
    cvReleaseImage( &frame);


    return (int)0;
}

5. Run the project

After compiling the code compiled and run.

Install OpenCV on Linux (CentOS 7)

The project’s final operating environment is Linux with the CentOS release installed, and the OpenCV installation in CentOS 7 is as follows.

2.1 install dependent libraries

sudo yum install autoconf automake cmake freetype-devel gcc gcc-c++ git libtool make mercurial nasm pkgconfig zlib-devel

2.2 install FFmpeg

FFmpeg is used to decode the H264 encoded video data stream and convert it to the OpenCV processable Mat format before further processing by OpenCV. If you have the relevant needs of the project, you need to install FFmpeg.

Compile and install FFmpeg as follows:

$ git clone git://source.ffmpeg.org/ffmpeg
$ cd ffmpeg
$ ./configure --enable-shared
$ make
$ sudo make install

After installation, you can check whether the installation was successful by the ffmpeg -version command:

$ ffmpeg -version
ffmpeg version 2.8.13 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-11)
...

2.3 Install OpenCV

We can download the source code from the GitHub official OpenCV repository, and then compile and install.

The steps are as follows:

$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git checkout 2.4
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install

After the installation is complete, the same sample can be compiled samples directory to verify the installation was successful.