MacOS+OpenCV

https://www.tiziran.com/topics-and-projects/source-code/opencv/macos-opencv 

YouTube Channel: https://www.youtube.com/c/ComputerVisionDeepLearning 

OpenCV on MacOS - 2023

How to compile OpenCV on Mac

How to use OpenCV with Xcode (C++)

To view these steps, you may watch a video on YouTube.  https://youtu.be/dgYy4Cf1qO4 


TARGET = ./main

SRCS := $(wildcard ./src/*.cpp ./*.cpp)

OBJS := $(patsubst %cpp,%o,$(SRCS))

CFLG = -g -Wall -I/usr/local/Cellar/opencv/4.7.0_1/include/opencv4 -Iinc -I./ -std=c++17

LDFG = -Wl, $(shell pkg-config opencv --cflags --libs)

CXX = g++

$(TARGET) : $(OBJS)

$(CXX) -o $(TARGET) $(OBJS) $(LDFG)

%.o:%.cpp

$(CXX) $(CFLG) -c $< -o $@ 

clean:

-rm ./*.o

    "version": "2.0.0",

    "tasks": [

        {

            "label": "Build",

            "type": "shell",

            "command": "g++",

            "args": [

                "-std=c++17",

                "${file}",

                "-o",

                "${fileDirname}/${fileBasenameNoExtension}.out",

                "-I",

                "/usr/local/Cellar/opencv/4.7.0_1/include/opencv4/opencv2",

                "-I",

                "/usr/local/Cellar/opencv/4.7.0_1/include/opencv4",

                "-L",

                "/usr/local/Cellar/opencv/4.7.0_1/lib",

                "-l",

                "opencv_stitching",

                "-l",

                "opencv_superres",

                "-l",

                "opencv_videostab",

                "-l",

                "opencv_aruco",

                "-l",

                "opencv_bgsegm",

                "-l",

                "opencv_bioinspired",

                "-l",

                "opencv_ccalib",

                "-l",

                "opencv_dnn_objdetect",

                "-l",

                "opencv_dpm",

                "-l",

                "opencv_face",

                "-l",

                "opencv_fuzzy",

                "-l",

                "opencv_hfs",

                "-l",

                "opencv_img_hash",

                "-l",

                "opencv_line_descriptor",

                "-l",

                "opencv_optflow",

                "-l",

                "opencv_reg",

                "-l",

                "opencv_rgbd",

                "-l",

                "opencv_saliency",

                "-l",

                "opencv_stereo",

                "-l",

                "opencv_structured_light",

                "-l",

                "opencv_phase_unwrapping",

                "-l",

                "opencv_surface_matching",

                "-l",

                "opencv_tracking",

                "-l",

                "opencv_datasets",

                "-l",

                "opencv_dnn",

                "-l",

                "opencv_plot",

                "-l",

                "opencv_xfeatures2d",

                "-l",

                "opencv_shape",

                "-l",

                "opencv_video",

                "-l",

                "opencv_ml",

                "-l",

                "opencv_ximgproc",

                "-l",

                "opencv_xobjdetect",

                "-l",

                "opencv_objdetect",

                "-l",

                "opencv_calib3d",

                "-l",

                "opencv_features2d",

                "-l",

                "opencv_highgui",

                "-l",

                "opencv_videoio",

                "-l",

                "opencv_imgcodecs",

                "-l",

                "opencv_flann",

                "-l",

                "opencv_xphoto",

                "-l",

                "opencv_photo",

                "-l",

                "opencv_imgproc",

                "-l",

                "opencv_core",

                "-g"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "problemMatcher": [

                "$gcc"

            ]

        }

    ]

}

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "(lldb) Launch",

            "type": "cppdbg",

            "request": "launch",

            "program": "${fileDirname}/${fileBasenameNoExtension}.out",

            "args": [],

            "stopAtEntry": true,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "lldb",

            "preLaunchTask": "Build"

        }

    ]

}

{

    "configurations": [

        {

            "name": "Mac",

            "includePath": [

                "${workspaceFolder}/**",

                "/usr/local/Cellar/opencv/4.7.0_1/include/opencv4",

                "/usr/local/Cellar/opencv/4.7.0_1/include"

            ],

            "defines": [],

            "macFrameworkPath": [],

            "compilerPath": "/usr/bin/g++",

            "cStandard": "c17",

            "cppStandard": "c++17",

            "intelliSenseMode": "clang-x64",

            "browse": {

                "path": [

                    "/usr/local/Cellar/opencv/4.7.0_1/include/opencv4"

                ],

                "limitSymbolsToIncludedHeaders": true,

                "databaseFilename": ""

            }

        }

    ],

    "version": 4

}

Command+Shift+Dot.


#include <iostream>

#include "opencv2/opencv.hpp"

 

using namespace cv;

using namespace std;



int main(int argc, const char * argv[]) {

    // insert code here...

    cout << "OpenCV version : " << CV_VERSION << endl;

    cout << "Major version : " << CV_MAJOR_VERSION << endl;

    cout << "Minor version : " << CV_MINOR_VERSION << endl;

    cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;

    

    std::cout << "Hello, World!\n";

    return 0;

}


To learn how to install OpenCV (C++) on a MacOS and utilize it in an Xcode project with a simple configuration, you can watch a video on YouTube. The video will include a download link, source code, and additional documents for reference. It is scheduled to be released in 2023.

OpenCV (C++) is a popular computer vision library that allows developers to perform various image and video processing tasks such as object detection, face recognition, and image segmentation. Using OpenCV on a MacOS provides developers with a stable and reliable platform to build their computer vision applications. Additionally, MacOS has a user-friendly interface and powerful development tools, such as Xcode, which can help streamline the development process. Overall, utilizing OpenCV on a MacOS can help developers create high-performance computer vision applications with ease.


Metal is a low-level graphics framework developed by Apple that can be used in conjunction with OpenCV (C++) on a MacOS. Metal provides a high-performance computing environment for developers to process large amounts of data while minimizing CPU usage. By combining the power of Metal with the feature-rich OpenCV library, developers can create high-performance computer vision applications that are capable of processing large amounts of data in real-time.

Metal's efficient graphics pipeline and parallel processing capabilities make it ideal for use in computer vision applications. It provides a high level of performance and scalability for tasks such as image and video processing, object recognition, and machine learning. Additionally, Metal's seamless integration with the Xcode development environment makes it easy to use in combination with OpenCV for MacOS application development. Overall, using Metal with OpenCV (C++) on a MacOS can help developers create high-performance and efficient computer vision applications.

Overall, the combination of OpenCV with Metal on a MacOS provides developers with a powerful platform to build a wide range of computer vision applications.



Compile 1:

sudo xcodebuild -license

sudo xcode-select --install

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"




brew install cmake 

brew install --cask cmake




cd ~/<my_working_directory>

git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git

mkdir build_opencv

cd build_opencv

cmake gui


make -j8


sh setup_vars.sh 

sudo make install




Compile 3:

sudo xcodebuild -license

sudo xcode-select --install

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"




brew install cmake 

brew install --cask cmake




cd ~/<my_working_directory>

git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git

mkdir build_opencv

cd build_opencv

cmake 



cmake -D CMAKE_BUILD_TYPE=RELEASE \

    -D CMAKE_INSTALL_PREFIX=/usr/local \

    -D OPENCV_EXTRA_MODULES_PATH=/Users/farshid/code/opencv_contrib/modules \

    -D PYTHON3_LIBRARY=`python -c 'import subprocess ; import sys ; s = subprocess.check_output("python-config --configdir", shell=True).decode("utf-8").strip() ; (M, m) = sys.version_info[:2] ; print("{}/libpython{}.{}.dylib".format(s, M, m))'` \

    -D PYTHON3_INCLUDE_DIR=`python -c 'import distutils.sysconfig as s; print(s.get_python_inc())'` \

    -D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python \

    -D BUILD_opencv_python2=OFF \

    -D BUILD_opencv_python3=ON \

    -D INSTALL_PYTHON_EXAMPLES=ON \

    -D INSTALL_C_EXAMPLES=OFF \

    -D OPENCV_ENABLE_NONFREE=ON \

    -D BUILD_EXAMPLES=ON ../opencv


rm CMakeCache.txt


make -j8


sh setup_vars.sh 

sudo make install



Compile:

brew install cmake 

brew install --cask cmake

cd ~/<my_working_directory>

git clone https://github.com/opencv/opencv.git

git clone https://github.com/opencv/opencv_contrib.git

mkdir build_opencv

cd build_opencv

cmake gui


make -j8


sh setup_vars.sh 

sudo make install



brew install pkg-config


/usr/local/include/opencv5/**