Use OpenCV with Xcode

0xkoji - Nov 15 '20 - - Dev Community

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .