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
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install jpeg libpng libtiff openexr
brew install opencv
find path of OpenCV. in order to see hidden folders and file in Mac you can use "Command+Shift+Dot"
export PKG_CONFIG_PATH="/usr/local/Cellar/opencv/4.7.0_1/lib/pkgconfig:$PKG_CONFIG_PATH"
pkg-config --cflags opencv4
s
Makefile
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
tasks.json
{
"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"
]
}
]
}
launch.json
{
"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"
}
]
}
if you do not want external terminal (you want to use internal terminal in vscode) you may change the line
"externalConsole": false
if you do not want debug and run line by line the code and not stop in first line of code (F10 or F5 to continue) you may change the line to
"stopAtEntry": false
c_cpp_properties.json
{
"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
}
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install jpeg libpng libtiff openexr
brew install opencv
export PKG_CONFIG_PATH="/usr/local/Cellar/opencv/4.7.0_1/lib/pkgconfig:$PKG_CONFIG_PATH"
pkg-config --cflags opencv4
in VS code add below into c_cpp_properties.json file in .vscode
"includePath": [ ... "/usr/local/opt/opencv/include/opencv4" ...
Command+Shift+Dot.
/usr/local/Cellar/opencv/4.7.0_1/include/opencv4/**
copy .dylib files from lib folder (you can copy directly into your project or create folder and copy there)
#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.
Real-time object detection: With the help of OpenCV's object detection algorithms and Metal's parallel processing capabilities, developers can build an application that can detect and track objects in real-time video streams.
Image segmentation: Image segmentation is the process of dividing an image into different regions or segments, each of which represents a different object or part of the image. Using OpenCV with Metal, developers can build an application that can perform image segmentation in real-time, making it useful for various applications such as medical imaging.
Facial recognition: With OpenCV's facial recognition algorithms and Metal's parallel processing capabilities, developers can build a facial recognition system that can quickly and accurately identify people in real-time.
Machine learning: OpenCV provides a rich set of machine learning tools, and Metal's parallel processing capabilities can be used to train and run machine learning models in real-time.
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.
brew install pkg-config
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
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 pkg-config
brew install jpeg libpng libtiff openexr
brew install wget
brew install --cask yuna
sudo ./cmake-gui
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
git branch -a
git switch 5.x
mkdir build_opencv
cd build_opencv
cmake gui
opencv flolder
opencv build folder
Unix makefile compiler ( do not select Xcode)
OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib>/modules
OPENCV_ENABLE_NONFREE=ON
configure again
generate
remove below items
zlib
Java = 2x
imgcode
ipp = 2x
xfeatures2d
face
wechat qrcode
imgproc
ade
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 pkg-config
brew install jpeg libpng libtiff openexr
brew install wget
brew install --cask yuna
sudo ./cmake-gui
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
git branch -a
git switch 5.x
mkdir build_opencv
cd build_opencv
cmake
opencv flolder
opencv build folder
Unix makefile compiler ( do not select Xcode)
OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib>/modules
OPENCV_ENABLE_NONFREE=ON
configure again
generate
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
git branch -a
git switch 5.x
mkdir build_opencv
cd build_opencv
cmake gui
opencv flolder
opencv build folder
Unix makefile compiler ( do not select Xcode)
OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib>/modules
configure again
generate
remove below items
zlib
Java = 2x
imgcode
ipp = 2x
xfeatures2d
face
wechat qrcode
imgproc
ade
make -j8
sh setup_vars.sh
sudo make install
brew install pkg-config
/usr/local/include/opencv5/**