HOWTO: Install, Build and Use openCV (MacOSX 10.10)

转自: http://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/


Overview

Despite the wealth of information on the internet, installation guides for openCV are far and few between. Whilst we have used openCV packages in previous projects they have always been wrapped in an addon format, which obviously makes it easier to use but, for a forthcoming project we needed the ability to access the library directly. Briefly, openCV is a library of functions which mainly focuses on image analysis, processing and evaluation. In lamen’s terms, it allows computers to process and understand images and other forms of visual data.

In this post we will explain how we managed to work around the mysterious installation process, and provide a simple set of instructions that will enable you to install, build and use the openCV libraries and binaries on your system.

First you will need:

  • Mac OSX Yosemite 10.10
  • XCode
  • Command Line Tools (This is done from inside XCode)
  • CMake

Ready … lets begin!

Building OpenCV

Update:

Having rebuilt openCV using the XCode again the makefile for some reason is not generated, will try and find out why. For now use the Terminal method as outlined below.
Attention: Steps 2 a and b document methods of installing both the static and shared libraries.

Step 1:
Download openCV and unzip it somewhere on your computer. Create two new folders inside of the openCV directory, one called StaticLibs and the other SharedLibs.

Step 2a: Build the Static Libraries with Terminal.
To build the libraries in Terminal.
Screen Shot 2014-10-23 at 10.17.51

  • Open CMake.
  • Click Browse Source and navigate to your openCV folder.
  • Click Browse Build and navigate to your StaticLib Folder.
  • Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.

Screen Shot 2014-10-23 at 10.24.01
You will need to uncheck and add to the following options.

  • Uncheck BUILD_SHARED_LIBS
  • Uncheck BUILD_TESTS
  • Add an SDK path to CMAKE_OSX_SYSROOT, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.
  • Add x86_64 to CMAKE_OSX_ARCHITECTURES, this tells it to compile against the current system
  • Uncheck WITH_1394
  • Uncheck WITH_FFMPEG

Click Configure again, then Click Generate.

When the application has finished generating, Open Terminal and type the following commands.

- cd <path/to/your/opencv/staticlibs/folder/>
- make (This will take awhile)
- sudo make install

Enter your password.
This will install the static libraries on your computer.

Step 2c: Build the Shared Libraries with Terminal.

  • Open CMake.
  • Click Browse Source and navigate to your openCV folder.
  • Click Browse Build and navigate to your SharedLib Folder.
  • Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.

You will need to uncheck and add to the following options.

  • Check BUILD_SHARED_LIBS
  • Uncheck BUILD_TESTS
  • Add an SDK path to CMAKE_OSX_SYSROOT, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.
  • Add x86_64 to CMAKE_OSX_ARCHITECTURES, this tells it to compile against the current system
  • Uncheck WITH_1394
  • Uncheck WITH_FFMPEG

Click Configure again, then Click Generate.

When the application has finished generating, Open Terminal.

- cd <path/to/your/opencv/SharedLibs/folder/>
- make (This will take awhile)
- sudo make install

Enter your password.
This will install the shared libraries on your computer.

Make an Application

This is a very basic example, but the similar principles can be applied to other code.
For this post, lets make an application that shows two images, one normal and one that has been put through a blur filter.
Step 1:

  • Create a new folder somewhere on the computer.
  • Inside the folder, create a CMakeLists.txt file then create a BlurImage.cpp file.
  • Then add an image file.

For this example I’ll use this fruity number.
fruits

Step 2:

Open the BlurImage.cpp in your favourite text editor and add the following text.

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

Mat src; Mat dst;
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";

int main( int argc, char** argv )
{
    /// Load the source image
    src = imread( argv[1], 1 );

    namedWindow( window_name1, WINDOW_AUTOSIZE );
    imshow("Unprocessed Image",src);

    dst = src.clone();
    GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 );

    namedWindow( window_name2, WINDOW_AUTOSIZE );
    imshow("Processed Image",dst);

    waitKey();
    return 0;
}

Save the file.

Step 3: 

Open the CMakeLists.txt file then add the following text.

cmake_minimum_required(VERSION 2.8)
project( BlurImage )
find_package( OpenCV )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( BlurImage BlurImage.cpp )
target_link_libraries( BlurImage ${OpenCV_LIBS} )

Save the File.

Step 4: 
Open Terminal and navigate to your applications directory.

- cd <path/to/application/folder>
- /Applications/CMake.app/Contents/bin/cmake .
- make

This will generate both the makefile and the executable file.

Then type ./BlurImage image.jpg

Screen Shot 2014-10-23 at 13.44.56

Screen Shot 2014-10-23 at 13.45.52

Yay … it works all you need to do is add your own .cpp file and alter the .txt file. Then follow the above commands.

For more detailed examples go to the openCV Tutorial Page, or check out the sample folder inside the opencv folder.

Huge thank you to Daniel Shiffman, whose guide put us on the right track :http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值