DEV Community

0xkoji
0xkoji

Posted on

Use OpenCV with Xcode

Step1 Install OpenCV via Homebrew

$ brew install opencv
$ brew install -v cmake
Enter fullscreen mode Exit fullscreen mode

opencv 4.5.0_3 will be installed.

Step2 Set Header Search Paths

Build Settings > Search Header Search Paths

Alt Text

Add this to paths and select recursive
/usr/local/Cellar/opencv/4.5.0_3/include

Step3 Add .dylib to Link Binary With Libraries

Technically, you don't need to pass all .dylib, but in this case, I passed all .dylib under /usr/local/Cellar/opencv/4.5.0_3/lib.

Step 4 Build code

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main(){
    cv::Mat imMat(400,400, CV_8UC3);

    for(int y = 0 ; y < imMat.rows; y++){
        for(int x = 0 ; x < imMat.cols; x++){
            cv::Vec3b &p = imMat.at<cv::Vec3b>( y, x);
            p[0] = x;
            p[1] = y;
            p[2] = (int)((x+y)/2);
        }
    }
    imshow("openCVTest",imMat);
    waitKey(0);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

If you can build, you will see ↓

Alt Text

Top comments (2)

Collapse
 
pertuit profile image
René Pertuit

Hi,
Thank you for this working installation. This is all very well, but how do you export this program to another computer without having to reinstall all OpenCV on this computer? This is a problem I currently have: when launching the program (the built program) on another computer where OpenCV is not installed, it does not work:
It claims that the Dylibs be placed in /usr/local/opt/opencv/lib/xxx.dylib, although I put them in the program folder. Is there a setting to be made in Xcode to allow the export to another computer (without having to reinstall everything)? Do you have a solution to avoid reinstalling all OpenCV on each computer where the program is copied?
Thanks.

Collapse
 
0xkoji profile image
0xkoji

Probably, you will need to use static libraries.