今天安装下opencv,在官方下载最新版,我们先安装FFmpeg 安装FFmpeg之前也先安装音频解码
http://www.audiocoding.com/downloads.html
下载这两个 注意,我最新版mp3lame编译没有通过,用3.99版
对不熟linux同学可能比较陌生,现在linux很少有人手工编译安装了。
如果你是64位可以取消i386 不然会报错,我是取消的
https://www.tuicool.com/articles/EJvumq 这个地址有安装
mp3lame:
curl -L -o lame-3.99.5.tar.gz http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --disable-dependency-tracking CFLAGS="-arch -arch x86_64" LDFLAGS="-arch -arch x86_64"
make
sudo make install
cd ..
faac (notice that only compiles for x86_64):
curl -L -o faac-1.28.tar.gz http://sourceforge.net/projects/faac/files/faac-src/faac-1.28/faac-1.28.tar.gz/download
tar xzvf faac-1.28.tar.gz
cd faac-1.28
./configure --disable-dependency-tracking CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64"
make
sudo make install
cd ..
faad:
curl -L -o faad2-2.7.tar.gz http://sourceforge.net/projects/faac/files/faad2-src/faad2-2.7/faad2-2.7.tar.gz/download
tar xvzf faad2-2.7.tar.gz
cd faad2-2.7
./configure --disable-dependency-tracking CFLAGS="-arch -arch x86_64" LDFLAGS="-arch -arch x86_64"
make
sudo make install
cd ..
ffmpeg:
curl -O http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.gz
tar xzvf ffmpeg-0.11.1.tar.gz
cd ffmpeg-0.11.1
./configure --enable-libmp3lame --enable-libfaac --enable-nonfree --enable-shared --enable-pic --disable-mmx --arch=x86_64
make
sudo make install
cd ..
经过漫长安装。
现在终于可以安装opencv了
https://www.opencv.org/releases.html
opencv 3.4.0
解压出来
我是用cmake编译
目录里面建一个编译目录我用release目录。
取消java接口,如果你用到可以不取消,cuda我的macbook 是集显, 以取消这个。
点configure,然后就会出来模块列表了,然后点generate配置项目。然后点open project 会打开xcode编译
选择编译三角头右边 all_build MyMac 然后编译完再点下拉选择 install MyMac
现在opencv已经安装到你的电脑/usr/local里面了
/usr/local/include/opencv /usr/local/include/opencv2就是头文件
库文件在/usr/local/lib
现在安装完毕,我们要测试下,找一个目录,里面建一个opencv.cpp 和一张图片beihn.jpg
#include
#include
#include
using namespace cv;
int main()
{
// 读入一张图片(游戏原画)
String file="beihn.jpg";
Mat img=imread(file,1);
// 创建一个名为 "游戏原画"窗口
namedWindow("游戏原画");
// 在窗口中显示游戏原画
imshow("游戏原画",img);
// 等待6000 ms后窗口自动关闭
waitKey(6000);
return 1;
}
g++ opencv.cpp -std=c++1z -I/usr/local/include/ -L/usr/local/lib/ -lopencv_highgui.3.4.0 -lopencv_core.3.4.0 -lopencv_imgcodecs
编译参数和搜索目录 用到那个模块就加上那个我们用了三个
"cv::imread(cv::String const&, int)", referenced from:
_main in opencv-66163f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
就是缺少 -lopencv_imgcodecs 参数 可以加上去,什么都不加编译参数如下错误
g++ opencv.cpp -std=c++1z
Undefined symbols for architecture x86_64:
"cv::namedWindow(cv::String const&, int)", referenced from:
_main in opencv-7d4459.o
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in opencv-7d4459.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in opencv-7d4459.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(char const*) in opencv-7d4459.o
"cv::imread(cv::String const&, int)", referenced from:
_main in opencv-7d4459.o
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
_main in opencv-7d4459.o
"cv::waitKey(int)", referenced from:
_main in opencv-7d4459.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() in opencv-7d4459.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果正确安装的话,会出现你那张图片,6秒会自动关闭。
对于xcodeproj工程我们怎么添加/usr/local/lib库呢,因为xcode 点项目General- linked Frameworks and libraries
点下面+是无法添加的,因为是隐藏目录,我们直接拖进去
打开finder 菜单 前往 前往文件夹 输入/usr/local/lib就出来了,不然你还会遇到前面那个没有加库的错误
前面那个图片要用绝对路径比发/User/hzq/bbb/beihn.jpg
#include
#include
#include
using namespace cv;
int main()
{
// 读入一张图片(游戏原画)
String file="/Users/hzq/bbb/beihn.jpg";
Mat img=imread(file,1);
// 创建一个名为 "游戏原画"窗口
namedWindow("游戏原画");
// 在窗口中显示游戏原画
imshow("游戏原画",img);
// 等待6000 ms后窗口自动关闭
waitKey(6000);
return 1;
}