Use OpenCV4 with Xcode

Subscribe to my newsletter and never miss my upcoming articles

Step1 Install OpenCV via Homebrew

$ brew install opencv
$ brew install -v cmake

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

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 =<cv::Vec3b>( y, x);
            p[0] = x;
            p[1] = y;
            p[2] = (int)((x+y)/2);
    return 0;

If you can build, you will see ↓

Alt Text

Share this
Proudly part of