Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello OpenCV in macOS #93

Open
soapgu opened this issue Dec 27, 2021 · 0 comments
Open

Hello OpenCV in macOS #93

soapgu opened this issue Dec 27, 2021 · 0 comments
Labels

Comments

@soapgu
Copy link
Owner

soapgu commented Dec 27, 2021

图片

  • 什么是OpenCV

最近人工智能概念大火,人脸识别、人脸支持、无人驾驶都属于OpenCV的应用领域。
OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和Mac OS操作系统上。 它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

  • 在macOS上安装OpenCV库

书上和网上的教程主要安装的环境是windows的。对于macOS来说只能借鉴,引用OpenCV关键需要引用VC的“库文件”和“头文件”。

  1. 安装brew
    brew就是一个功能强大的包管理工具。以前我们用Linux使用Debian或者是CenterOS都,软件安装都是非常方便,yum或者是atp命令非常熟悉。而用apple反而就没这么顺手了,AppStore很多开发包都是缺缺缺。
    安装了brew以后就感觉macOS有了Linux的灵魂

注意官网上的安装方式是无效的
所以我们不得不另辟蹊径。

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
  • HomebrewCN
    到处都有活雷锋,安装和镜像问题一站式解决了
  1. 从brew安装opencv必备组件
  • 安装wget
    brew install wget
  • 安装cmake
    brew install cmake

还不够,如果现在安装opencv的话会报

Error: gcc: the bottle needs the Apple Command Line Tools to be installed

是gcc组件没装,这个brew建议我们从XCode里面更新,直接控制台输入xcode-select --install
接下来就是耐心等待就行

  1. 从brew安装opencv库
    brew install opencv
    键入命令等就行。我的电脑在装一个依赖包失败,我单独安装了zstd这个包后再执行成功的。

官网上还有直接拉源代码编译的方式安装Installation in MacOS,不过我觉得还是brew更方便一点

  • Quick Start

终于到最后demo实现阶段了
先用XCode-> File->New->Project
选择Command Line Tool ,语言选择C++
设置头文件目录和库文件目录

这里有个小插曲,网上博客上的地址是
header search paths :/usr/local/include/opencv4
Library search paths:/usr/local/Cellar/opencv/4.1.0_2/lib

而我本机并没有。明明装上了文件都到哪里去了哪里了?
从brew命令里面找

brew info opencv    
opencv: stable 4.5.4 (bottled)
Open source computer vision library
https://opencv.org/
/opt/homebrew/Cellar/opencv/4.5.4_1 (845 files, 119.5MB) *
  Poured from bottle on 2021-12-26 at 21:43:58
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/opencv.rb
License: Apache-2.0
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: ceres-solver ✔, eigen ✔, ffmpeg ✔, glog ✔, harfbuzz ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, openblas ✔, openexr ✔, protobuf ✔, [email protected] ✔, tbb ✔, vtk ✔, webp ✔
==> Analytics
install: 16,822 (30 days), 42,891 (90 days), 184,402 (365 days)
install-on-request: 16,050 (30 days), 41,165 (90 days), 176,702 (365 days)
build-error: 136 (30 days)

事后证明是博客的地址也是对的,邓兄就是用这个地址找到。 可能匹配的macOS版本或者机型较老一些。

  1. 设置Header Search Path & Library Search Path

图片

  • 选择 项目(我选到target了,乌龙)
  • Build Settings
  • Search Paths

增加path

  1. 增加一个目录,添加库文件
    由于是保护目录,必须输入shift + command + G,输入/opt/homebrew/Cellar/opencv/4.5.4_1/lib,把libopencv_开头的文件全加进去
  • 两处更正
  • 只需要 .dylib文件,不需要静态库.a
  • 不需要Copy Items if needed,应该是Libary Search Path已经设置到关系吧
  1. 最后在main.cpp输入源码
//
//  main.cpp
//  HelloOpenCV
//
//  Created by guhui on 2021/12/26.
//

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
 // insert code here...
    Mat image;
    image = imread("/Users/guhui/Desktop/1.jpg",1);
    namedWindow("display",WINDOW_AUTOSIZE);
    imshow("display",image);

    Mat gray;
    cvtColor(image, gray,COLOR_RGBA2GRAY);
    namedWindow("gray");
    imshow("gray", gray);
    waitKey(0); //等待直到有键按下
    return 0;
}

图片

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant