在Mac下安装openCV

September 17, 2013

通过brew安装openCV运行库。

   brew install opencv

通过一个样例进行测试:

   mkdir example
   cd example
   emacs example.cpp

example.cpp文件:

#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char* argv[] )
{
  Mat image;
  image = imread( argv[1], 1 );
 
  if( argc != 2 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }
 
  namedWindow( "Example", CV_WINDOW_AUTOSIZE );
  imshow( "Example", image );
 
  waitKey(0);
 
  return 0;
}

通过cmake解决命令行下g++编译链接库的问题。 新建CMakeLists文件:

   project( example )
   find_package( OpenCV REQUIRED )
   add_executable( example example )
   target_link_libraries( example ${OpenCV_LIBS} )

编译文件:

   cmake .
   make

运行程序:

   ./example example.jpg