在mac os上开始使用opencv

时间:2016-07-28 21:10:10

标签: c++ macos opencv makefile

我在OSX 10.10.5上,是opencv的新手。

在意识到我安装了旧版本的opencv(2.4.6)之前,我安装了最新的opencv3,内容如下:

brew tap homebrew/science
brew install opencv3 --with-contrib 

我无法将命令行中的简单opencv示例与旧的opencv库或新的opencv库链接起来。

这是我的Makefile:

CC = g++

# Use the following to access new install of opencv3
#CFLAGS = -I/usr/local/Cellar/opencv3/3.1.0_3/include
#LDFLAGS =  -L /usr/local/Cellar/opencv3/3.1.0_3/lib/ -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc

# Use the following to access old install of opencv (2.4.6) 
CFLAGS = -I/opt/local/include
LDFLAGS =  -L /opt/local/lib/ -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc
ALL = vision

all: $(ALL)

vision: vision.o
    $(CC) $(LDFLAGS) -o $@ $^

vision.o: vision.cpp
    $(CC) $(CFLAGS) -c $<

.PHONY: clean

clean:
    rm -rf *.o core* $(ALL)

在这两种情况下,文件都会编译但无法链接相同的错误:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in vision.o
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in vision.o
...

然而opencv libs就在那里:

旧的2.4.6(例如highgui,但其他的也存在):

$ ls -l /opt/local/lib/libopencv_highgui*
-rwxr-xr-x  1 root  wheel  328972 Sep 23  2013 /opt/local/lib/libopencv_highgui.2.4.6.dylib
lrwxr-xr-x  1 root  wheel      29 Oct  8  2013 /opt/local/lib/libopencv_highgui.2.4.dylib -> libopencv_highgui.2.4.6.dylib
lrwxr-xr-x  1 root  wheel      27 Oct  8  2013 /opt/local/lib/libopencv_highgui.dylib -> libopencv_highgui.2.4.dylib

或新的:

$ ls -l /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.*
-r--r--r--  1 lolo  admin  61752 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.3.1.0.dylib
lrwxr-xr-x  1 lolo  admin     29 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.3.1.dylib -> libopencv_highgui.3.1.0.dylib
lrwxr-xr-x  1 lolo  admin     27 Jul 22 15:27 /usr/local/Cellar/opencv3/3.1.0_3/lib/libopencv_highgui.dylib -> libopencv_highgui.3.1.dylib

供参考,我试图运行的示例代码:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgcodecs.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( )
{

       Mat image;

       // LOAD image
       image = imread("image1.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file "image.jpg".
              //This file "image.jpg" should be in the project folder.
              //Else provide full address : "D:/images/image.jpg"

       if(! image.data )  // Check for invalid input
       {
              cout <<  "Could not open or find the image" << std::endl ;
              return -1;
       }

       //DISPLAY image
       namedWindow( "window", CV_WINDOW_AUTOSIZE ); // Create a window for display.
       imshow( "window", image ); // Show our image inside it.

       //SAVE image
       imwrite("result.jpg",image);// it will store the image in name "result.jpg"

       waitKey(0);                       // Wait for a keystroke in the window
       return 0;
}

我当前的软件包配置虽然我认为不应该考虑到我如何链接:

pkg-config --cflags --libs opencv
-I/opt/local/include/opencv -I/opt/local/include  /opt/local/lib/libopencv_calib3d.dylib /opt/local/lib/libopencv_contrib.dylib /opt/local/lib/libopencv_core.dylib /opt/local/lib/libopencv_features2d.dylib /opt/local/lib/libopencv_flann.dylib /opt/local/lib/libopencv_gpu.dylib /opt/local/lib/libopencv_highgui.dylib /opt/local/lib/libopencv_imgproc.dylib /opt/local/lib/libopencv_legacy.dylib /opt/local/lib/libopencv_ml.dylib /opt/local/lib/libopencv_nonfree.dylib /opt/local/lib/libopencv_objdetect.dylib /opt/local/lib/libopencv_photo.dylib /opt/local/lib/libopencv_stitching.dylib /opt/local/lib/libopencv_superres.dylib /opt/local/lib/libopencv_ts.dylib /opt/local/lib/libopencv_video.dylib /opt/local/lib/libopencv_videostab.dylib  

-------更新工作及后续问题-------

感谢Mark,我能够编译代码,虽然我不太确定我做对了。

首先,我将链接命令更改为依赖pkg-config并使用opencv3:

g++ $(pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc) vision.cpp -o test

这给我一个错误:

ld: library not found for -lippicv

然后,我从opencv3使用的lib文件夹中添加了一个指向该库的符号链接,这就完成了。

$ ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/Cellar/opencv3/3.1.0_3/lib/

这为我自己的理解带来了一些问题:

  • 这是正确的做法吗?

  • 我希望g++ $(pkg-config --cflags --libs opencv) vision.cpp -o test能够使用我安装的旧opencv库,但它会产生相同的“未找到符号”错误

  • 有没有办法永久更改pkg配置,以便pkg-config --cflags --libs opencv获取opencv3包配置,而不必在将来指定完整路径?

0 个答案:

没有答案